ListView android上的复选框
Posted
技术标签:
【中文标题】ListView android上的复选框【英文标题】:checkBox on ListView android 【发布时间】:2015-06-07 22:02:43 【问题描述】:我有这个 simpleList 视图,我想为每个复选框创建不同的侦听器。
在 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。 试试onItemSelected
而不是onItemClick
这是我的完整代码:link onItemSelected 不能解决我的问题。
【参考方案1】:
在 XML 布局中将 CheckBox 设置为 focusable="false"。
【讨论】:
以上是关于ListView android上的复选框的主要内容,如果未能解决你的问题,请参考以下文章
带有复选框和所有可点击的Android ListView [重复]