在我的应用程序中,我有一个由CheckedTextView组成的ListView。 该列表链接到具有int参数的对象集合;如果该参数是== 1,那么我希望已经检查过CheckedTextView。
这是我的代码:
活动(它实际上是一个片段):
ListView lv;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.aller_layout, container, false);
lv = (ListView) rootView.findViewById(R.id.listView1);
CustomAdapter ca = new CustomAdapter(rootView.getContext(), android.R.layout.simple_list_item_multiple_choice, oggetti);
lv.setAdapter(ca);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id){
/*TextView clickedView = (TextView) view;
Toast.makeText(rootView.getContext(), "Item with id ["+id+"] - Position ["+position+"] - Planet ["+clickedView.getText()+"]", Toast.LENGTH_SHORT).show();*/
}
});
return rootView;
}
适配器:
public class CustomAdapter extends ArrayAdapter<Oggetto>{
private Context context;
private Oggetto[] oggetti = null;
private int layoutid;
public CustomAdapter(Context context, int layoutid, Oggetto[] oggetti){
super(context, layoutid, oggetti);
this.context = context;
this.oggetti = oggetti;
this.layoutid = layoutid;
}
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(layoutid, parent, false);
}
Oggetto oggetto = oggetti[position];
CheckedTextView ctv = (CheckedTextView) convertView.findViewById(android.R.id.text1);
ctv.setText(oggetto.nome);
if(oggetto.preferito == 1)
ctv.setChecked(true);
return convertView;
}
}
ListView它显示正确,我可以检查任何选项没有问题,但setChecked(true)方法不起作用。
知道为什么吗?
答案 0 :(得分:1)
CheckedTextView
没有明显显示已经检查过,它只是在内部存储该状态。您应该用CheckBox
替换它。如果您不希望它被点击,只需在XML中添加android:clickable="false"
即可。
答案 1 :(得分:0)
if(oggetto.preferito == 1)
ctv.setChecked(false); --> THIS IS WRONG
你应该
ctv.setChecked(oggetto.preferito == 1);