我正在尝试制作一个列表视图,其中每个节点都有一个标题和2个子标题。它出于某种原因崩溃了。我似乎无法弄明白。请帮忙
Borrowed CLASS:
public class Borrowed extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.borrowed_layout, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
MyAdapter adapter = new MyAdapter(this.getActivity(), generateData());
ListView listView = getListView();
listView.setAdapter(adapter);
}
private ArrayList<Item> generateData(){
ArrayList<Item> items = new ArrayList<Item>();
items.add(new Item("Physics notes","Hazim","1/1/14"));
items.add(new Item("Football","Hazim","1/1/14"));
items.add(new Item("dil","sb bta du?","1/1/14"));
return items;
}
}
然后是Adapter类:
public class MyAdapter extends ArrayAdapter<Item> {
private final Context context;
private final ArrayList<Item> itemsArrayList;
public MyAdapter(Context context, ArrayList<Item> itemsArrayList) {
super(context, R.layout.row, itemsArrayList);
this.context = context;
this.itemsArrayList = itemsArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = inflater.inflate(R.layout.row, parent, false);
// 3. Get the two text view from the rowView
TextView itemView = (TextView) rowView.findViewById(R.id.item);
TextView nameView = (TextView) rowView.findViewById(R.id.name);
TextView dateView = (TextView) rowView.findViewById(R.id.date);
// 4. Set the text for textView
itemView.setText(itemsArrayList.get(position).getItem());
nameView.setText(itemsArrayList.get(position).getName());
dateView.setText(itemsArrayList.get(position).getDate());
// 5. retrn rowView
return rowView;
}
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:paddingLeft="5dp">
</TextView>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/label"
android:textSize="12sp"
android:paddingLeft="5dp" >
</TextView>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/label"
android:textSize="12sp"
android:paddingLeft="5dp" >
</TextView>
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
请帮助我。提前谢谢你
答案 0 :(得分:0)
覆盖onActivityCreated
并初始化为
ListView listView = getListView();
并且在xml中有
<ListView android:id="@android:id/list"
http://developer.android.com/reference/android/app/ListActivity.html