ExpandableListView- 在 Activity 中更新或删除特定行而不调用适配器

ExpandableListView- Update or Remove a particular Row without calling the adapter in Activity

我在从数据库中获取数据后实现了一个 expandableListview。 数据在列表中正常显示,子项详细信息页面上的 onClicking() 正在打开,用户可以在其中删除数据并编辑数据。但是在返回到 MainActivity 之后,我必须再次调用 OnResume 中的 Adpater class 来重新加载我不需要的数据。我只想删除 或显示更新的 数据。 我试过 notifyDataSetChanged() 但它没有用,可能是我用错了 place.Please 指导我一点...

适配器Class..

public class TransExpAdapter extends BaseExpandableListAdapter {
    private Context context;
    List<ExcelDataModel> list_shareofshelf;

    public TransExpAdapter(Context context, List<ExcelDataModel> list_shareofshelf) {
        this.context = context;
        this.list_shareofshelf = list_shareofshelf;
    }


    @Override
    public Object getChild(int listPosition, int expandedListPosition) {
        return this.list_shareofshelf.get(listPosition);
    }

    @Override
    public long getChildId(int listPosition, int expandedListPosition) {
        return expandedListPosition;
    }

    @Override
    public View getChildView(final int listPosition, final int expandedListPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View cview = layoutInflater.inflate(R.layout.list_child_sos, null);

        RelativeLayout child_container = cview.findViewById(R.id.child_container);
        TextView tv_category = cview.findViewById(R.id.tv_category);
        TextView tv_expense = cview.findViewById(R.id.tv_expense);
        ImageView iv_icon = cview.findViewById(R.id.iv_icon);

        try {
            if (list_shareofshelf.get(listPosition).getGroupFocAll().get(expandedListPosition).getIcon_name() != null &&
                    !list_shareofshelf.get(listPosition).getGroupFocAll().get(expandedListPosition).getIcon_name().isEmpty()) {
                int imageid = context.getResources().getIdentifier(list_shareofshelf.get(listPosition).getGroupFocAll().get(expandedListPosition).getIcon_name(), "drawable", context.getPackageName());
                iv_icon.setImageResource(imageid);
            } else {
                iv_icon.setImageResource(R.drawable.ic_gray_no_img);
            }
        } catch (Exception e) {
            iv_icon.setImageResource(R.drawable.ic_gray_no_img);
        }

        if (list_shareofshelf.get(listPosition).getGroupFocAll().get(expandedListPosition).getIncome_Expenses().equalsIgnoreCase("Expenses")) {
            tv_expense.setText("-" + list_shareofshelf.get(listPosition).getGroupFocAll().get(expandedListPosition).getAmount());
        } else {
            tv_expense.setText(list_shareofshelf.get(listPosition).getGroupFocAll().get(expandedListPosition).getAmount());

        }


        child_container.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!list_shareofshelf.get(listPosition).getGroupFocAll().get(expandedListPosition).getId().isEmpty()) {
                    Intent i = new Intent(context, ExpenseDetails.class);
                    i.putExtra("trans_id", list_shareofshelf.get(listPosition).getGroupFocAll().get(expandedListPosition).getId());
                    context.startActivity(i);
                }
            }
        });

        if (!list_shareofshelf.get(listPosition).getGroupFocAll().get(expandedListPosition).getMemo().isEmpty()) {
            tv_category.setText(list_shareofshelf.get(listPosition).getGroupFocAll().get(expandedListPosition).getMemo());
        } else {
            tv_category.setText(list_shareofshelf.get(listPosition).getGroupFocAll().get(expandedListPosition).getCategory());
        }

        if (expandedListPosition == list_shareofshelf.get(listPosition).getGroupFocAll().size() - 1) {
            child_container.setBackgroundResource(R.drawable.card_bottom_round);
        }

        return cview;
    }


    @Override
    public int getChildrenCount(int listPosition) {
        return list_shareofshelf.get(listPosition).getGroupFocAll().size();
    }

    @Override
    public Object getGroup(int listPosition) {
        return list_shareofshelf.get(listPosition).getGroupFocAll();
    }

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

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

    @Override
    public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.list_parent_sos, null);
        ExpandableListView eLV = (ExpandableListView) parent;
        eLV.expandGroup(listPosition);
        TextView listTitleTextView = view.findViewById(R.id.listTitle);

        TextView tv_expenditures = view.findViewById(R.id.tv_expenditures);
        listTitleTextView.setText(Utils.getFormattedDate(list_shareofshelf.get(listPosition).getDate()));

        tv_expenditures.setText("Expenses: " + list_shareofshelf.get(listPosition).getExp_amt_() + " Income: " + list_shareofshelf.get(listPosition).getInc_amt_());


        return view;
    }

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

    @Override
    public boolean isChildSelectable(int listPosition, int expandedListPosition) {
        return true;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        super.registerDataSetObserver(observer);
    }
}

MainActivity 调用...

