ListView 上的复选框 android
checkBox on ListView android
我有这个简单的列表视图,我想为每个复选框设置不同的侦听器。
在 OnItemClick 函数上我有 toast 消息但我根本看不到它(如果我从布局中删除复选框 xml 我可以看到 toast...)
我的代码:
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 布局中将 CheckBox 设置为 focusable="false"。
我有这个简单的列表视图,我想为每个复选框设置不同的侦听器。
在 OnItemClick 函数上我有 toast 消息但我根本看不到它(如果我从布局中删除复选框 xml 我可以看到 toast...)
我的代码:
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 布局中将 CheckBox 设置为 focusable="false"。