使用ListView和Adapter的空白活动

时间:2013-03-05 16:18:09

标签: android android-listview android-arrayadapter

我正在学习使用ListViews和Adapters。 我在这里发布的代码来自其中一个教程 Here 这是我的类(相当于教程中的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>

searchlist_row

<?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>

我从tutorial本身获取的其他XML list_selector,gradient_bg.xml,gradient_bg_hover.xml。

我的问题是,当我按下应该显示列表视图的活动时(通过按钮点击时的先前活动的意图),出现的屏幕完全是黑色和空白。 请帮帮我。

1 个答案:

答案 0 :(得分:1)

如果您希望ListView显示数据,您的适配器必须使用getCount()方法返回其包含(或想要显示)的项目数,而不是0(其中)如果适配器将被视为空,并且不会显示任何内容):

@Override
public int getCount() {       
    return data.size();
}