可扩展列表视图 - 子项,不同的布局

时间:2015-11-14 10:54:28

标签: java android listview expandable

我对使用该应用程序的可扩展列表有疑问。

当我开始显示/隐藏不同的群组时,我的列表会引发错误。然后他给我一个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

我不知道为什么它会向我显示此错误,如果我的convertView没有持有者,那么它会创建新的:(

首先,它使用List_Kategoria文件来存储类别。

其次,我有一些代表几种布局的列表。它们被输入类别。

以下是可扩展列表视图的一些方法。

  @Override
public Object getChild(int groupPosition, int childPosition) {
   ArrayList<Object> lista = catList.get(groupPosition).getItemList();
   Object obj = lista.get(childPosition);
    return obj;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    ArrayList<Object> lista = catList.get(groupPosition).getItemList();
    Object obj = lista.get(childPosition);
    return obj.hashCode();

}

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


    ViewHolder holder = null;

    ArrayList<Object> lista = catList.get(groupPosition).getItemList();
    Object obj = lista.get(childPosition);

    if(convertView != null) {
        holder = (ViewHolder) convertView.getTag();
    }

   if (convertView == null || convertView.getTag()==null) {
        holder = new ViewHolder();

        if(obj instanceof List_item_alarm_4 ){
            convertView = inflater.inflate(R.layout.list_item_exp_alarm,null);
        }
        if(obj instanceof List_item_wyswietl_0){
            convertView = inflater.inflate(R.layout.list_item_exp_temp,null);
        }
       if(obj instanceof List_item_tempzad_1){
           convertView = inflater.inflate(R.layout.list_item_exp_temp_zad,null);
       }
       if(obj instanceof List_item_went_wyswietl){
           convertView =  inflater.inflate(R.layout.list_item_exp_went_wyswietl,null);
       }
       if(obj instanceof List_item_went_zad){
           convertView = inflater.inflate(R.layout.list_item_exp_went_zad,null);
       }
        convertView.setTag(holder);
    }



    if(obj instanceof List_item_alarm_4 ) {
        holder.item_alarm = (TextView) convertView.findViewById(R.id.item_alarm);
        holder.item_alarm.setText("asd");
    }
    if(obj instanceof List_item_wyswietl_0){
        holder.item_temp_0_nazwa = (TextView) convertView.findViewById(R.id.item_temp_0_nazwa);
        holder.item_temp_0_wartosc = (TextView) convertView.findViewById(R.id.item_temp_0_wartosc);
        holder.item_temp_0_nazwa.setText("temp");
        holder.item_temp_0_wartosc.setText("XX");
    }
    if(obj instanceof List_item_tempzad_1){
        holder.item_tempzad_1_nazwa = (TextView) convertView.findViewById(R.id.item_tempzad_1_nazwa);
        holder.item_tempzad_1_nazwa.setText("conv");
    }
    if(obj instanceof List_item_went_wyswietl){
        holder.tv_wentRodzaj = (TextView) convertView.findViewById(R.id.tV_went_rodzaj);
        holder.tv_wentPrad = (TextView) convertView.findViewById(R.id.tV_wentPrad);
        holder.tv_wentCzest = (TextView) convertView.findViewById(R.id.tV_wentCzest);

        holder.tv_wentRodzaj.setText("asd");
        holder.tv_wentPrad.setText("asd");
        holder.tv_wentCzest.setText("asd");
    }
    if(obj instanceof List_item_went_zad){
        holder.tV_wentRodzajZad = (TextView) convertView.findViewById(R.id.tV_went_rodzaj_zad);
        holder.eT_went_niski = (EditText) convertView.findViewById(R.id.eT_went_niski);
        holder.eT_went_ekono = (EditText) convertView.findViewById(R.id.eT_went_ekono);
        holder.eT_went_komfort = (EditText) convertView.findViewById(R.id.eT_went_komf);
        holder.tV_wentRodzajZad.setText("NAWIEW");
        holder.eT_went_niski.setText("10");
        holder.eT_went_ekono.setText("10");
        holder.eT_went_komfort.setText("10");
    }

    return convertView;

}

@Override
public int getChildrenCount(int groupPosition) {
    int size = catList.get(groupPosition).getItemList().size();
    return size;
}

以下是我用于初始化数据的方法

private void initData(){         catList = new ArrayList();

    List_Kategoria cat1 = createCategory("NAME",0, "R.mipmap/ic_launcher");
    cat1.setItemList(createItemsCat1());
    catList.add(cat1);
    List_Kategoria cat2 = createCategory("NAME",1, "R.mipmap/ic_launcher");
    cat2.setItemList(createItemsCat2());
    catList.add(cat2);

}

