How to Perform Update and Delete operation in listview item row while Click the button in CursorAdapter

早过忘川 提交于 2019-12-01 21:07:02

Finally I done it on my own.But instead of using Cursor Adapter I am using Base Adapter to get the output.

I referred this Tutorial.I took more time to find this tutorial.It helped me a lot.Then I change some modification for the front page and for my requirement.

I change my front page like this.Below I am showing the changes in MainActivity for that:

MainActivity.java:

public class MainActivity extends Activity implements OnClickListener {

    private Button btn_add;
    ActionBar actionBar;

    private ListView listview;

    DatabaseHelper db;
    public ArrayList<ProductModel> _productlist = new ArrayList<ProductModel>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn_add = (Button) findViewById(R.id.btn_add);

        btn_add.setOnClickListener(this);

        listview = (ListView) findViewById(R.id.listview);

        actionBar = getActionBar();
        ColorDrawable colorDrawable = new ColorDrawable(
                Color.parseColor("#000000"));
        actionBar.setBackgroundDrawable(colorDrawable);

    }  

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        _productlist.clear();

        db = new DatabaseHelper(getApplicationContext());
        db.getWritableDatabase();
        ArrayList<ProductModel> product_list = db.getProudcts();

        for (int i = 0; i < product_list.size(); i++) {

            String tidno = product_list.get(i).getIdno();

            System.out.println("tidno>>>>>" + tidno);
            String tname = product_list.get(i).getProductname();
            String tprice = product_list.get(i).getProductprice();

            ProductModel _ProductModel = new ProductModel();

            _ProductModel.setIdno(tidno);
            _ProductModel.setProductname(tname);
            _ProductModel.setProductprice(tprice);

            _productlist.add(_ProductModel);
        }

        listview.setAdapter(new ListAdapter(this));
        db.close();

    }

    private class ListAdapter extends BaseAdapter {
        LayoutInflater inflater;
        ViewHolder viewHolder;

        public ListAdapter(Context context) {
            // TODO Auto-generated constructor stub
            inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return _productlist.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            if (convertView == null) {

                convertView = inflater.inflate(R.layout.listview_row, null);
                viewHolder = new ViewHolder();

                viewHolder.txt_pname = (TextView) convertView
                        .findViewById(R.id.txtdisplaypname);
                viewHolder.txt_pprice = (TextView) convertView
                        .findViewById(R.id.txtdisplaypprice);

                viewHolder.txtidno = (TextView) convertView
                        .findViewById(R.id.txtdisplaypid);
                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.txt_pname.setText(_productlist.get(position)
                    .getProductname().trim());
            viewHolder.txt_pprice.setText(_productlist.get(position)
                    .getProductprice().trim());

            viewHolder.txtidno.setText(_productlist.get(position).getIdno()
                    .trim());

            final int temp = position;
            (convertView.findViewById(R.id.btn_update))
                    .setOnClickListener(new OnClickListener() {

                        public void onClick(View arg0) {

                            String _productid = String.valueOf(_productlist
                                    .get(temp).getIdno());
                            String _productname = _productlist.get(temp)
                                    .getProductname();
                            String _productprice = _productlist.get(temp)
                                    .getProductprice();

                            Intent intent = new Intent(MainActivity.this,
                                    AddUpdateValues.class);

                            Bundle bundle = new Bundle();
                            bundle.putString("id", _productid);
                            bundle.putString("name", _productname);
                            bundle.putString("price", _productprice);
                            intent.putExtras(bundle);
                            startActivity(intent);

                        }
                    });

            (convertView.findViewById(R.id.btn_delete))
                    .setOnClickListener(new OnClickListener() {

                        public void onClick(View arg0) {

                            AlertDialog.Builder alertbox = new AlertDialog.Builder(
                                    MainActivity.this);
                            alertbox.setCancelable(true);
                            alertbox.setMessage("Are you sure you want to delete ?");
                            alertbox.setPositiveButton("Yes",
                                    new DialogInterface.OnClickListener() {

                                        public void onClick(
                                                DialogInterface arg0, int arg1) {

                                            Log.i(">>>TEMP>>>", temp + "");
                                            Log.i(">>>getIdno>>>>>>",
                                                    _productlist.get(temp)
                                                            .getIdno().trim()
                                                            + "");
                                            System.out
                                                    .println(">>>getIdno>>>>>>"
                                                            + _productlist
                                                                    .get(temp)
                                                                    .getIdno()
                                                                    .trim());
                                            db.removeProduct(
                                                    _productlist.get(temp)
                                                            .getIdno().trim(),
                                                    "", "");

                                            MainActivity.this.onResume();

                                            Toast.makeText(
                                                    getApplicationContext(),
                                                    "Project Deleted...",
                                                    Toast.LENGTH_SHORT).show();

                                        }  

                                    });
                            alertbox.setNegativeButton("No",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(
                                                DialogInterface arg0, int arg1) {

                                        }
                                    });
                            alertbox.show();
                        }
                    });
            return convertView;

        }
    }

    private class ViewHolder {
        TextView txt_pname;
        TextView txt_pprice;
        TextView txtidno;

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btn_add:
            Intent addintent = new Intent(MainActivity.this, AddRecord.class);
            startActivity(addintent);
            break;

        default:
            break;
        }

    }

}

I Hope this answer may be helpful to someone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!