我想创建一个包含listView的Activity,其中的行包含listView。
这是布局结构。
只有我可以在mother_row.xml中看到标题TextView但是,我无法在child_row.xml中显示行TextView。
请检查我的MainActivity.java代码
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private final String TAG = MainActivity.class.getSimpleName();
public class Item {
List<String> ItemList;
String ItemHeader;
Item(String itemHeader, List<String> itemList){
ItemHeader = itemHeader;
ItemList = itemList;
}
}
static class ViewHolder {
TextView motherRowHeader;
ListView motherListView;
}
public class ItemsListAdapter extends BaseAdapter {
private Context context;
private List<Item> list;
ItemsListAdapter(Context c, List<Item> l) {
context = c;
list = l;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
rowView = inflater.inflate(R.layout.mother_row, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.motherRowHeader
= (TextView) rowView.findViewById(R.id.header);
viewHolder.motherListView
= (ListView) rowView.findViewById(R.id.child_listView);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.motherRowHeader.setText( list.get(position).ItemHeader );
// I think you have to inflate child list inside getview of mother
ItemsChildListAdapter ItemAdapter
= new ItemsChildListAdapter( MainActivity.this, list.get(position).ItemList );
holder.motherListView.setAdapter( ItemAdapter );
return rowView;
}
}
List<Item> items;
ListView listView;
ItemsListAdapter myItemsListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.mother_listView);
initItems();
myItemsListAdapter = new ItemsListAdapter(this, items);
listView.setAdapter(myItemsListAdapter);
}
private void initItems() {
items = new ArrayList<Item>();
String header = "aaa";
List<String> list = new ArrayList<String>();
list.add("1111111");
list.add("2222222");
list.add("3333333");
list.add("4444444");
Item item = new Item(header, list);
items.add(item);
list.clear();
header = "bbb";
list.add("1111111");
list.add("2222222");
list.add("3333333");
list.add("4444444");
item = new Item(header, list);
items.add(item);
list.clear();
header = "ccc";
list.add("1111111");
list.add("2222222");
list.add("3333333");
list.add("4444444");
item = new Item(header, list);
items.add(item);
}
static class ChildViewHolder {
TextView childRowTextView;
}
public class ItemsChildListAdapter extends BaseAdapter {
private Context context;
private List<String> list;
ItemsChildListAdapter(Context c, List<String> l) {
context = c;
list = l;
for(String t : list) {
Log.d(TAG, t);
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
rowView = inflater.inflate(R.layout.child_row, null);
ChildViewHolder viewHolder = new ChildViewHolder();
viewHolder.childRowTextView
= (TextView) rowView.findViewById( R.id.textView );
rowView.setTag( viewHolder );
}
ChildViewHolder holder = (ChildViewHolder) rowView.getTag();
TextView textView = holder.childRowTextView;
final String text = list.get(position);
textView.setText(text);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
return rowView;
}
}
}