【发布时间】:2013-11-16 19:02:46
【问题描述】:
我创建了这个名为 GeoArea 的类,它假设存储有子地理区域的“地理区域”,这是相当严格的:
public class GeoArea {
public String id;
public String name;
public List<GeoArea> subGeoAreas;
public GeoArea parentGeoArea;
public GeoArea(String id) {
this.id = id;
name = id;
subGeoAreas = new LinkedList<GeoArea>();
}
@Override
public String toString() {
return name;
}
}
我创建了以下布局以在 Android 上呈现它,这里的想法是让每个 GeoArea 递归地呈现它自己,然后它是 listView 中的子 GeoArea:
//layout_geo_area.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/txtGeoAreaName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:text="Geo Area Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="@+id/listViewChildGeoAreas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txtGeoAreaName"
android:gravity="left" >
</ListView>
</RelativeLayout>
这是我为 GeoArea 创建的要在 listView 中显示的适配器:
public class AdapterGeoArea extends ArrayAdapter<GeoArea>{
private ArrayList<GeoArea> _myGeoArea;
private Context _myContext;
LayoutInflater _inflater;
public AdapterGeoArea(Context context, ArrayList<GeoArea> myGeoArea) {
super(context, 0, myGeoArea);
_myGeoArea = myGeoArea;
_inflater = LayoutInflater.from(context);
_myContext = context;
}
public int getCount() {
return _myGeoArea.size();
}
public View getView(int position, View convertView, ViewGroup parent) {
GeoAreaLayoutHolder holder;
if (convertView == null) {
convertView = _inflater.inflate(R.layout.layout_geo_area,parent,false);
holder = new GeoAreaLayoutHolder();
holder.txtGeoAreaName = (TextView)convertView.findViewById(R.id.txtGeoAreaName);
holder.txtGeoAreaName.setTag(convertView);
holder.listViewChildGeoAreas = (ListView)convertView.findViewById(R.id.listViewChildGeoAreas);
holder.listViewChildGeoAreas.setTag(convertView);
} else {
holder = (GeoAreaLayoutHolder) convertView.getTag();
}
GeoArea curGeoArea = _myGeoArea.get(position);
holder.txtGeoAreaName.setText(curGeoArea.name);
if(curGeoArea.subGeoAreas.size()>0){
ArrayList<GeoArea> testList = new ArrayList<GeoArea>();
AdapterGeoArea adapter = new AdapterGeoArea(_myContext, testList);
for(GeoArea childGeoArea:curGeoArea.subGeoAreas){
testList.add(childGeoArea);
}
holder.listViewChildGeoAreas.setAdapter(adapter);
}
return convertView;
}
static class GeoAreaLayoutHolder {
public TextView txtGeoAreaName;
public ListView listViewChildGeoAreas;
}
}
这是我用来设置它的活动:
public class ActivityGeoAreas extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_geo_area);
GeoArea.searchTerm = "Bar & Grill";
GeoArea torontoArea = new GeoArea("cityOfToronto");
ArrayList<GeoArea> testList = new ArrayList<GeoArea>();
testList.add(torontoArea);
AdapterGeoArea adapter = new AdapterGeoArea(this, testList);
ListView lv = (ListView) findViewById(R.id.listViewChildGeoAreas);
lv.setAdapter(adapter);
}
}
当我尝试运行它时,我得到错误 nullPointerException 就行了:
holder.txtGeoAreaName.setText(curGeoArea.name);
我做错了什么?
【问题讨论】:
-
你还是一头雾水。
layout_geo_area.xml中的 ListView 和另一个 xml 中的文本视图。具有为列表视图中的每一行膨胀的自定义布局的 textview。所以在你的res/layout中创建list_item.xml。然后在那个xml中有textview。在getView中膨胀相同 -
在你的情况下,你有一个列表视图,每行文本视图和另一个列表视图。如果我更了解您的问题,最好使用
ExpandableListView -
您在活动类和自定义适配器类中膨胀相同的布局。
setContentView(R.layout.layout_geo_area)和convertView = _inflater.inflate(R.layout.layout_geo_area,parent,false);它的R.layout_geo_area。没有意义。我建议你寻找可扩展的 lsitview。
标签: android listview android-listview android-arrayadapter android-adapter