我必须在点击每个项目后更改列表视图中的数据任何正文都可以告诉我如何在列表视图中单击该项目后通知更改。我没有使用基础适配器。我使用的是自定义Adapter.Any帮助将非常值得注意。 检查我的代码。我正在尝试在用户点击的位置添加按钮。这是可能的。当我尝试点击位置它给力关闭。以下是我的代码。
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
}
}
答案 0 :(得分:4)
在你的onItemClick()方法中
ListAdapter uradapteobjr=listview.getAdapter();
uradapterobj.notifyDataSetChanged();
答案 1 :(得分:2)
使用notifydatasetchanged();
参考此文档:
http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged%28%29
答案 2 :(得分:0)
您在正在使用的适配器上调用notifyDataSetChanged()。使适配器成为类的一个字段,以便它在整个类方法的范围内。然后从onItemClickedListener处理程序中调用它:
listView.setOnItemClickListener( new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// do stuff ....
myAdapter.notifyDataSetChanged();
}
});
答案 3 :(得分:0)
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();
}
});