2013-06-02 39 views
1

我正在开发一个应用程序以获取附近的加油站并将它们显示在列表中,我的问题是将它们显示在列表中,我经历了许多教程,但无法弄清楚问题是没有列表出现与数据我相信这部分的问题,但不知道如何解决它,并确保在这个问题,我试图把进展对话,并直接解雇它之前这部分码。ArrayList无法在列表视图中显示 - android

这里使用的适配器,显示数组列表我从onPostexecute

protected void showItems(ArrayList<HashMap<String,String>> placestoList2){ 

      ListAdapter adapter101 = new SimpleAdapter(MainActivity.this, placestoList2, 
        R.layout.list_item, 
        new String[] { KEY_NAME, KEY_VICINITY}, new int[] { 
          R.id.name, R.id.vicinty }); 
lv.setAdapter(adapter101);  
     } 

调用该方法,并且此方法从列表中我得到的地方

protected ArrayList<HashMap<String,String>> getLocations(List<HashMap<String,String>> list){ 

       for(int i=0;i<list.size();i++){ 

        HashMap<String, String> mapM = list.get(i); 
        HashMap<String, String> mapM2 = new HashMap<String, String>(); 


        String name = mapM.get("place_name"); 
        mapM2.put(KEY_NAME, name); 

        String vicinity = mapM.get("vicinity"); 
        mapM2.put(vicinity, vicinity); 
        placesListItems.add(mapM2); 
        } 
       return placesListItems; 
} 

和清单数据在布局中查看

<ListView 
     android:id="@+id/listView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 

其他布局

<TextView android:id="@+id/name" 
    android:visibility="gone" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 

<TextView android:id="@+id/vicinty" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dip" 
    android:textStyle="bold" 
    android:textSize="16sp"/> 
+0

你是什么意思“但不知道问题?”你的代码在做什么或者不在做你希望发生或者不发生的事情? – MarsAtomic

+0

请参阅我编辑的问题 –

回答

0

您需要的是自定义适配器。以下是你需要这样做的方法。如果我没有错,你的数据存储在List>中。根据您的要求更改。下面是你在你的活动或片段做什么:

ListView listView; 
listView=(ListView)findViewById(R.id.listView); 

//Call this when you have data. If you are only updating think of something else 
Adapterlist adapter=new Adapterlist(this,getData()); 
listView.setAdapter(adapter); 

这里的自定义适配器:

public class Adapterlist extends BaseAdapter { 

private Context context; 
private List<HashMap<String,String>> list; 

public static final String PLACE="place_name"; 
public static final String VICINITY="vicinty"; 

public AdapterApplist(Context context, List<HashMap<String,String>> list) { 
    this.context = context; 
    this.list = list; 
} 

public int getCount() { 
    return list.size(); 
} 

public Object getItem(int position) { 
    return position; 
} 

public long getItemId(int position) { 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 

    if (convertView == null){ 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = inflater.inflate(R.layout.list_row, null); 

     holder = new ViewHolder(); 
     holder.name = (TextView) view 
       .findViewById(R.id.name); 
     holder.vicinty = (TextView) view 
       .findViewById(R.id.vicinty); 

     convertView.setTag(holder); 
    }else{ 
     holder=(ViewHolder)convertView.getTag(); 
    } 

    setUI(holder,position); 

    return view; 
} 

private void setUI(ViewHolder holder,int position){ 
    HashMap<String, String> map = list.get(position); 

    holder.name.setText(map.get(PLACE)); 
    holder.vicinty.setText(map.get(VICINITY)); 
} 

public static class ViewHolder { 
    public TextView name; 
    public TextView vicinty; 
} 

}

请做更多的研究下一次。这可以在互联网上找到。

+0

感谢您的回答,感谢您,我刚刚尝试过,但没有获得视图,我也不是很高的代码专家,这就是为什么我发布一个问题,我做了搜索,但没有得到我需要 –

+0

记录你的list.size()在适配器的构造函数中,并确保列表不是空的,如果它不是发布完整的XML布局。 – Milan

+0

我试图在构造函数中发送列表的大小,并使公共int getCount(){ // return list.size(); \t返回大小; } 但没有任何东西出现,对于第二部分列表不是空的,因为我尝试了一些东西是使用列表中的latlng在地图中绘制标记,并且发生了这种情况,我不知道缺少什么东西,解析列表活动和适配器类之间 –