简体   繁体   English

从数据库中删除数据后如何刷新listview?

[英]how to refresh listview after deleting data from database?

All Transaction Activity is used to to fetch data from database 所有Transaction Activity用于从数据库中获取数据

public class AllTransactionActivity extends MainActivity {

    ArrayList<String> price = new ArrayList<String>();
    ArrayList<String> category = new ArrayList<String>();
    ArrayList<String> date = new ArrayList<String>();
    ArrayList<String> id = new ArrayList<String>();
    ArrayList<String> payment_mode = new ArrayList<String>();
    ArrayList<String> description = new ArrayList<String>();
    RadioButton rb1 , rb2 ;
    ListView listView ;
    Cursor cv;
    LinearLayout list_lay;
    RelativeLayout no_data_lay;
    String date_change;
    Boolean check_btn = false;
    Transactiondata data ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_transaction);
        findViewById();

        Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar);
        TextView mTitle = (TextView) toolbarTop.findViewById(R.id.title);
        mTitle.setText("All Transaction");

        data = new Transactiondata(AllTransactionActivity.this);
        data.open();
        cv = data.incomeDisplayData();

            id.clear();
            price.clear();
            category.clear();
            date.clear();
            payment_mode.clear();
            description.clear();

            if (cv.moveToFirst()) {
                do {
                    category.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_CATEGORY)));
                    id.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_ID)));
                //    date.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_DATE)));
                    SimpleDateFormat simpleDateFormate = new SimpleDateFormat("yyyy-MM-dd");
                    Date date1 = null;
                    try {
                        date1 = simpleDateFormate.parse(cv.getString(cv.getColumnIndex(Transactiondata.KEY_DATE)));
                        SimpleDateFormat fmtOut = new SimpleDateFormat("dd-MM-yyyy");

                        date_change = fmtOut.format(date1);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    date.add(date_change);
                    price.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_PRICE)));
                    payment_mode.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_PAYMENT_MODE)));
                    description.add(cv.getString(cv.getColumnIndex(Transactiondata.KEY_DESCRIPTION)));

                } while (cv.moveToNext());

            }else {

                list_lay.setVisibility(View.GONE);
                no_data_lay.setVisibility(View.VISIBLE);
            }

            listView.setAdapter(new DisplayAllDataAdapter(this, AllTransactionActivity.this , check_btn , id , category , date , price , payment_mode , description ));
            cv.close();

    }

    public void findViewById(){

        listView = (ListView)findViewById(R.id.list);
        rb1 =(RadioButton)findViewById(R.id.rb1) ;
        rb2 =(RadioButton)findViewById(R.id.rb2) ;
        no_data_lay = (RelativeLayout)findViewById(R.id.no_data_lay);
        list_lay = (LinearLayout)findViewById(R.id.list_lay);
    }
}

Custom Adapter Class which is used to bind data with list view 自定义适配器类,用于将数据与列表视图绑定

package app.dailyexpensemanager.adapters;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import app.dailyexpensemanager.R;
import app.dailyexpensemanager.activity.EditReminderActivity;
import app.dailyexpensemanager.activity.PaymentTransactionActivity;
import app.dailyexpensemanager.db.Transactiondata;


public class DisplayAllDataAdapter extends BaseAdapter {

    private Context mContext;
    private static Cursor cursor;
    private static Activity activity;
    private static Boolean check_btn;
    private ArrayList<String> category_list;
    private ArrayList<String> date_list;
    private ArrayList<String> id_list ;
    private ArrayList<String> price_list ;
    private ArrayList<String> payment_mode_list ;
    private ArrayList<String> description_list ;

    public DisplayAllDataAdapter(Context cv, Activity activity, Boolean check_btn, ArrayList<String> id, ArrayList<String> category, ArrayList<String> date,
                                 ArrayList<String> price, ArrayList<String> payment_mode, ArrayList<String> description) {

        this.check_btn = check_btn;
        this.id_list = id;
        this.category_list = category;
        this.date_list = date;
        this.price_list = price;
        this.payment_mode_list = payment_mode;
        this.description_list = description;
        this.mContext = cv;
        this.activity = activity;
    }

    public DisplayAllDataAdapter(Context cv, Activity activity,Cursor cursor , Boolean check_btn, ArrayList<String> id, ArrayList<String> category, ArrayList<String> date,
                                 ArrayList<String> price, ArrayList<String> payment_mode, ArrayList<String> description) {

        this.check_btn = check_btn;
        this.id_list = id;
        this.category_list = category;
        this.date_list = date;
        this.price_list = price;
        this.payment_mode_list = payment_mode;
        this.description_list = description;
        this.mContext = cv;
        this.activity = activity;
        this.cursor = cursor;    
    }

    @Override
    public int getCount() {
        return date_list.size();
    }

