2012-11-08 49 views
0

所以我有一个基础适配器类,我建立。我也尝试在我的一项活动中使用它。我有一个用于父布局的xml和用于膨胀的行的xml。但是,这些组件不能链接在一起。该应用程序不会崩溃,但行不膨胀(尽管母版xml文件仍显示在屏幕上)。可以看看我的代码并告诉我我忽略了哪些关键组件?我只会发布相关的代码。无法发布错误日志,因为没有发生错误。非常感谢BaseAdapter not populating listview

父XML(nearby_places_list)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="10dp" 
    android:orientation="vertical" 
    android:background="@drawable/bg"> 

    <Button 
    android:id="@+id/continuetomap" 
    android:layout_height="100dp" 
    android:layout_width="match_parent" 
    android:background="@drawable/map_continue" 
    /> 

    <ListView 
     android:id="@+id/list_view_nearby" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:divider="#b5b5b5" 
     android:listSelector="@drawable/list_selector"> 
    </ListView> 


</LinearLayout> 

行膨胀XML(nearby_list_row)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:padding="5dp" 
     android:orientation="vertical" 
     android:background="@drawable/list_selector">" 

     <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:background="@drawable/list_selector"> 

      <LinearLayout 
      android:layout_width="0dp" 
      android:layout_weight="3" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:background="@drawable/list_selector"> 
       <TextView 
        android:id= "@+id/name"   
        android:textSize="20dp" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="name" 
        android:textColor="#54944d"/> 
       <TextView 
        android:id= "@+id/vicinity"   
        android:textSize="20dp" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="vicinity" 
        android:textColor="#000000"/> 
        <TextView 
        android:id= "@+id/openorclosed" 
        android:textSize="20dp" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Open" 
        android:textColor="#000000" 
        android:textStyle="bold"/>  
        </LinearLayout> 


      <RelativeLayout 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.74" > 

       <Button 
       android:layout_width="60dp" 
       android:id="@+id/button_add_to_fav" 
       android:background="@drawable/add_square" 
       android:layout_height="60dp" 
       android:layout_alignParentRight="true" 
       /> 

       <Button 
       android:layout_width="60dp" 
       android:id="@+id/button_call_rest" 
       android:background="@drawable/add_square" 
       android:layout_height="60dp" 
       android:layout_alignParentRight="true" 
       android:layout_below="@+id/button_add_to_fav" 
       /> 

      </RelativeLayout> 

      </LinearLayout> 


    </RelativeLayout> 

底座适配器类

public class AdapterForList extends BaseAdapter { 

    private final Context context; 
    private final ArrayList<HashMap<String, String>> alHm; 
    private final String TAG_A; 
    private final String TAG_B; 
    private final String TAG_C; 


    public AdapterForList(Context context, ArrayList<HashMap<String, String>> alhm, String TAG_1, String TAG_2, String TAG_3) { 
     super(); 
     this.context = context; 
     this.alHm = alhm; 
     TAG_A = TAG_1; 
     TAG_B = TAG_2; 
     TAG_C = TAG_3; 
    } 

    @Override 
    public View getView(int position, View convertView , ViewGroup parent){ 
     LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     convertView = inflator.inflate(R.layout.nearby_list_row, parent, false); 
     TextView tv1 = (TextView) convertView.findViewById(R.id.name); 
     TextView tv2 = (TextView) convertView.findViewById(R.id.vicinity); 
     TextView tv3 = (TextView) convertView.findViewById(R.id.openorclosed); 
     Button btnAdd= (Button) convertView.findViewById(R.id.button_add_to_fav); 
     Button btnCall= (Button) convertView.findViewById(R.id.button_call_rest); 

     if (alHm.get(position).containsKey(TAG_A)){ // get the value from the key (if it exist) and set the first text view to it 
      String value = alHm.get(position).get(TAG_A); 
      tv1.setText(value);; 
     } 

     if (alHm.get(position).containsKey(TAG_B)){ 
      String value = alHm.get(position).get(TAG_B); 
      tv2.setText(value);; 
     } 

     if (alHm.get(position).containsKey(TAG_C)){ // get the value from the key (if it exist) and set the first text view to it 
      String value = alHm.get(position).get(TAG_C); 
      tv3.setText(value);; 
     } 

     return convertView; 
    } 
    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return alHm.size(); 
    } 
    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 
    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

} 

相关活动代码

protected void onPostExecute(Boolean result){ 
    if (result){ 
     // adding data to listview 
     final ListView lv = (ListView) findViewById(R.id.list_view_nearby); 
     lv.setAdapter(new AdapterForList(context, listOfHM, JSONextractor.TAG_NAME, 
      JSONextractor.TAG_VICINITY, JSONextractor.TAG_OPERATINGHOURS_OPENNOW)); 

     //more code ... 

    } 
} 
+0

你在AdapterForList构造记录的alhm的ArrayList大小试过吗?因为如果是空的多数民众赞成在问题... – sabadow

+0

阿尔姆有正确的计数为20,其中的值是正确的。正确的listview也被找到 –

+0

WTF?!?!它现在工作正常。我还没有改变一行代码。我已经运行这个并在调试器中多次遍历这个 –

回答

0

因此,事实证明我的代码确实工作(但不是最佳)。我在下面发布如何正确使用基础适配器

刚刚阅读由savadow构成的文章(真正打开我的想法)。基本上,我有我的BaseAdapter我没有利用任何类的优化能力。相反,我会将代码更改为基本适配器类中的以下内容。

http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/

public View getView(int position, View convertView , ViewGroup parent){ 
     LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null){ 
     convertView = inflator.inflate(R.layout.nearby_list_row, parent, false); 
     } 
     // now if the convertView is not null i simply update the values rather than re-inflate the view (inflating is expensive) 

     tv1 = (TextView) convertView.findViewById(R.id.name); 
     tv2 = (TextView) convertView.findViewById(R.id.vicinity); 
     tv3 = (TextView) convertView.findViewById(R.id.openorclosed); 
     btnAdd= (Button) convertView.findViewById(R.id.button_add_to_fav); 
     btnCall= (Button) convertView.findViewById(R.id.button_call_rest);