2015-07-20 90 views
0

我試圖創建一個ExpandableListView,下面是我的擴展列表申請停止

DBHelper dbHelper; 
ExpandableListAdapter listAdapter; 
ExpandableListView expListView; 
HashMap<String, List<String>> hashDataChild; 
ArrayList<DetailsOfLocation> arrayList; 

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

expListView = (ExpandableListView) v.findViewById(R.id.lvExp); // get the listview 
initializeDatabase(); 
     prepareChildData(); 

listAdapter = new ExpandListAdapter(getActivity(), arrayList, hashDataChild); 
     expListView.setAdapter(listAdapter); 

return v; 
} 

public void initializeDatabase() 
{ 
    dbHelper = new DBHelper(getActivity()); 

    try { 
     dbHelper.createDataBase(); 
    } catch (IOException ioe) { 
     throw new Error("Unable to create database"); 
    } 

    try { 
     dbHelper.openDataBase(); 
    } catch (SQLException sqle) { 
     throw sqle; 
    } 
} 

private void prepareChildData(){ 
    hashDataChild = new HashMap<String, List<String>>(); 

//arrayList has a multiple column in it where it will be displayed as the header. The data is from the database 

    Integer id = ((MyApplication) getActivity().getApplication()).getID(); 
    arrayList = new ArrayList<DetailsOfLocation>(); 
    arrayList = dbHelper.getList(id); 

//child is the child's data 
    List<String> child = new ArrayList<String>(); 
    child.add("Other Info"); 
    child.add("Picture"); 
    child.add("Contact"); 

//the header and child is being inserted 
    for(DetailsOfLocation list : arrayList){ 
      hashDataChild.put(String.valueOf(list), child); 
     } 
    } 
} 

創建數據當我調試它的代碼中,hashDataChild有我期待的每個頭裏有3個孩子的值。但在我的手機上試用時,它會崩潰,並且不會在logcat中顯示任何內容。我使用的是用於創建可擴展列表視圖的引用[這]:http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

我在想,這可能是因爲ExpandableListview OnGroupClickListener不工作?我該如何解決這個問題?它去我onGroupClick內,但沒有顯示孩子的名單

// Listview Group click listener 
     expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { 
      @Override 
      public boolean onGroupClick(ExpandableListView parent, View v, 
             int groupPosition, long id) { 
       Toast.makeText(
         getActivity().getApplicationContext(), 
         arrayList.get(groupPosition) 
           + " : " 
           + hashDataChild.get(
           arrayList.get(groupPosition)), Toast.LENGTH_SHORT) 
         .show(); 

       return false; 
      } 
     }); 

下面是我爲我的可擴展適配器代碼

public class ExpandListAdapter extends BaseExpandableListAdapter { 

private Context _context; 
private ArrayList<DetailsOfLocation> mDetailsWithNoLocation; 
private HashMap<String, List<String>> mchildOfDetailsWithNoLocation; 

public ExpandListAdapter(Context context, ArrayList<DetailsOfLocation> detailsWithNoLocation, HashMap<String, List<String>> HashChildData) { 
    this._context = context; 
    this.mDetailsWithNoLocation = detailsWithNoLocation; //header 
    this.mchildOfDetailsWithNoLocation = HashChildData; //hash data 
} 


@Override 
public Object getChild(int groupPosition, int childPosititon) { 
    return this.mchildOfDetailsWithNoLocation.get(this.mDetailsWithNoLocation.get(groupPosition)).get(childPosititon); 
} 

@Override 
public long getChildId(int groupPosition, int childPosition) { 
    return childPosition; 
} 

@Override 
public View getChildView(int groupPosition, final int childPosition, 
         boolean isLastChild, View convertView, ViewGroup parent) { 

    final String childText = (String) getChild(groupPosition, childPosition); 

    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.expandlist_child_item, null); 
    } 

    TextView txtListChild = (TextView) convertView 
      .findViewById(R.id.tvother_info); 

    txtListChild.setText(childText); 
    return convertView; 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    return this.mchildOfDetailsWithNoLocation.get(this.mDetailsWithNoLocation.get(groupPosition)) 
      .size(); 
} 


