开发者

How to notifyDataSetChange() from onitemclick() method?

I have to change the the data in list view after clicking on each item Can any body please tell me how to notify the change after the item is clicked in list view. I am not using base adapter .I am using customized Adapter.Any help will be highly Appreciable. check my code.I am trying to add button on which position the user clicks.Is it possible .When I try click on the position It gives force close.following is my code.

public class CamNe extends Activity implements OnItemClickListener,OnItemSelectedListener{
    /** Called when the activity is first created. */
String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
            "consectetuer", "adipiscing", "elit", "morbi", "vel",
            "ligula", "vitae", "arcu", "aliquet", "mollis",
            "etiam", "vel", "erat", "placerat", "ante",
            "porttitor", "sodales", "pellentesque", "augue",
            "purus"};
int pos=0;
ListView ls;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



        ListView ls=(ListView)findViewById(R.id.list);
        //ls1=(ListView)findViewById(R.id.list);

        ls.setAdapter(new adapter(getApplicationContext(), items));
        ls.setOnItemClickListener(this);


    }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        pos=arg2;

        setTitle("mil");
        ListAdapter ad=ls.getAdapter();
        ((ArrayAdapter) ad).notifyDataSetChanged();



    }
    class adapter extends ArrayAdapter{

        private Context context;

        public adapter(Context context,String[] item开发者_C百科s) {
            super(context,R.layout.row,items);
            this.context=context;
            // TODO Auto-generated constructor stub
        }
        public View getView(int position, View convertView,
                ViewGroup parent) {
            View row=convertView;
            ViewWrapper wrapper;
            Button but = null;
            if (row==null) {
                LayoutInflater inflater = (LayoutInflater)context.getSystemService
                (Context.LAYOUT_INFLATER_SERVICE);


                row=inflater.inflate(R.layout.row, null);
                wrapper=new ViewWrapper(row);
                row.setTag(wrapper);

                but=wrapper.getRatingBar();
                but.setVisibility(4);
            if(position==pos){
                but.setVisibility(0);
            }

                }
            else {
                wrapper=(ViewWrapper)row.getTag();
            if(position==pos)
                but=wrapper.getRatingBar();
            }
            wrapper.getLabel().setText(items[position]);
            if(position==pos){
            but.setTag(new Integer(pos));
            }
                return row;
        }

    }
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        setTitle("mil");
        pos=arg2;
        ((BaseAdapter) ls.getAdapter()).notifyDataSetChanged();



    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }


}  


in your onItemClick() methodlike this

ListAdapter uradapteobjr=listview.getAdapter();

uradapterobj.notifyDataSetChanged();


use notifydatasetchanged();

refer this doc:

http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged%28%29


You call notifyDataSetChanged() on the Adapter you're using. Make the Adapter a field of the class so that it is in scope throughout your class methods. Then just call it from within your onItemClickedListener handler:

listView.setOnItemClickListener( new OnItemClickListener() {            
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id)
        {
            // do stuff ....
            myAdapter.notifyDataSetChanged();

        }
    });


    final ListView listView = new ListView(this); // Create your listview
    listView.setAdapter(new BaseAdapter()); // Set your adapter
    listView.setOnItemClickListener(new OnItemClickListener() {            

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            listView.getAdapter().notifyDataSetChanged();
        }
    });
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