我有一个由适配器控制的 ListView。该适配器负责动态加载内容并填充 ListView。第一次填充 ListView 时,每当我单击一个项目时,onItemClick
都会调用该方法。但是,一旦我对适配器进行了更改(向下滚动,这会强制适配器加载更多数据并将其添加到 ListView),该onItemClick
方法就不会被调用。
这是相关代码:
public class RingtonesListActivity extends Activity {
private ArrayList<Category> categories;
ListView myListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
this.categories = getIntent().getParcelableArrayListExtra("categories");
myListView = (ListView) findViewById(R.id.listView);
myListView.setAdapter(new MyAdapter(this, categories));
// click in ringtone
myListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int i,
long arg3) {
Intent intent = new Intent(view.getContext(), NewActivity.class);
intent.putExtra("resource", (Resource) myListView.getItemAtPosition(i));
startActivity(intent);
}
});
}
}
在我的适配器中:
public class MyAdapter extends BaseAdapter {
private List<Resources> resources;
...
@Override
public int getCount() {
return resources.size();
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
if (position >= getCount() - 1) {
// do some stuff that adds entries to the resources List
notifyDataSetChanged();
}
TextView tv = new TextView(this.context);
tv.setText(resources.get(position).getName());
return tv;
}