2016-11-19 7 views
-4

データベース値をListviewに表示しようとしていますが、要素の位置を手動で設定しています(if(position == 0){})。しかし、これらの要素をID、名前、モバイルに応じて表示したいとします(つまり、第1要素アクティビティでこれらのパラメータを解析して2番目のパラメータが適切な値を保持できるようにするなど)。 これをどのように実行できますか?位置に応じてListViewの値を取得する方法は?

List.java

protected void showList(){ 
      try { 
       JSONObject jsonObj = new JSONObject(myJSON); 
       peoples = jsonObj.getJSONArray(TAG_RESULTS); 

       for(int i=0;i<peoples.length();i++){ 
        JSONObject c = peoples.getJSONObject(i); 
        String id = c.getString(TAG_ID); 
        String name = c.getString(TAG_NAME); 
        String mobile = c.getString(TAG_MOB); 

        HashMap<String,String> persons = new HashMap<String,String>(); 

        persons.put(TAG_ID,id); 
        persons.put(TAG_NAME,name); 
        persons.put(TAG_MOB,mobile); 

        personList.add(persons); 
       } 

       ListAdapter adapter = new SimpleAdapter(
         User_List.this, personList, R.layout.list_item, 
         new String[]{TAG_ID,TAG_NAME,TAG_MOB}, 
         new int[]{R.id.id, R.id.name, R.id.mobile} 
       ); 

       list.setAdapter(adapter); 
       list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
          if(position==0) { 

           Intent intent = new Intent(User_List.this, MapsActivity.class); 
           startActivity(intent); 
          } 
        } 
       }); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
+0

は、次のアクティビティは同じですか? –

+0

@Satish Silveri ..yes! –

答えて

0

あなたがする必要があるのは、このようなpersonList配列内のその位置にハッシュマップのオブジェクトを送信です:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

           Intent intent = new Intent(User_List.this, MapsActivity.class); 
           intent.putExtra("object",(Serializable)personList.get(position)); 
           startActivity(intent); 
          } 

       }); 

と別のアクティビティに:

HashMap<String,String> object=(HashMap<String, String>)getIntent().getSerializableExtra("object"); 

とし、次にようにtextviewを設定してください:

textview.setText(object.TAG_ID); 
textview1.setText(object.TAG_NAME); 
textview2.setText(object.TAG_MOB); 

これが動作すればチェックしてください!!!!!

uはもうactvities間でハッシュマップ合格する方法に疑問している場合は、下のリンクをチェック:リストビュー内のすべての要素のための

Android - How to pass HashMap<String,String> between activities?

+0

TAG_IDで「解決できません」と表示されます... –

+0

try object.get(TAG_ID) –

+0

はい!...動作します!...ありがとうございます! –

関連する問題