2014-09-21 29 views
0

爲了在數據發生更改時在ListView上顯示新數據,我使用了NotifyDataSetchanged()。 但是,當我把下面的代碼,在listview上沒有任何東西。 什麼問題?我該怎麼做才能弄明白? 請讓我知道。如何在數據更改時在listview上顯示新數據,Notifydatasetchanged()

非常感謝你!

 String names[] = { 
      "Tom", 
      "Jane", 
      "Mary", 
      }; 

    Double lati[] = { 
      35.000001, 
      26.000001, 
      67.000001, 
      }; 

    Double longi[] = { 
      129.000001, 
      159.000001, 
      100.000001, 
      };   


    originalValues = new ArrayList<HashMap<String, Object>>(); 

    HashMap<String, Object> temp; 

    if (x==0){ 
     originalValues.clear(); 
      } 
    else{ 
     int oriname = names.length; 
     for (int i = 0; i < oriname; i++) { 
      temp = new HashMap<String, Object>(); 

      temp.put("name", names[i]); 
      temp.put("lati", lati[i]); 
      temp.put("longi", longi[i]); 

      temp.put("dis",  Math.round(Math.round(6371000.0 * Math.acos(Math.sin(Math.PI/180 * lati[i]) * Math.sin(Math.PI/180 * Double.valueOf(x)) 
    + Math.cos(Math.PI/180 * lati[i]) * Math.cos(Math.PI/180 * Double.valueOf(x)) * Math.cos(Math.PI/180 * (longi[i] - Double.valueOf(y)))))/1000)); 


      originalValues.add(temp); 

     } 
     adapter.notifyDataSetChanged(); 
    } 

    adapter = new CustomAdapter(this,R.layout.tab01_consul_row, originalValues); 
    insurListView.setAdapter(adapter); 

回答

0

從我從你的代碼中看到的,我覺得反而讓包含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時,它也更帥。

+0

非常感謝。不幸的是,即使我修改了所有內容以應用您的建議,也沒有任何改變如果我解決了這個問題,我會添加代碼。 – Young 2014-09-22 18:42:21

0

你調用adapter.notifyDataSetChanged();在你初始化你的適配器之前......我想你會在那裏得到一個異常,除非你在別的地方做了它,我不這麼認爲。

修復此問題,然後再試...請提供更多代碼

+0

我應用了許多建議來弄清楚,但仍未解決。情況正在惡化。如果我重新組織所有數據代碼,我會上傳。對此,我真的非常感激。 – Young 2014-09-22 18:44:00

相關問題