如何使用BaseAdapter刷新或更新listview?

时间:2014-09-28 13:23:17

标签: android

每当我删除其中的行时,我一直在尝试刷新listview。我已将BaseAdapter用于listview。我没有得到如何更新它。我使用了SQLite,在刷新时我也删除了数据库中的行。

public class MainActivity extends Activity {

    DataBase md ;
    ImageButton ib;
    ListView notelist;
    int i;
    ArrayList<String> titles = new ArrayList<String>();
    ArrayList<String> not = new ArrayList<String>();
    ArrayList<String> date = new ArrayList<String>();
    ArrayList<String> time = new ArrayList<String>();

    ArrayList<String> Revtitles = new ArrayList<String>();
    ArrayList<String> Revnot = new ArrayList<String>();
    ArrayList<String> Revdate = new ArrayList<String>();
    ArrayList<String> Revtime = new ArrayList<String>();


    DataAdapter adapter;

    View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        md = new DataBase(this);

        SQLiteDatabase sd = md.getReadableDatabase();

        Cursor c = sd.query(md.TABLE_NAME, null, null, null, null, null, null);

        while (c.moveToNext()) {

            titles.add(c.getString(0));
            not.add(c.getString(1));
            date.add(c.getString(2));
            time.add(c.getString(3));

        }

        for (int i = titles.size() - 1; i >= 0; i--) {

            Revtitles.add(titles.get(i));
            Revnot.add(not.get(i));
            Revdate.add(date.get(i));
            Revtime.add(time.get(i));

        }

        notelist = (ListView) findViewById(R.id.listView1);
        ib = (ImageButton) findViewById(R.id.imageButton1);

        adapter = new DataAdapter(this);
        notelist.setAdapter(adapter);
        // notelist.setStackFromBottom(true);
        notelist.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                Intent i = new Intent(MainActivity.this,Show.class);

                i.putExtra("TITLE", Revtitles.get(arg2));
                i.putExtra("NOTE", Revnot.get(arg2));

                startActivity(i);
                finish();

            }
        });

        notelist.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View v,
                    final int pos, long arg3) {
                // TODO Auto-generated method stub
                final String st = Revtitles.get(pos);
                String st1 = Revnot.get(pos);

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

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub

                    //sd.delete(md.TABLE_NAME, md.COL_ROLLNO+"=?", new String[]{st});

                    //Intent i = new Intent(MainActivity.this,MainActivity.class);
                    //startActivity(i);

                ///////PROBLEM LINES///////



                        SQLiteDatabase sd = md.getWritableDatabase();

                        sd.delete(md.TABLE_NAME,md.COL_TITLE+"=?", new String[]{st});

                        Revtitles.remove(pos);
                        Revnot.remove(pos);
                        Revdate.remove(pos);
                        Revtime.remove(pos);

                        adapter.notifyDataSetChanged();
                        adapter.notifyDataSetInvalidated();

                        notelist.setAdapter(adapter);

                    }



                });
                ad.setNegativeButton("No", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });

                AlertDialog adl = ad.create();
                adl.show();
                return true;
            }

        });

        ib.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent i = new Intent(MainActivity.this, Add.class);
                startActivity(i);
                finish();
            }
        });

    }

    class DataAdapter extends BaseAdapter{

        private Context con;
        LayoutInflater lat = getLayoutInflater();

        public DataAdapter(MainActivity mainActivity) {
            // TODO Auto-generated constructor stub
            this.con = mainActivity;
        }

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

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

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

        @Override
        public View getView(int position, View convo, ViewGroup arg2) {
            // TODO Auto-generated method stub
            convo = lat.inflate(R.layout.mainlist, null);

            TextView tit = (TextView)convo.findViewById(R.id.inflatetitle);
            TextView datea = (TextView)convo.findViewById(R.id.inflatedate);
            String rtitle;

            int len = Revtitles.get(position).length();
            if (len <= 20){
             rtitle = Revtitles.get(position);
            }
            else {
                rtitle = Revtitles.get(position).substring(0, 20) + "...";
            }
            tit.setText(rtitle);
            datea.setText(Revdate.get(position) + "\n" + Revtime.get(position));

            view = convo;
            return convo;
        }

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

}

1 个答案:

答案 0 :(得分:0)

您使用getView()来调整SQLite中的数据到listView,在您删除listView中的项目后,您应该将listview更新为notelist.setAdapter(adapter);更改其中的数据。

您使用listview将数据绑定到listView

为了刷新您的adapter,您必须通知adapter.notifyDataSetChanged();已发生的事情,并应使用listview class

进行刷新

您可以在adapter classpublic void updateAdapter(your_list arrylst ) { this.arrylst= arrylst; //and call notifyDataSetChanged notifyDataSetChanged(); }

中执行此操作

在适配器类中使用此方法:

getVeiw()

请使用糟糕的{{1}}方法,就像Google I / O 2013所说的那样。

检查this video for more info

希望有所帮助