微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

getChidrenCount 空指针,我得到了一个冗余列表

如何解决getChidrenCount 空指针,我得到了一个冗余列表

我想在来自 sqlite 数据库的 ExpandableList 中按类别显示文章列表。组是类别,项目是文章。 我在文章中的作用

public List<Article> getAllArticle() {
    List<Article> listArticle = new ArrayList<Article>();
    Article art = null;
    String[]columns = {DBHelper.COLUMN_ARTICLES_NAME,DBHelper.COLUMN_ARTICLES_PRIX,DBHelper.COLUMN_ARTICLES_DATE};

    mDatabase = new DBHelper(mContext).getWritableDatabase();
    Cursor c = mDatabase.query(DBHelper.TABLE_ARTICLES,columns,null,null);
    c.movetoFirst();
    while (!c.isAfterLast()) {
        art = new Article();
        art.setName(c.getString(c.getColumnIndex(DBHelper.COLUMN_ARTICLES_NAME)));
        art.setPrix(c.getDouble(c.getColumnIndex(DBHelper.COLUMN_ARTICLES_PRIX)));
        art.setDateAchat(c.getString(c.getColumnIndex(DBHelper.COLUMN_ARTICLES_DATE)));
        listArticle.add(art);
        c.movetoNext();
    }

我的功能分类

public List<Article> getArticleByCategory(int catId) {
    List<Article> listArticle = new ArrayList<Article>();
    Article art = null;
    String[]columns = {DBHelper.COLUMN_ARTICLES_NAME,DBHelper.COLUMN_ARTICLES_DATE};
    String selection = DBHelper.COLUMN_ARTICLES_CATEGORY + " = ? ";
    String[] selectionArgs = new String[]{String.valueOf(catId)};
    mDatabase = new DBHelper(mContext).getWritableDatabase();
    Cursor c = mDatabase.query(DBHelper.TABLE_ARTICLES,selection,selectionArgs,null);
    c.movetoFirst();
    while (!c.isAfterLast()) {
        art = new Article();
        art.setName(c.getString(c.getColumnIndex(DBHelper.COLUMN_ARTICLES_NAME)));
        art.setPrix(c.getDouble(c.getColumnIndex(DBHelper.COLUMN_ARTICLES_PRIX)));
        art.setDateAchat(c.getString(c.getColumnIndex(DBHelper.COLUMN_ARTICLES_DATE)));
        listArticle.add(art);
        c.movetoNext();
    }

我的活动

ExpandableListView listArt; ArrayList listGroup = new ArrayList();

HashMap listChild= new HashMap();

BottomNavigationView bnav;
NavigationView nav;
Context ctx;
ArrayList<Integer> lva;
private ListArticles mAdapter;
DBHelper helper = new DBHelper(this);
ArticleDAO DB = new ArticleDAO(this);
CategoryDAO CA = new CategoryDAO(this);
Button shop;
CheckBox sh;
 protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    nomUser = (TextView) findViewById(R.id.loguse);
    identi = (TextView) findViewById(R.id.numUse);
    listArt = (ExpandableListView) findViewById(R.id.listArticle);
  
    emptyArt = (TextView) findViewById(R.id.link_to_shopplist);
    cat = (TextView) findViewById(R.id.link_to_cat);
    Ajt = (TextView) findViewById(R.id.link_to_ajout);
    shop = (Button) findViewById(R.id.btn_send);

    Intent in = getIntent();
    Bundle b = in.getExtras();
    if (b != null) {
        String j = b.getString("username");
        nomUser.setText("" + j);
        int k = (int) b.get("ID");
        identi.setText("" + k);
    }


    int ids = Integer.parseInt(identi.getText().toString());
    long iduse = Long.valueOf(ids);


    Ajt.setonClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(),AjoutArticleActivity.class);
            i.putExtra("id",ids);
            startActivity(i);
        }
    });



    shop.setonClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ArrayList<String> mylist = new ArrayList<String>();
            for (int i = 0; i < listArt.getChildCount(); i++) {

                sh = (CheckBox) listArt.getChildAt(i).findViewById(R.id.check_art);
                if (sh instanceof CheckBox) {
                    CheckBox check = (CheckBox) sh;
                    if (check.isChecked()) {
                        //na = listChild.get("name").get(i).getName();
                        na = listChild.get("name").get(i);
                        mylist.add(na);
                        Log.d("h","the list is " + mylist);

                    }
                }
            }


            inte = new Intent(getApplicationContext(),ShoppingListActivity.class);
            inte.putExtra("val",mylist);
            inte.putExtra("is",iduse);
            startActivity(inte);


        }
    });


    ArrayList<Integer> arrayList = new ArrayList<>();
    List<Category> mListCategory = CA.getAllCategory();
    int is;
    for (int K = 0; K <= mListCategory.size() - 1; K++) {
        String nom = mListCategory.get(K).getName();
        is = (int) mListCategory.get(K).getId();
        arrayList.add(is);
        listGroup.add(nom);
        Log.d("h","the cat is " + arrayList);
    }

  
    ArrayList<String> arti = new ArrayList<>();
   List<Article> la = null;
   
    for (Integer str : arrayList) {
        la = DB.getArticleByCategory(str);

        for (int i = 0; i < la.size(); i++) {
          
             String no = la.get(i).getName();
             arti.add(no);
          
        }
       
      listChild.put(str,arti);

    }

    mAdapter = new ListArticles(listGroup,listChild);
    listArt.setAdapter(mAdapter);
}

