gpt4 book ai didi

android - Android 中 ArrayAdapter 中的 CheckBox 和 TextView

转载 作者:行者123 更新时间:2023-11-30 02:20:38 26 4
gpt4 key购买 nike

我在 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;


}

最佳答案

我把它放在这里是因为我不能评论:(。你试过吗

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 用作按钮,您必须使用 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());
}
});
}

关于android - Android 中 ArrayAdapter 中的 CheckBox 和 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28594004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com