    @Override
    public Object getItem(int position) {
        return date_list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View child, ViewGroup parent) {

            final Holder mHolder;
            LayoutInflater layoutInflater = null;
            if (child == null) {
                layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                child = layoutInflater.inflate(R.layout.activity_list_all_transaction, null);
                mHolder = new Holder();
                mHolder.txt_category = (TextView) child.findViewById(R.id.category);
                mHolder.txt_date = (TextView) child.findViewById(R.id.date);
                mHolder.txt_id = (TextView) child.findViewById(R.id.id);
                mHolder.txt_price = (TextView) child.findViewById(R.id.price);
                mHolder.txt_payment = (TextView) child.findViewById(R.id.payment);
                mHolder.txt_description = (TextView) child.findViewById(R.id.description);

                child.setTag(mHolder);   
            }
            else {   
                mHolder = (Holder) child.getTag();
            }

            mHolder.txt_category.setText(category_list.get(position));
            mHolder.txt_date.setText(date_list.get(position));
            mHolder.txt_id.setText(id_list.get(position));
            mHolder.txt_price.setText(price_list.get(position));
            mHolder.txt_payment.setText(payment_mode_list.get(position));
            mHolder.txt_description.setText(description_list.get(position));

            child.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    v.setBackgroundResource(R.drawable.item_pressed);
                    /*Animation animation1 = new AlphaAnimation(0.3f, 1.0f);
                    animation1.setDuration(2000);
                    v.startAnimation(animation1);*/

                    String price_lv = mHolder.txt_price.getText().toString();
                    String id_lv = mHolder.txt_id.getText().toString();
                    String category_lv = mHolder.txt_category.getText().toString();
                    String payment_lv = mHolder.txt_payment.getText().toString();
                    String date_lv = mHolder.txt_date.getText().toString();
                    String description_lv = mHolder.txt_description.getText().toString();

                    showCustomeDialogBox(mContext, false, price_lv, id_lv, category_lv, payment_lv,
                            date_lv, description_lv);
            //        notifyDataSetChanged();

                }
            }); 
        return child;    
    }

    public class Holder {
        TextView txt_category;
        TextView txt_price;
        TextView txt_id;
        TextView txt_date;
        TextView txt_payment;
        TextView txt_description;
    }

    public static void showCustomeDialogBox(final Context context, final boolean activityVisibility,String price,
                                  final String id , String category , String payment , String date , String description ) {

        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);
        dialog.setContentView(R.layout.activity_custom_dailog);
        Button positivebtn = (Button) dialog.findViewById(R.id.positive);
        Button negativebtn = (Button) dialog.findViewById(R.id.negative);
        LinearLayout category_lay = (LinearLayout) dialog.findViewById(R.id.category_lay);
        TextView dialog_title = (TextView) dialog.findViewById(R.id.dialog_title);
        TextView price_tv = (TextView) dialog.findViewById(R.id.price);
        TextView category_tv = (TextView) dialog.findViewById(R.id.category);
        TextView payment_tv = (TextView) dialog.findViewById(R.id.payment_mode);
        TextView date_tv = (TextView) dialog.findViewById(R.id.date);
        TextView description_tv = (TextView) dialog.findViewById(R.id.description);

        category_lay.setVisibility(View.GONE);
        dialog_title.setText(category);
        price_tv.setText(price);
        category_tv.setText(category);
        payment_tv.setText(payment);
        date_tv.setText(date);
        description_tv.setText(description);

        positivebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!check_btn) {
                    Transactiondata delete = new Transactiondata(context);
                    delete.open();
                    delete.deleteIncomeEntry(id);
                    this.notifyDataSetChanged();
                    delete.close();
                    /*recreate all transaction Activity .... to refresh data ..........*/
                    activity.recreate();
                    dialog.dismiss();

                } else {
                    Transactiondata delete = new Transactiondata(context);
                    delete.open();
                    delete.deleteExpenseEntry(id);
                    this.notifyDataSetChanged();
                    delete.close();
                    /*recreate all transaction Activity .... to refresh data ..........*/
                    activity.recreate();
                    dialog.dismiss();
                }

            }
        });

        negativebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss(); 
            }
        });


        dialog.show();
        Window window = dialog.getWindow();
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);   
    }   
}

In dialog box code I have done it with recreate activity but I think it is not good solution. 在对话框代码中,我已经完成了重新创建活动,但我认为这不是一个好的解决方案。 Now how to delete and refresh same time listview data only? 现在如何删除和刷新同一时间列表视图数据? hlep me :( hlep me :(

Try this, 尝试这个,

      positivebtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (!check_btn) {
                Transactiondata delete = new Transactiondata(context);
                delete.open();
                delete.deleteIncomeEntry(id);
                notifyDataSetChanged();
                delete.close();
                /*recreate all transaction Activity .... to refresh data ..........*/
                activity.recreate();
                dialog.dismiss();

            } else {
                Transactiondata delete = new Transactiondata(context);
                delete.open();
                delete.deleteExpenseEntry(id);
                notifyDataSetChanged();
                delete.close();
                /*recreate all transaction Activity .... to refresh data ..........*/
                activity.recreate();
                dialog.dismiss();
            }

        }
    });

