如何从onitemclick()方法中通知notifyDataSetChange()? [英] How to notifyDataSetChange() from onitemclick() method?

查看:77
本文介绍了如何从onitemclick()方法中通知notifyDataSetChange()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击每个项目后,我必须在列表视图中更改数据.请问任何机构,请告诉我如何在列表视图中单击该项目后通知更改.我没有使用基础适配器.我正在使用定制的适配器.任何帮助都是高度赞赏的. 检查我的代码.我正在尝试在用户单击的位置上添加按钮.有可能.当我尝试单击该位置时,它会强制关闭.以下是我的代码.

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[] items) {
            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

    }


}  

推荐答案

在您的onItemClick()方法中

in your onItemClick() methodlike this

ListAdapter uradapteobjr=listview.getAdapter();

uradapterobj.notifyDataSetChanged();

这篇关于如何从onitemclick()方法中通知notifyDataSetChange()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