private class AsyncTaskGetTransactionData extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... voids) {
            list_final_transaction_data = db.getTransactionDetails(current_month);
            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            if (list_final_transaction_data.size() > 0) {
                expandableListAdapter = new TransExpAdapter(context, list_final_transaction_data);
                expandableListView.setAdapter(expandableListAdapter);
                expandableListView.setVisibility(View.VISIBLE);
                new AsyncTaskSummationExpenses().execute();
            } else {
                tv_income.setText("0");
                tv_expenses.setText("0");
                tv_balance.setText("0");
                p.dismiss();
                expandableListView.setVisibility(View.GONE);
            }
        }
    }

////数据库方法

 public List<ExcelDataModel> getTransactionDetails(String current_month) {
        List<ExcelDataModel> listProd = new ArrayList<>();
        Log.d("CurrentMonthDB", current_month);

        SQLiteDatabase db = this.getWritableDatabase();
        try {
            String selectQuery = "select f.date,(select sum(amount) from tbl_transactions WHERE trans_type='Expenses' and date=f.date)exp, " +
                    "(select sum(amount) from tbl_transactions where trans_type='Income' and date=f.date)inc from tbl_transactions f where f.date like '%" + current_month + "%' GROUP by f.date ORDER BY strftime('%s', f.date) desc";

            Log.d("FirstQuery", selectQuery);
            Cursor c = db.rawQuery(selectQuery, null);
            if (c.moveToFirst()) {
                do {
                    ExcelDataModel foc_cat = new ExcelDataModel();
                    String date = c.getString(c.getColumnIndex("date"));
                    String income = c.getString(c.getColumnIndex("inc"));
                    String expense = c.getString(c.getColumnIndex("exp"));
                    String selectQuery1 = "select * from tbl_transactions a LEFT join tbl_category b where a.cat_id=b.cat_id and date='" + date + "' order by memo asc";
                    Log.d("SecondQuery", selectQuery1);
                    Cursor cursor2 = db.rawQuery(selectQuery1, null);
                    if (cursor2.moveToFirst()) {
                        List<ExcelDataModel> subCat_list = new ArrayList<>();
                        // ExcelDataModel pp_dummy = new ExcelDataModel();
                        //pp_dummy.setId("");
                        // subCat_list.add(pp_dummy);
                        do {
                            ExcelDataModel prod_subcat = new ExcelDataModel();
                            prod_subcat.setDate(cursor2.getString(cursor2.getColumnIndex("date")));
                            prod_subcat.setIncome_Expenses(cursor2.getString(cursor2.getColumnIndex("trans_type")));
                            prod_subcat.setCategory(cursor2.getString(cursor2.getColumnIndex("cat_name")));
                            prod_subcat.setMemo(cursor2.getString(cursor2.getColumnIndex("memo")));
                            prod_subcat.setAmount(cursor2.getString(cursor2.getColumnIndex("amount")));
                            prod_subcat.setId(cursor2.getString(cursor2.getColumnIndex("trans_id")));
                            prod_subcat.setIcon_name(cursor2.getString(cursor2.getColumnIndex("cat_iconname")));
                            subCat_list.add(prod_subcat);
                        } while (cursor2.moveToNext());
                        foc_cat.setDate(date);

                        if (expense != null) {
                            foc_cat.setExp_amt_(Double.parseDouble(expense));
                        } else {
                            foc_cat.setExp_amt_(0);
                        }

                        if (income != null) {
                            foc_cat.setInc_amt_(Double.parseDouble(income));
                        } else {
                            foc_cat.setInc_amt_(0);
                        }

                        foc_cat.setDate(date);
                        foc_cat.setGroupFocAll(subCat_list);
                        listProd.add(foc_cat);
                        cursor2.close();
                    }
                } while (c.moveToNext());
            }
            c.close();
            db.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (db != null)
                db.close();
        }
        return listProd;
    }

尝试在 child 的 OnClick() 上使用回调并在 activity 中捕获该回调,这样您就可以 startActivityForResult() 而不是 [=14] =].

这样您就可以知道用户何时删除了详细信息 activity 中的某些内容,紧接着 activity 被销毁。然后,在 onActivityResult 你做 yourAdapter.notifyDataSetChanged();

像这样,在适配器中:

child_container.setOnClickListener(new View.OnClickListener() {

    @Override
         public void onClick(View v) {
         if (!list_shareofshelf.get(listPosition).getGroupFocAll().get(expandedListPosition).getId().isEmpty()) {
              callback.childClicked(list_shareofshelf.get(listPosition).getGroupFocAll().get(expandedListPosition).getId());
         }
     }
});

然后,在您的 activity 中实现回调

@Override
public void childClicked(int id){
    Intent i = new Intent(context, ExpenseDetails.class);
    i.putExtra("trans_id", id);
    startActivityForResult(i, yourRequestCode);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable 
    Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == yourRequestCode) {
        if (resultCode == RESULT_DELETED) {
            adapter.notifyDataSetChanged();
        }
    }
}