Try to Write mContext.notifyDataSetChanged(); 尝试Write mContext.notifyDataSetChanged(); .

After this. 在这之后。

delete.deleteIncomeEntry(id);

mContext.notifyDataSetChanged();

EDIT 1: 编辑1:

Just do this. 就这样做吧。

this.notifyDataSetChanged();

EDIT 2: 编辑2:

Look your data is deleted from database that is sure. 查看您的数据是否已从数据库中删除。 so in your Base Adapter to refresh your List View you need to again call the method which is getting data from database. 因此,在您的Base Adapter中刷新List View您需要再次调用从数据库获取数据的方法。 and then do notifydatasetchanged() .I hope you will understand. 然后做notifydatasetchanged() 。我希望你能理解。

try this code 试试这段代码

adapter=new CustomListAdapter(getActivity(),List);
    adapter.notifyDataSetChanged();

    lv.setAdapter(adapter);

And Also add notifyDataSetChanged(); 并且还添加notifyDataSetChanged(); in GetView method below Where you set value. 在下面的GetView方法中设置值的位置。

Check edited first method, just change your showDialog method by below nothing else. 检查编辑后的第一个方法,只需更改showdialog方法。

One more thing pass position in your method at last as like below. 最后还有一件事在你的方法中传递位置,如下所示。

     showCustomeDialogBox(mContext, false, price_lv, id_lv, category_lv, payment_lv,
                        date_lv, description_lv, position);

See new DialogBox method below, 请参阅下面的新DialogBox方法,

 public static void showCustomeDialogBox(final Context context, final boolean activityVisibility,String price,
                              final String id , String category , String payment , String date , String description, int position ) {

    final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(true);
    dialog.setContentView(R.layout.activity_custom_dailog);
    Button positivebtn = (Button) dialog.findViewById(R.id.positive);
    Button negativebtn = (Button) dialog.findViewById(R.id.negative);
    LinearLayout category_lay = (LinearLayout) dialog.findViewById(R.id.category_lay);
    TextView dialog_title = (TextView) dialog.findViewById(R.id.dialog_title);
    TextView price_tv = (TextView) dialog.findViewById(R.id.price);
    TextView category_tv = (TextView) dialog.findViewById(R.id.category);
    TextView payment_tv = (TextView) dialog.findViewById(R.id.payment_mode);
    TextView date_tv = (TextView) dialog.findViewById(R.id.date);
    TextView description_tv = (TextView) dialog.findViewById(R.id.description);

    category_lay.setVisibility(View.GONE);
    dialog_title.setText(category);
    price_tv.setText(price);
    category_tv.setText(category);
    payment_tv.setText(payment);
    date_tv.setText(date);
    description_tv.setText(description);

    positivebtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (!check_btn) {
                Transactiondata delete = new Transactiondata(context);
                delete.open();
                delete.deleteIncomeEntry(id);
                this.notifyDataSetChanged();
                delete.close();
                /*recreate all transaction Activity .... to refresh data ..........*/
                //activity.recreate();
                //dialog.dismiss();

            } else {
                Transactiondata delete = new Transactiondata(context);
                delete.open();
                delete.deleteExpenseEntry(id);
                this.notifyDataSetChanged();
                delete.close();
                /*recreate all transaction Activity .... to refresh data ..........*/
                //activity.recreate();
                //dialog.dismiss();
            }

          id_list.remove(position);
          category_list.remove(position);
          date_list.remove(position);
          price_list.remove(position);
          payment_mode_list.remove(position);
          description_list.remove(position);

          dialog.dismiss();
          notifyDataSetChanged();

        }
    });

    negativebtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            dialog.dismiss(); 
        }
    });


    dialog.show();
    Window window = dialog.getWindow();
    window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);   
}   

Try Below Code It will help you, 尝试以下代码它会帮助你,

First comment your dialog box method, and write below method to use. 首先注释你的对话框方法,并写下以下方法使用。

   public void deleteData(int position)
   {
      String id = id_list.get(position);
      if (!check_btn) {
                Transactiondata delete = new Transactiondata(context);
                delete.open();
                delete.deleteIncomeEntry(id);
                delete.close();
            } 
            else 
            {
                Transactiondata delete = new Transactiondata(context);
                delete.open();
                delete.deleteExpenseEntry(id);
                delete.close();
           }

          id_list.remove(position);
          category_list.remove(position);
          date_list.remove(position);
          price_list.remove(position);
          payment_mode_list.remove(position);
          description_list.remove(position);

          notifyDataSetChanged();
   }

Now, replace your onCLick method by below. 现在,通过下面替换onCLick方法。

  child.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            v.setBackgroundResource(R.drawable.item_pressed);

            deleteData(position);
        }
   });       

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM