gpt4 book ai didi

android - 如何从 onitemclick() 方法通知 DataSetChanged()?

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:23 26 4
gpt4 key购买 nike

单击每个项目后,我必须更改 ListView 中的数据 任何人都可以告诉我如何在 ListView 中单击项目后通知更改。我没有使用基本适配器。我使用的是自定义适配器。任何帮助都将非常有用。检查我的代码。我正在尝试在用户点击的位置添加按钮。是否可能。当我尝试点击该位置时它会强制关闭。以下是我的代码。

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() 方法中像这样

ListAdapter uradapteobjr=listview.getAdapter();

uradapterobj.notifyDataSetChanged();

关于android - 如何从 onitemclick() 方法通知 DataSetChanged()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7334833/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com