2011-10-20 62 views
3

我正在從URL解析JSON到ExpandableListView。取決於「狀態」標籤值(活動或未決),將相應地將記錄放置到不同的組「活動」和「待定」。一切工作正常。如何從android中的expandablelistview獲取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; 

     } 






} 

logcat的信息時,公共布爾onChildClick()調用:

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 

logcat的顯示,我對組1(活動)和第2組獲得相同的值(待定)

我需要特定的值基於點擊每個子視圖。

回答

13

您必須使用Adapter的​​方法檢索子實例並將其轉換爲您的Map並使其運行。

@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

Thanks.I在我的應用程序 – Piraba

+0

@NagkeeranPiraba:當我點擊子項目時不會調用此方法,你能解釋我多一點嗎? – mH16

2

下面是不完美的,但它的工作原理。對於那些需要訪問視圖而不覆蓋onChildClick方法(如帶有ExpandableListView的自定義對話框)的用戶可能會有幫助。

​​

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> 
相關問題