开发者

Unable to upate ListView data

开发者 https://www.devze.com 2023-01-31 07:13 出处:网络
I have a class A which extends Activity. From the class A ,I am calling Adapter class B which extends BaseAdapter.

I have a class A which extends Activity. From the class A ,I am calling Adapter class B which extends BaseAdapter.

I am displaying ListView data from within the overriden method public View getView(final int position, View convertView, ViewGroup parent)

I am fetching the data for the ListView from SQLite. Now on clicking on a cancel button for a specific listitem , I am able to delete the record the data from the database , but unable to refresh the view . Still the data is being displayed . But if again I navigate to the screen from the home screen , the data is not being displayed , indicating there is no problem with the database , only updating display is a issue .

The skeletal code is as follows:

class A extends Activity {

    B adapter ;
    ListView list ;

    public void onCreate(Bundle savedInstanceState) {

        list = (ListView) findViewById(R.id.textList);
        // I am calling the adapter

        adapter = new B(this, txtStr);
        list.setAdapter(adapter);
    }

    class B extends BaseAdapter{
        public View getView(final int position, View convertView, ViewGroup parent){

            // on click of the delete button , record is delated for a specific
            // listitem & the view needs to be updated , but I am unable to update the view

     开发者_如何学Go       holder.deleteImage.setOnClickListener(new OnClickListener() {
                    private int pos = position;
                    public void onClick(View v) {// (position)) -- what was this doing here? (dbreslau)

                        sampleDB.execSQL("DELETE FROM " + Constants.TABLE_NAME + " where id = 1");
                        adapter.notifyDataSetChanged();
                    }
                });
        }
    }
}

Even after calling the notifyDataSetChanged , the view is not getting updated. Kindly help if anyone already implemented the same.


Declare your onClickListener out of your adapter, in the activity class, since you're creating new listener for each view constructed, then in the onClickListener onClick method just do this:

    class A extends Activity {

                B adapter ;
                ListView list ;

                public void onCreate(Bundle savedInstanceState) {

                    list = (ListView) findViewById(R.id.textList);
                    // I am calling the adapter

                    adapter = new B(this, txtStr);
                    list.setAdapter(adapter);

                    list.setOnItemClickListener (listener);
                }


private OnItemClickListener listener = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
         // TODO Auto-generated method stub
         sampleDB.execSQL("DELETE FROM " + Constants.TABLE_NAME + " where id = 1");
         //adapter.notifyDataSetChanged();
         ((B)adapter).notifyDataSetChanged();
    }
};

    **EDIT 1** 
    //with an onCLickListener...
    private OnClickListener deleteListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int)v.getTag();
                            sampleDB.execSQL("DELETE FROM " + Constants.TABLE_NAME + " where id = 1");
                            ((B)adapter).notifyDataSetChanged());
            }
        };


                class B extends BaseAdapter{
                    public View getView(final int position, View convertView, ViewGroup parent){

                        // on click of the delete button , record is delated for a specific
                        // listitem & the view needs to be updated , but I am unable to update the view

    holder.deleteImage.setTag(position);                   
    holder.deleteImage.setOnClickListener(deleteListener);
    //...
    convertView.setTag(holder);
                    }
                }
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消