0
首先,我很抱歉我的坏英语,也许,我的愚蠢问题(我是新手)。 我想实现一个格式化为标题的选项面板。 所以在这里我的代码:Android:自定义列表适配器图像,文本,子文本和复选框
listview_item_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<ImageView
android:id="@+id/imgIcon"
android:contentDescription="option image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txtText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@id/imgIcon"
android:paddingLeft="15dp"
android:textSize="20sp" />
<TextView
android:id="@+id/txtSubText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@id/imgIcon"
android:layout_below="@id/txtText"
android:paddingLeft="15dp"
android:textSize="14sp" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/txtText" />
</RelativeLayout>
Row.java
public class Row {
public int icon;
public String text;
public String subtext;
public Row(){
super();
}
public Row(int icon, String text ,String subtext) {
super();
this.icon = icon;
this.text = text;
this.subtext = subtext;
}
}
RowAdapter.java
public class RowAdapter extends ArrayAdapter<Row>{
Context context;
int layoutResourceId;
Row data[] = null;
public RowAdapter(Context context, int layoutResourceId, Row[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View myRow = convertView;
RowHolder holder = null;
if(myRow == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
myRow = inflater.inflate(layoutResourceId, parent, false);
holder = new RowHolder();
holder.imgIcon = (ImageView)myRow.findViewById(R.id.imgIcon);
holder.txtText = (TextView)myRow.findViewById(R.id.txtText);
holder.txtSubText = (TextView)myRow.findViewById(R.id.txtSubText);
holder.checkBox = (CheckBox)myRow.findViewById(R.id.checkBox);
myRow.setTag(holder);
}
else
{
holder = (RowHolder)myRow.getTag();
}
Row row = data[position];
holder.txtText.setText(row.text);
holder.imgIcon.setImageResource(row.icon);
holder.txtSubText.setText(row.subtext);
return myRow;
}
static class RowHolder
{
ImageView imgIcon;
TextView txtText;
TextView txtSubText;
CheckBox checkBox;
}
}
现在我没有显示任何复选框,当我开始活动,即使我已经膨胀它。我怎样才能解决这个问题?
此外,如果我可以利用这个问题,当我必须实现复选框的onClickListener时,我怎么理解哪个复选框被点击? 我会做这样的事情:
...
Row row = data[position];
holder.txtText.setText(row.text);
holder.imgIcon.setImageResource(row.icon);
holder.txtSubText.setText(row.subtext);
holder.check..setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
// my code here
}
}
});
...
我认为“位置”是关键,但我不知道我该如何使用它。
仍然没有工作,我试图重新编写所有我的代码实现“BaseAdapter”,因为你在代码中建议,但我只是一个只有标题行可见的白色屏幕。我也使用getSystemService(..)代替getLayoutInflater()来修改我的代码(这是我在2个代码之间看到的唯一区别),但结果相同:所有工作正常,但checkBox不会出现。 –
解决,谢谢! –