我正在学习将 ListViews 与适配器一起使用。我在此处发布的代码来自 Here This is my class(相当于教程中的 MainActvity 类)的教程之一,其中我正在初始化 ListView 并向适配器提供数据。
setContentView(R.layout.searchresultrowlayout);
try {
jsonarray1=searchscreen.searchresults();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayList<HashMap<String, String>> propertylist=new ArrayList<HashMap<String, String>> ();
for(int i=0;i<jsonarray1.length();i++){
HashMap<String,String> propertymap=new HashMap<String,String> ();
try {
jsonobject=jsonarray1.getJSONObject(i);
Log.d("json", jsonobject.getString("id"));
propertymap.put("propertytype",jsonobject.getString("propertytype") );
propertylist.add(propertymap);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
list=(ListView)findViewById(R.id.list);
adapter=new PropertySearchArrayAdapter(this,propertylist);
// Log.d("adapter", propertylist.get(1).get("price").toString());
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
这是我的适配器类
public class PropertySearchArrayAdapter extends BaseAdapter{
private Activity activity;
private ArrayList<HashMap<String,String>> data;
private static LayoutInflater inflater=null;
//public ImageLoader imageloader;
public PropertySearchArrayAdapter(Activity context, ArrayList<HashMap<String,String>> d){
activity=context;
data=d;
inflater=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position,View convertview, ViewGroup parent){
View rowview=convertview;
if(convertview==null){
rowview=inflater.inflate(R.layout.searchlist_row, null);}
//ViewHolder viewholder=new ViewHolder();
TextView propertyType=(TextView) rowview.findViewById(R.id.propertytype); //propertytype
HashMap<String,String> property= new HashMap<String,String> ();
property= data.get(position);
propertyType.setText(property.get("propertytype"));
Log.d("adaptertext", property.get("propertytype"));
return rowview;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
}
这些是 XML
searchresultrowlayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</LinearLayout>
搜索列表行
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal"
android:padding="5dip" >
<!-- Property type-->
<TextView
android:id="@+id/propertytype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
</RelativeLayout>
其他 XMLs list_selector,gradient_bg.xml,gradient_bg_hover.xml 我取自教程本身。
我的问题是,当我推动应该显示列表视图的活动时(通过按钮单击上一个活动的意图),出现的屏幕完全是黑色和空白的。请帮帮我。