private List_Kategoria createCategory(String name, long id, String imgPath) {
    return new List_Kategoria(id, name, imgPath);

}

private ArrayList<Object> createItemsCat1(){

    ArrayList<Object> result = new ArrayList<Object>();

    List_item_wyswietl_0 Dana1= new List_item_wyswietl_0(0, "Dana1", "xx.x");
    List_item_wyswietl_0 Dana2 = new List_item_wyswietl_0(1, "Dana2", "xx.x");
    List_item_tempzad_1 Dana3 = new List_item_tempzad_1(2, "Dana3", "xx.x");

    result.add(Dana1);
    result.add(Dana2);
    result.add(Dana3);
    return result;
}

private ArrayList<Object> createItemsCat2(){

    ArrayList<Object> result = new ArrayList<Object>();

    List_item_wyswietl_0 Dana1 = new List_item_wyswietl_0(0, "Dana1", "xxx");

    result.add(Grzanie);

    return result;
}

我再次检查了这个应用程序,如果我有3组或更多,我有错误,如果我有2组一切都可以:)

如果我有3个具有相同List_category的组,一切正常:(

如果我有组,哪一组有几个不同的列表元素,我有问题 为什么??? :(

2 个答案:

答案 0 :(得分:0)

试试这个

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


    ViewHolder holder = null;

    ArrayList<Object> lista = catList.get(groupPosition).getItemList();
    Object obj = lista.get(childPosition);

    if(convertView != null) {
        holder = (ViewHolder) convertView.getTag();
    }



    if (convertView == null || convertView.getTag()==null) {
        holder = new ViewHolder();

        if(obj instanceof List_item_alarm_4 ){
            convertView = inflater.inflate(R.layout.list_item_exp_alarm,null);
            holder.item_alarm = (TextView) convertView.findViewById(R.id.item_alarm);
        }
        if(obj instanceof List_item_wyswietl_0){
            convertView = inflater.inflate(R.layout.list_item_exp_temp,null);
            holder.item_temp_0_nazwa = (TextView) convertView.findViewById(R.id.item_temp_0_nazwa);
            holder.item_temp_0_wartosc = (TextView) convertView.findViewById(R.id.item_temp_0_wartosc);
        }
        if(obj instanceof List_item_tempzad_1){
            convertView = inflater.inflate(R.layout.list_item_exp_temp_zad,null);
            holder.item_tempzad_1_nazwa = (TextView) convertView.findViewById(R.id.item_tempzad_1_nazwa);
        }
        if(obj instanceof List_item_went_wyswietl){
            convertView =  inflater.inflate(R.layout.list_item_exp_went_wyswietl,null);
            holder.tv_wentRodzaj = (TextView) convertView.findViewById(R.id.tV_went_rodzaj);
            holder.tv_wentPrad = (TextView) convertView.findViewById(R.id.tV_wentPrad);
            holder.tv_wentCzest = (TextView) convertView.findViewById(R.id.tV_wentCzest);
        }
        if(obj instanceof List_item_went_zad){
            convertView = inflater.inflate(R.layout.list_item_exp_went_zad,null);
            holder.tV_wentRodzajZad = (TextView) convertView.findViewById(R.id.tV_went_rodzaj_zad);
            holder.eT_went_niski = (EditText) convertView.findViewById(R.id.eT_went_niski);
            holder.eT_went_ekono = (EditText) convertView.findViewById(R.id.eT_went_ekono);
            holder.eT_went_komfort = (EditText) convertView.findViewById(R.id.eT_went_komf);
        }
        convertView.setTag(holder);
    }



    if(obj instanceof List_item_alarm_4 ) {
        holder.item_alarm.setText("asd");
    }
    if(obj instanceof List_item_wyswietl_0){
        holder.item_temp_0_nazwa.setText("temp");
        holder.item_temp_0_wartosc.setText("XX");
    }
    if(obj instanceof List_item_tempzad_1){
        holder.item_tempzad_1_nazwa.setText("conv");
    }
    if(obj instanceof List_item_went_wyswietl){

        holder.tv_wentRodzaj.setText("asd");
        holder.tv_wentPrad.setText("asd");
        holder.tv_wentCzest.setText("asd");
    }
    if(obj instanceof List_item_went_zad){
        holder.tV_wentRodzajZad.setText("NAWIEW");
        holder.eT_went_niski.setText("10");
        holder.eT_went_ekono.setText("10");
        holder.eT_went_komfort.setText("10");
    }

    return convertView;

}

答案 1 :(得分:0)

问题在于背景。执行此操作并在初始化期间分配上下文

LayoutInflater infalInflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item_exp_alarm,null);