我正在使用textview和复选框创建自定义列表视图项。每当选中对应于用户的复选框时,我想在祝酒词中看到他的名字。我使用了以下代码:
mUserList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(ManualInviteActivity.this, position, Toast.LENGTH_SHORT).show();
Users1 user = (Users1) parent.getItemAtPosition(position);
if(user.isSelected()==true)
{
Toast.makeText(getBaseContext(), user.getName(), Toast.LENGTH_SHORT).show();
}
}
});
当我切换复选框时,我看不到任何内容。这里有什么错误? 谢谢 (isSelected和getName是我自己在用户类中的函数)
适配器代码:
public class UserListAdapter extends ArrayAdapter<Users1> {
static Context context;
static int layoutResourceId;
Users1 data[] = null;
public UserListAdapter(Context context, int layoutResourceId, Users1[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
public long getItemId(int position) {
return position;
}
static class UserHolder {
TextView txtTitle;
CheckBox cbxUser;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.user_name);
holder.cbxUser = (CheckBox) row.findViewById(R.id.user_checked);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
Users1 weather = data[position];
holder.txtTitle.setText(weather.title);
holder.cbxUser.setChecked(weather.isSelected());
// holder.txtTitle2.setText(weather.title2);
// holder.txtTitle2.setText(weather.title2);
// holder.txtTitle3.setText(weather.title3);
return row;
}
}
Users1类:
public class Users1 {
public String title;
boolean selected = true;
public Users1()
{
super();
}
public Users1(String title,boolean selected) {
super();
this.selected = selected;
this.title = title;
}
public String getName() {
return title;
}
public void setName(String name) {
this.title = name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
答案 0 :(得分:0)
您需要使用onCheckedStateChange作为复选框。
答案 1 :(得分:0)
您需要为RadioGroup设置一个监听器(而不是您的列表)。 像这样:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
OnCheckedChangeListener OCCL = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radio1:
//...
break;
case R.id.radio2:
//...
break;
case R.id.radio3:
//...
break;
default:
break;
}
}
};
radioGroup.setOnCheckedChangeListener(OCCL);
答案 2 :(得分:0)
holder.checkbox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Toast.makeText(getBaseContext(), user.getName(), Toast.LENGTH_SHORT).show();
}
});
答案 3 :(得分:0)
试试这段代码
public class Customadapter extends BaseAdapter {
Context context;
ArrayList<String> names;
LayoutInflater inflater;
public Customadapter(Context context, ArrayList<String> names) {
this.names = names;
this.context = context;
inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return names.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public class Holder {
TextView name;
CheckBox check;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
final Holder holder;
if(convertView==null)
{
holder=new Holder();
convertView=inflater.inflate(R.layout.checkbox,null);
holder.name=(TextView) convertView.findViewById(R.id.textView1);
holder.check=(CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.check.setId(position);
}
else
{
holder=(Holder) convertView.getTag();
}
holder.name.setText(names.get(position));
holder.check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,""+names.get(holder.check.getId()),Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}