@Override 
public Object getGroup(int groupPosition) { 
    return this.mDetailsWithNoLocation.get(groupPosition); 
} 

@Override 
public int getGroupCount() { 
    return mDetailsWithNoLocation.size(); 
} 

@Override 
public long getGroupId(int groupPosition) { 
    return groupPosition; 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, 
         View convertView, ViewGroup parent) { 


    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.expandlist_group_item, null); 
    } 

    TextView lblListDesc = (TextView) convertView.findViewById(R.id.tv_desc); 
    TextView lblListRoom = (TextView) convertView.findViewById(R.id.tv_location); 
    TextView lblListAudience = (TextView) convertView.findViewById(R.id.tv_audience); 
    TextView lblListSpeaker = (TextView) convertView.findViewById(R.id.tv_speaker); 


    DetailsOfLocation noLocationobj = mDetailsWithNoLocation.get(groupPosition); 

    lblListDesc.setText(noLocationobj.getDesc()); 
    lblListRoom.setText("Room: "+noLocationobj.getLocation()); 
    lblListAudience.setText("Audience: "+noLocationobj.getAudience()); 
    lblListSpeaker.setText("Speaker/s: "+noLocationobj.getSpeaker()); 


    return convertView; 
} 

@Override 
public boolean hasStableIds() { 
    return false; 
} 

@Override 
public boolean isChildSelectable(int groupPosition, int childPosition) { 
    return true; 
} 

} 

非常感謝你的幫助。


更新

我曾試圖返回在我onGroupClick()和hashDataChild.get真( arrayList.get(groupPosition))爲空,爲什麼會這樣,即使檢查的ArrayList時,它具有值?


我想我找到了原因,採用這種

if(!hashDataChild.containsKey(arrayList)) 

我hashDataChild不包含關鍵我不知道爲什麼,以及如何解決它的任何想法,爲什麼是這樣呢?即使當我調試它顯示它的鍵和我的arrayList的值?


我已經改變的containsKey和使用String.valueOf(arrayList.get(groupPosition))的建議。但是我的孩子數據仍然不顯示,並在onGroupClick()中返回false,它仍然關閉我的應用程序。

+0

的機會是'NPE'。請在'dbHelper.getList(id);'後檢查'arrayList'的值。它可能返回'null'。 –

+0

什麼是NPE? @ViswanathL –

+0

我使用吐司檢查了我的ArrayList和它的數據在它@ViswanathL –

回答

1

好吧,我已經試過你的代碼和點擊導致NullPointerException異常, 你錯過了將String.valueOf()在你的mDetailsWithNoLocation的每一個電話, 在適配器更改這兩個方法

@Override 
public int getChildrenCount(int groupPosition) { 
    return this.mchildOfDetailsWithNoLocation.get(String.valueOf(
      this.mDetailsWithNoLocation.get(groupPosition))).size(); 
} 

@Override 
public Object getChild(int groupPosition, int childPosititon) { 
    return this.mchildOfDetailsWithNoLocation.get(String.valueOf(
      this.mDetailsWithNoLocation.get(groupPosition))).get(
      childPosititon); 
} 

現在應該有工作.. 筆記:你也應該在你的問題中發佈你的活動代碼,以使其他人更容易測試你的代碼。我需要做一些猜測,以測試這.. :)

+0

非常感謝您的支持!它真的幫助我很多:)我會把我的活動代碼。我在我的一個標籤中使用它,這就是爲什麼我只發佈我的片段。最後一個問題如何控制每個孩子被點擊時? –

+0

不客氣,你的意思是ExpandableListView.setOnChildClickListener()? – fchristysen

+0

是的..我很困惑如何使用它,因爲我試着把它敬酒,然後當我點擊標題/父母它會觸發代碼,但我期待它只會觸發當我點擊子項目。 –