簡體   English   中英

如何在展開式列表視圖中添加子項? 安卓系統

[英]How to add Child Item in an expandable listview? Android

我試圖建立一個可擴展的列表視圖,其中我的標題來自數據庫,而我的子項是靜態的,在單擊的每個標題中重復顯示它。

就我而言,我已經完成了標題部分,但是每當我單擊列表視圖中的項目以顯示子項目時,應用程序就會崩潰。

我總是有這個錯誤。

Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference

這是我的ExpandableListAdapter

public class ExpandListAdapter extends BaseExpandableListAdapter {

private Context _context; 

List<String> child;
private ArrayList<Details> mDetails;
private HashMap<ArrayList<Details>, List<String>> mchildOfDetails;

public ExpandListAdapter(Context context, HashMap<ArrayList<Details>, List<String>> listChildData, ArrayList<Details> details) {
    this._context = context;
    this.mDetails = details;
    this.mchildOfDetails = listChildData;

}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this.mchildOfDetails.get(mDetails.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, parent,false);
    }

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

    txtListChild.setText(childText);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this.mchildOfDetails.size();
}

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

@Override
public int getGroupCount() {
    return mDetails.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 lblDesc = (TextView) convertView.findViewById(R.id.tv_desc);


    Details mDetailsobj = mDetails.get(groupPosition);

    lblListDesc.setText(mDetailsobj.getDesc());

    return convertView;
}

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

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

}

這是我使用可擴展列表視圖的片段

ExpandableListAdapter listAdapter;
ExpandableListView expListView;

List<String> child = new ArrayList<String>();
HashMap<ArrayList<Details>, List<String>> listDataChild;

ArrayList<Details> Details;


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

....

expListView = (ExpandableListView) v.findViewById(R.id.lvExp);

        prepareChildData();

        listDataChild = new HashMap<ArrayList<Details>, List<String>>();
        listDataChild.put(Details, child);

        listAdapter = new ExpandListAdapter(getActivity(), listDataChild, Details);
        expListView.setAdapter(listAdapter);

....
}

private void  prepareChildData(){
   child = new ArrayList<String>();

    child.add("One");
    child.add("Two");
    child.add("Three");
}

另一個說明:

我認為我的getChild()錯誤。 因為我無法在getChildView()內部顯示正在發生顯示的子列表。

先謝謝您的幫助。

所以,我要做的是創建一個新列表來處理子項

//header titles
private ArrayList<DetailsWithNoLocation> mDetailsWithNoLocation;
//child titles
List<String> mlistOtherDetails;
// child data in format of header title, child title
private HashMap<ArrayList<Details>, List<String>> mchildOfDetails;


 public ExpandListAdapter(Context context, ArrayList<Details> details, List<String> listOtherDetails, HashMap<ArrayList<Details>, List<String>> HashChildData) {
    this._context = context;
    this.mDetails = detailsWithNoLocation; //header
    this.mlistOtherDetails = listOtherDetails; // child
    this.mchildOfDetails = HashChildData; //hash data
}

然后在我的getChild()中,而不是返回我上次調用新創建的列表時使用的內容

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

我現在可以顯示我的子視圖,但不必在每行中分別顯示三個值,分別為{One,Two,Three}。 每行顯示一個值,因此我仍在處理它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM