2013-03-05 1 views
0

ListView를 어댑터와 함께 사용하는 방법을 배우고 있습니다. 여기에 게시 할 코드는 Here 의 자습서 중 하나입니다.이 클래스는 ListView를 초기화하고 어댑터에 데이터를 제공하는 클래스입니다 (자습서의 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; 
} 

}

입니다 이러한 XMLS

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> 
입니다

다른 XML list_selector, gradient_bg.xml, gradient_bg_hover.xml 자체는 tutorial에서 가져 왔습니다.

내 문제는 목록보기 (버튼 클릭시 이전 활동의 의도에 의한)를 표시해야하는 내 활동을 푸시 할 때 나타나는 화면이 완전히 검게 표시됩니다. 도와주세요.

+1

예외 추적 스택 추적 및 게시가있을 수 있습니다. – Pragnani

+0

답장을 보내 주셔서 감사 드리며 예외 나 경고가 없습니다. – user1581539

+0

목록에 데이터가 포함되어 있습니까? ..? – Pragnani

답변

1

당신이 데이터를 표시하기 위해 ListView을하려면 어댑터가 포함 된 (또는 보여주고 싶어)을 getCount() 방법 및 0 (이 경우 어댑터가 비어있는 것으로 간주됩니다하지 아무것도 없을 것입니다 항목 수를 반환해야합니다 표시) :

@Override 
public int getCount() {  
    return data.size(); 
} 
관련 문제