0
我有這個simpleList視圖,我想爲每個複選框做不同的監聽器。ListView上的複選框android
上OnItemClick功能我有面包消息,我沒有看到它在所有的(如果我刪除從佈局XML的複選框,我可以看到麪包......)
我的代碼:
public class MainActivity extends ActionBarActivity {
ListView myList;
List<Student> data;
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.student_list);
data = Model.getInstance().getAllStudents();
myList = (ListView) findViewById(R.id.listView);
adapter = new CustomAdapter();
myList.setAdapter(adapter);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),"item click " + position,Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(),StudentDetailsActivity.class);
intent.putExtra("id",data.get(position).id);
startActivity(intent);
}
});
}
@Override
public void onResume(){
super.onResume();
adapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class CustomAdapter extends BaseAdapter{
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.rows_student_list,null);
}
ImageView image = (ImageView) convertView.findViewById(R.id.imageView);
TextView name = (TextView) convertView.findViewById(R.id.nameTextView);
TextView id = (TextView) convertView.findViewById(R.id.idTextView);
CheckBox checkBox=(CheckBox) convertView.findViewById(R.id.checkBox);
Student st = data.get(position);
name.setText(st.name);
id.setText(st.id);
checkBox.setTag(Integer.valueOf(position));
return convertView;
}
}
}
我該怎麼做?
請張貼您的佈局xml。 – 2015-04-02 21:52:06
嘗試'onItemSelected'而不是'onItemClick' – 2015-04-02 22:18:27
這裏是我的完整代碼:[link](http://pastebin.com/iR3kjDQ8) onItemSelected無法解決我的問題。 – Yossi 2015-04-03 09:05:13