我的领养者

public class ListArticles extends Baseexpandablelistadapter  {
public static final String TAG = "ListArticles";
private List<Category> listCat;
private LayoutInflater mInflater;
private SparseBooleanArray mCheckStates;
HashMap<Category,ArrayList<String>> listChild;
public ListArticles(HashMap<Category,ArrayList<String>> listChild,List<Category> listCat) {
   this.listCat=listCat;
   this.listChild=listChild;
    mCheckStates = new SparseBooleanArray(listChild.size());
}
public boolean isChecked(int position) {
    return mCheckStates.get(position,false);
}
public void setChecked(int position,boolean isChecked) {
    mCheckStates.put(position,isChecked);
}
public void toggle(int position) {
    setChecked(position,!isChecked(position));
}
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
    mCheckStates.put((Integer) buttonView.getTag(),isChecked);
}
@Override
public int getGroupCount() {
    return listCat.size();
}
@Override
public int getChildrenCount(int i) {
    return listChild.get(listCat.get(i)).size();
}
@Override
public Object getGroup(int i) {
    return listCat.get(i);
}
@Override
public Object getChild(int i,int i1) {
    return listChild.get(listCat.get(i)).get(i1);
}
@Override
public long getGroupId(int groupPosition) {
    return 0;
}
@Override
public long getChildId(int groupPosition,int childPosition) {
    return 0;
}
@Override
public boolean hasstableIds() {
    return false;
}
@Override
public View getGroupView(int i,boolean v,View view,ViewGroup viewGroup) {
    view = LayoutInflater.from(viewGroup.getContext())
            .inflate(android.R.layout.simple_expandable_list_item_1,viewGroup,false);
    TextView textView=(TextView) view.findViewById(android.R.id.text1);
    String sGroup = String.valueOf(getGroup(i));
    textView.setText(sGroup);
    textView.setTypeface(null,Typeface.BOLD);
    textView.setTextColor(Color.BLUE);
    return view;
}
@Override
public View getChildView(int i,int i1,boolean b,ViewGroup viewGroup) {
    view = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.list_item_article,false);
     TextView txtArticleName = (TextView) view.findViewById(R.id.txt_article_name);
    TextView txtPrix = (TextView) view.findViewById(R.id.txt_prix);
    TextView txtDate = (TextView) view.findViewById(R.id.txt_date);
    CheckBox chekArt=(CheckBox) view.findViewById(R.id.check_art);
   List<Article> currentItem = (List<Article>) getChild(i,i1);
   // String sChild = String.valueOf(getChild(i,i1));
    //txtArticleName.setText(sChild);
    if(currentItem != null) {
        String name = String.valueOf(currentItem.get(0));
        txtArticleName.setText(name);
        String prix = String.valueOf(currentItem.get(1));
        txtPrix.setText(prix);
        String date = String.valueOf(currentItem.get(2));
        txtDate.setText(date);
        chekArt.setTag(i,i1);
        chekArt.setChecked(mCheckStates.get(i,false));
        chekArt.setonCheckedchangelistener(this);
    }
    return view;
}
@Override
public boolean isChildSelectable(int groupPosition,int childPosition) {
    return false;
}
class ViewHolder {
    TextView txtArticleName;
    TextView txtPrix;
    TextView txtDate;
  
    CheckBox chekArt;
}

    

我得到了 java.lang.classCastException:java.util.ArrayList 无法转换为 java.util.HashMap 进程:com.example.gestcourses,PID:22664

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。