2011-10-20 3 views
3

URL에서 ExpandableListView까지 JSON을 구문 분석하는 프로젝트를 진행하고 있습니다. "상태"태그 값 (활성 또는 보류 중)에 따라 다른 그룹 "활성"및 "보류"에 따라 레코드가 배치됩니다. 모든 것이 잘 작동합니다.android의 expandablelistview에서 onChildClick 값을 얻는 방법은 무엇입니까?

내 문제는 다른 데이터를 표시해야하는 두 번째 그룹 내 자식을 클릭 할 때 데이터가 첫 번째 그룹 자식에서 데이터를 가져 오는 것입니다. 공공 부울 onChildClick은()가 호출

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.app.ExpandableListActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ExpandableListView; 
import android.widget.ListView; 
import android.widget.SimpleExpandableListAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.ExpandableListView.OnGroupClickListener; 

public class ExpandableActivity1 extends ExpandableListActivity { 
    JSONArray jArray = null; 
    JSONObject json_data = null; 
    TextView txtMLSID; 
     List<Map<String, String>> child1; 
    List<Map<String, String>> child2; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 



    List<Map<String, String>> groups = new ArrayList<Map<String, String>>(); 
    Map<String, String> group1 = new HashMap<String, String>(); 
    group1.put("group", " Active"); 
    groups.add(group1); 
    Map<String, String> group2 = new HashMap<String, String>(); 
    group2.put("group", " Pending"); 
    groups.add(group2); 
    child1 = new ArrayList<Map<String, String>>(); 
    child2 = new ArrayList<Map<String, String>>(); 




    String url ="http://stage.realtylog.net/iPhone/functions.php?username=hussain16&act=listing"; 

    String json = JSONfunctions.getJSONfromURL(url); 




try{ 




jArray = new JSONArray(json); 

for(int i=0;i<jArray.length() ;i++){      


    Map<String, String> childdata1 = new HashMap<String, String>(); 

     JSONObject e = jArray.getJSONObject(i); 

     String status = e.getString("2"); 
     Log.e("log_tag","Status: "+status); 

     if (status.contains("active")){ 
      Log.e("log_tag","StatusActive: "+status); 

      childdata1.put("0", String.valueOf(i+1)+" "+ e.getString("mlsid")); 



       child1.add(childdata1); 

     }else if(status.contains("pending")){ 
      Log.e("log_tag","StatusPending: "+status); 



       Map<String, String> childdata2= new HashMap<String, String>(); 
       childdata2.put("0", String.valueOf(i+1)+" "+ e.getString("mlsid")); 


       child2.add(childdata2); 




     } 


    } 

    }catch(JSONException e)  { 
Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 





     List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>(); 
     childs.add(child1); 
     childs.add(child2); 




    SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
      this, groups, R.layout.groups, new String[] { "group" }, 
      new int[] { R.id.group }, childs, R.layout.childs, 
      new String[] { "0"}, new int[] { R.id.child }); 
     setListAdapter(adapter); 
     } 

    @Override 
    public boolean setSelectedChild(int groupPosition, int childPosition, 
      boolean shouldExpandGroup) { 
     //do something 
     Log.e("log_tag","setSelectedChild: "); 
     return super.setSelectedChild(groupPosition, childPosition, 
        shouldExpandGroup); 
    } 

    @Override 
    public void setSelectedGroup(int groupPosition) { 
    //do something 

     Log.e("log_tag","setSelectedGroup: "); 
     super.setSelectedGroup(groupPosition); 
} 


**//Here I am Getting problem** 

@SuppressWarnings("unchecked") 
public boolean onChildClick(
      ExpandableListView parent, 
      View v, 
      int groupPosition, 
      int childPosition, 
      long id) { 
Log.d("LOG_TAG", "onChildClick: "+"GROUP: "+groupPosition+" CHILD Position: "+ childPosition); 

HashMap<String, String> o = (HashMap<String, String>) parent.getItemAtPosition(childPosition+1);   

       String val = o.get("0"); 
      Log.d("LOG_TAG", "selected value : "+val ); 




      return false; 

     } 






} 

