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

时间:2016-05-23 07:36:17

标签: java android android-sqlite

所有交易活动用于从数据库中获取数据

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);
    }
}

自定义适配器类,用于将数据与列表视图绑定

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);   
    }   
}

在对话框代码中,我已经完成了重新创建活动,但我认为这不是一个好的解决方案。现在如何删除和刷新同一时间列表视图数据?发牢骚我:(

4 个答案:

答案 0 :(得分:0)

试试这个,

      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();
            }

        }
    });

答案 1 :(得分:0)

  

尝试Write mContext.notifyDataSetChanged();

在此之后。

delete.deleteIncomeEntry(id);

mContext.notifyDataSetChanged();

编辑1:

就这样做。

this.notifyDataSetChanged();

编辑2:

  

查看您的数据是否已从数据库中删除。因此,在Base Adapter刷新List View时,您需要再次调用从数据库获取数据的方法。然后做notifydatasetchanged()。我希望你能理解。

答案 2 :(得分:0)

试用此代码

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

    lv.setAdapter(adapter);

并且还添加notifyDataSetChanged();在下面的GetView方法中设置值。

答案 3 :(得分:0)

检查编辑后的第一种方法,只需更改你的showDialog方法。

最后在你的方法中再次传递位置,如下所示。

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

请参阅下面的新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);   
}   

尝试以下代码它会对您有帮助,

首先注释你的对话框方法,并写下以下方法使用。

   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();
   }

现在,请通过以下方式替换onCLick方法。

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

            v.setBackgroundResource(R.drawable.item_pressed);

            deleteData(position);
        }
   });