Putting LinearLayout Inside Of ListView (Android)
I'm trying to programmatically put a LinearLayout inside of a ListView and am having some trouble. I want to do this so that I can insert horizontal columns inside each ListView cell. Here's the relevant snippet of code:
LinearLayout listArray[] = new LinearLayout[routeTable.GetTableHeight()];
for (int i = 0; i < routeTable.GetTableHeight(); i++) {
listArray[i] = new LinearLayout(this);
TextView view = new TextView(this);
view.setText(strList[i]);
listArray[i].addView(view);
}
// Create list
ListView list = new ListView(this);
list.setAdapter(new ArrayAdapter<LinearLayout>(this,
android.R.lay开发者_JAVA百科out.simple_list_item_1, listArray));
layout.addView(list, new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
This compiles, but the resulting list that is displayed does not display any of the strings in strList[]. Instead, the list elements that are displayed appear something like "android.widget.LinearLayout@47b63c0". Does anybody know how I could fix this so that the appropriate TextViews that are inside of listArray[] would be displayed?
ArrayAdapter just calls .toString() method on each object in the list and displays the resulting string in each list item. In your case, .toString() is called on LinearLayout, that is what you see as list element. It is also not clear what are the elements of routeTable.
You should subclass BaseAdapter so that you can construct necessary View for each list item yourself.
精彩评论