로그 캣 추가 정보 :

10-20 11:24:28.004: DEBUG/LOG_TAG(1652): onChildClick: GROUP: 0 CHILD Position: 0 
10-20 11:24:28.004: DEBUG/LOG_TAG(1652): selected value : 1 555 
10-20 11:24:42.454: DEBUG/LOG_TAG(1652): onChildClick: GROUP: 1 CHILD Position: 0 
10-20 11:24:42.454: DEBUG/LOG_TAG(1652): selected value : 1 555 

로그 캣 내가 그룹 1 (활성) 및 그룹 2에 대해 같은 값을 얻고 있음을 보여줍니다 (보류)

내가 특정 값을 필요로 클릭을 기반으로 각 하위보기에 대해

답변

13

어댑터의 getChild(groupPosition, childPosition) 메서드를 사용하여 하위 인스턴스를 검색하고지도에 캐스트하여 작동 시키십시오.

@Override 
public boolean onChildClick(ExpandableListView parent, View v, 
     int groupPosition, int childPosition, long id) { 

    Toast.makeText(this, (String)((Map<String, String>) 
       adapter.getChild(groupPosition, childPosition)).get("lalit"), 
                 Toast.LENGTH_LONG).show(); 
    return super.onChildClick(parent, v, groupPosition, childPosition, id); 
} 
+1

고마워요, 그 작업은 – mH16

+3

글로벌 어댑터 인스턴스에 액세스 할 필요가 없습니다. 대신 제공된 메소드를 사용하여 getExpandableListAdapter()라는 어댑터를 가져 오십시오. – slott

3

이 시도 :

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo(); 

    String title = ((TextView) info.targetView).getText().toString(); 

    int type = ExpandableListView.getPackedPositionType(info.packedPosition); 
    System.out.println(" ----- " + type); 
    if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { 
     int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
     int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition); 
     Toast.makeText(this,title + ": Child " + childPos + " clicked in group "+ groupPos, Toast.LENGTH_SHORT).show(); 
     return true; 
    } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { 
     int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
     Toast.makeText(this, title + ": Group " + groupPos + " clicked",Toast.LENGTH_SHORT).show(); 
     return true; 
    } 

    return false; 
} 
+0

좋은 친구 ... –

+0

감사합니다. 내 응용 프로그램에서 수행했습니다. – Piraba

+0

@NagkeeranPiraba : 하위 항목을 클릭하면이 메서드가 호출되지 않습니다. 좀 더 설명 할 수 있습니까? – mH16

2

을 아래 완벽하지 만 작동합니다. ExpandableListView가있는 사용자 정의 대화 상자와 같이 onChildClick 메서드를 재정의하지 않고 뷰에 액세스해야하는 사용자에게 유용 할 수 있습니다.

ExpandableListView ellQuickKeys = (ExpandableListView) layout.findViewById(R.id.ellQuickKeys); 

    SimpleExpandableListAdapter expListAdapter = 
      new SimpleExpandableListAdapter(
          this, 
          createGroupList(),        // Creating group List. 
          R.layout.group_row,        // Group item layout XML.      
          new String[] { "Group Item" }, // the key of group item. 
          new int[] { R.id.row_name }, // ID of each group item.-Data under the key goes into this TextView.         
          createChildList(),        // childData describes second-level entries. 
          R.layout.child_row,        // Layout for sub-level entries(second level). 
          new String[] {"Sub Item"},    // Keys in childData maps to display. 
          new int[] { R.id.lblChildText}    // Data under the keys above go into these TextViews. 
        ); 

    ellQuickKeys.setOnChildClickListener(new OnChildClickListener() {  
     public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,int childPosition,long id) { 
      Log.d("Selected text: " + ((TextView) v.findViewById(R.id.lblChildText)).getText()); 

      return false; 
     } 
    }); 
    ellQuickKeys.setAdapter(expListAdapter); 

child_row.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 

<TextView android:id="@+id/lblChildText" 
    android:paddingLeft="50dp" 
    android:focusable="false" 
    android:textSize="18dp" 
    android:textStyle="normal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 

</LinearLayout> 
관련 문제