我試圖創建一個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,它仍然關閉我的應用程序。
的機會是'NPE'。請在'dbHelper.getList(id);'後檢查'arrayList'的值。它可能返回'null'。 –
什麼是NPE? @ViswanathL –
我使用吐司檢查了我的ArrayList和它的數據在它@ViswanathL –