单击每个项目后,我必须更改 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();
我是一名优秀的程序员,十分优秀!