我在android中的listview的每一行都有复选框和textview。 如果我单击复选框,我会设置我的复选框,但如果单击TextView,如何设置复选框。 这是我的代码示例。 谢谢你的时间。
public class PitanjaAdapter extends ArrayAdapter<Odgovor>{
private ArrayList<Odgovor> odgovorList;
Context context;
private Typeface custom_font;
public PitanjaAdapter(Context context, int textViewResourceId, ArrayList<Odgovor> odgovorList) {
super(context, textViewResourceId, odgovorList);
this.context = context;
this.odgovorList = new ArrayList<Odgovor>();
this.odgovorList.addAll(odgovorList);
this.custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/Aller.ttf");
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null){
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.pitanja_entry, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.code.setTypeface(custom_font);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.name.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Odgovor country = (Odgovor) cb.getTag();
country.setSelected(cb.isChecked());
}
});
holder.code.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
Odgovor country = odgovorList.get(position);
holder.code.setText(country.getContents());
holder.name.setChecked(country.isSelected());
holder.name.setTag(country);
if (odgovorList.get(position).getGreen() != 0 && odgovorList.get(position).getRed() != 0){
if (country.isSelected() && Integer.parseInt(country.getIs_correct()) == 1){
holder.code.setBackgroundResource(R.drawable.tocan_odgovor_drawable);
holder.name.setBackgroundResource(R.drawable.ikona_box2_tocno);
} else if (country.isSelected() && Integer.parseInt(country.getIs_correct()) == 0){
holder.code.setBackgroundResource(R.drawable.netocan_odgovor_drawable);
holder.name.setBackgroundResource(R.drawable.ikona_box2_netocno);
} else if (!country.isSelected() && Integer.parseInt(country.getIs_correct()) == 0){
holder.code.setBackgroundResource(R.drawable.netocan_odgovor_drawable);
holder.name.setBackgroundResource(R.drawable.selector_check);
holder.name.setEnabled(false);
} else if (!country.isSelected() && Integer.parseInt(country.getIs_correct()) == 1){
holder.code.setBackgroundResource(R.drawable.tocan_odgovor_drawable);
holder.name.setBackgroundResource(R.drawable.selector_check);
holder.name.setEnabled(false);
}
}
return convertView;
}
public void colorized(Odgovor odgovor){
odgovor.setRed(Color.RED);
odgovor.setGreen(Color.WHITE);
}
private class ViewHolder{
public TextView code;
public CheckBox name;
}
}
答案 0 :(得分:1)
我把它放在这里因为我无法评论:(。 Hace你试过
checkbox.setChecked(myboolean)
修改强>
你应该把它拿出来
public class PitanjaAdapter extends ArrayAdapter<Odgovor>{
public ViewHolder holder;
然后
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.pitanja_entry, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.code.setTypeface(custom_font);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.name.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Odgovor country = (Odgovor) cb.getTag();
country.setSelected(cb.isChecked());
}
这样你在getView中给它一个值,但你可以从onClick访问它,因为它是一个外部值。但是在使用它之前总是确保给它一个值,否则它将为null
要将textview用作buttom,你必须使用onclicklistener
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text= (TextView) findViewById(R.id.textView);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox A = (CheckBox) findViewById(R.id.checkBox2);
A.setChecked(!A.isChecked());
}
});
}