自定义布局的ListActivity不调用getview
我有一个带有自定义行布局的 ListActivity。行布局由复选框、文本视图和图像按钮组成。
其背后是一个自定义对象的 ArrayList,仅包含一个字符串和一个布尔值。我想要复选框来反映布尔值,文本视图来反映 Sting,右侧的按钮将启动一个操作。
我使用 ArrayAdapter 页面上列出的最后一个构造函数来尝试将它们绑定在一起。 ArrayAdapter(Context context, int resources, int textViewResourceId, List
由于某种原因,textView 是由对象 toString() 填充的,而不是由 getView 填充的。因此,复选框根本不会获取布尔值。有什么想法吗?
public class ListSelection extends ListActivity {
private ArrayList<ListItem> list;
private ArrayList<String> deleted;
static private final String TAG = "kinder";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "ListSelection.onCreate");
list = new ArrayList<ListItem>();
deleted = new ArrayList<String>();
Intent temp = getIntent();
String[] listNames = temp.getStringArrayExtra("listNames");
boolean[] listStatus = temp.getBooleanArrayExtra("listStatus");
for (int i = 0; i < listNames.length; i++) {
list.add(new ListItem(listNames[i], listStatus[i]));
}
**setListAdapter(new ArrayAdapter<ListItem>(this, R.layout.selectionlistitem, R.id.text, list));**
}
private class ListItem {
String listName;
Boolean listStatus;
public ListItem(String name, boolean status) {
listName = name;
listStatus = status;
}
@Override
public String toString() {
return listName;
}
}
public class ListItemArrayAdapter extends ArrayAdapter<ListItem> {
int resource;
LayoutInflater vi;
private List<ListItem> items;
public ListItemArrayAdapter(Context context, int _resource, List<ListItem> listitems) {
super(context, _resource, listitems);
Log.d(TAG, "listItemArrayAdapter");
vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
resource = _resource;
this.items = listitems;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(TAG, "listItemArrayAdapter.getView");
LinearLayout newView;
ListItem item = getItem(position);
if (convertView == null) {
newView = new LinearLayout(getContext());
vi.inflate(resource, newView);
} else {
newView = (LinearLayout)convertView;
}
TextView textview = (TextView)newView.findViewById(R.id.text);
Log.d(TAG, item.listName);
textview.setText(item.listName);
CheckBox checkbox = (CheckBox)newView.findViewById(R.id.checkbox);
checkbox.setChecked(item.listStatus);
return newView;
}
}
}
I have a ListActivity with a custom row layout. The row layout consists of a checkbox, a textview and an imagebutton.
Behind this is an ArrayList of a custom object containing simply a String and a boolean. I want to checkbox to reflect the boolean, the textview to reflect the Sting, and the button on the right will kick off an action.
I use the last constructor listed on the ArrayAdapter page to try to bind this all together. ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)
For some reason the textView is populated by the objects toString(), and not by getView. Therefore the checkbox never gets the boolean value at all. Any ideas?
public class ListSelection extends ListActivity {
private ArrayList<ListItem> list;
private ArrayList<String> deleted;
static private final String TAG = "kinder";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "ListSelection.onCreate");
list = new ArrayList<ListItem>();
deleted = new ArrayList<String>();
Intent temp = getIntent();
String[] listNames = temp.getStringArrayExtra("listNames");
boolean[] listStatus = temp.getBooleanArrayExtra("listStatus");
for (int i = 0; i < listNames.length; i++) {
list.add(new ListItem(listNames[i], listStatus[i]));
}
**setListAdapter(new ArrayAdapter<ListItem>(this, R.layout.selectionlistitem, R.id.text, list));**
}
private class ListItem {
String listName;
Boolean listStatus;
public ListItem(String name, boolean status) {
listName = name;
listStatus = status;
}
@Override
public String toString() {
return listName;
}
}
public class ListItemArrayAdapter extends ArrayAdapter<ListItem> {
int resource;
LayoutInflater vi;
private List<ListItem> items;
public ListItemArrayAdapter(Context context, int _resource, List<ListItem> listitems) {
super(context, _resource, listitems);
Log.d(TAG, "listItemArrayAdapter");
vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
resource = _resource;
this.items = listitems;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(TAG, "listItemArrayAdapter.getView");
LinearLayout newView;
ListItem item = getItem(position);
if (convertView == null) {
newView = new LinearLayout(getContext());
vi.inflate(resource, newView);
} else {
newView = (LinearLayout)convertView;
}
TextView textview = (TextView)newView.findViewById(R.id.text);
Log.d(TAG, item.listName);
textview.setText(item.listName);
CheckBox checkbox = (CheckBox)newView.findViewById(R.id.checkbox);
checkbox.setChecked(item.listStatus);
return newView;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,您应该将此行: 替换
为:
就目前而言,您正在使用 ArrayAdapter 的默认视图生成逻辑,即提供 toString() 对于每个数组元素到
TextView
。It seems to me that you should replace this line:
with this:
As it stands, you are using the default view generation logic of
ArrayAdapter
, which is to feedtoString()
for each array element to aTextView
.