從我從你的代碼中看到的,我覺得反而讓包含HashMap的ArrayList(通常是太複雜的想法本身),使一個類中,你將存儲三個值:名稱,緯度和經度。然後用這個對象列表的適配器做一個listview。 例如,您的列表視圖項目類:
public class Item {
private String name;
private double lati;
private double longi;
public Item() {
}
public Item(String name, double lati, double longi) {
this.name = name;
this.lati = lati;
this.longi = longi;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getLati() {
return lati;
}
public void setLati(double lati) {
this.lati = lati;
}
public double getLongi() {
return longi;
}
public void setLongi(double longi) {
this.longi = longi;
}
@Override
public String toString() {
return name + ", " + lati + ", " + longi;
}
}
然後使適配器類:
public class ItemAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<Item> items;
private Context context;
public ItemAdapter(Context context) {
super();
inflater = LayoutInflater.from(context);
this.context = context;
}
public ItemAdapter(Context context, List<Item> items) {
super();
inflater = LayoutInflater.from(context);
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.lv_row, parent, false);
TextView model = (TextView) convertView.findViewById(R.id.modelTV);
model.setText(items.get(position).toString());
return convertView;
}
public void reloadItems(List<Item> items) {
this.items = items;
notifyDataSetChanged();
}
}
,然後在活動類創建與此適配器您的列表視圖。 每當你想改變你的列表視圖數據,請與適配器的reloadItems方法:
public class MainActivity extends Activity {
private ListView lv;
private ItemAdapter adapter;
private List<Item> items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
items = new ArrayList<Item>();
items.add(new Item("Tom", 35.000001, 129.000001));
items.add(new Item("Jane", 26.000001, 159.000001));
items.add(new Item("Mary", 67.000001, 100.000001));
adapter = new ItemAdapter(getApplicationContext(), items);
lv = (ListView) findViewById(R.id.list);
lv.setAdapter(adapter);
}
@Override
protected void onResume() {
super.onResume();
// if list items changed reload items with newer
// by calling adapter.reloadItems(items);
// the best way to do it though, is to do it in separate thread,
// for example in asynctask
adapter.reloadItems(items);
}
}
現在唯一剩下的就是建立在MainActivity和爲ListView行佈局。 MainActivity的 實施例的佈局,main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, TestActivity"
/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
最後一個,佈局列表視圖行,lv_row.xml:
<?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:padding="6dip" >
<TextView
android:id="@+id/modelTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:textSize="16sp"
/>
</LinearLayout>
和清單文件,提供含有MainActivity類別包名稱:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your package here"
android:versionCode="1"
android:versionName="1.0">
<application android:label="@string/app_name" >
<activity
android:name="MainActivity"
android:label="Simple Listview">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
請注意,例如ItemAdapter類在此處不是必需的。您可以改用默認的ArrayAdapter。但簡而言之,通過提供自己的適配器,您可以更好地控制列表視圖數據。在處理包含你自己的模型的listview時,它也更帥。
非常感謝。不幸的是,即使我修改了所有內容以應用您的建議,也沒有任何改變如果我解決了這個問題,我會添加代碼。 – Young 2014-09-22 18:42:21