2014-07-18 147 views
1

这是我的适配器代码清除的ListView

package com.example.dovizkuru; 

import java.util.ArrayList; 
import java.util.List; 
import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class Adapter extends BaseAdapter { 

    private LayoutInflater mInflater; 
    private List<Doviz>  dovizListesi; 


    public Adapter(Activity activity, ArrayList<Doviz> list) { 
     mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     dovizListesi = list; 
    } 

    @Override 
    public int getCount() { 
     return dovizListesi.size(); 
     } 

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

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
View satirView; 

     satirView = mInflater.inflate(R.layout.list_item, null); 
     TextView textView = 
       (TextView) satirView.findViewById(R.id.textView1); 
     TextView textView1 = 
       (TextView) satirView.findViewById(R.id.textView2); 
     TextView textView2 = 
       (TextView) satirView.findViewById(R.id.textView3); 
     TextView textView3 = 
       (TextView) satirView.findViewById(R.id.textView4); 
     TextView textView4 = 
       (TextView) satirView.findViewById(R.id.textView5); 
     ImageView imageView = 
       (ImageView) satirView.findViewById(R.id.imageView1); 

     textView.setText("Satış fiyati"); 
     textView1.setText("Alış fiyati"); 
     textView2.setText(dovizListesi.get(position).getAlis()); 
     textView3.setText(dovizListesi.get(position).getSatis()); 
     textView4.setText(dovizListesi.get(position).getAdi()); 
     imageView.setImageResource(R.drawable.abc_ab_bottom_solid_dark_holo); 


     return satirView; 

} 
} 

问题是每当我进入适配器它使堆积在列表顶部的数据,如它给人的第一6路输出,但是当some1点击按钮调用适配器再次它使输出12,而它应该已经停留在6刷新数据。我试图在适配器的最后添加清单()和我的列表的缺点,但最终出现错误。 问:我需要做什么,我可以替换我的ListView,而不是在顶部添加新数据。

这是我添加的地方。 try {JSONObject jObj = new JSONObject(response); jsonArrayGold = jObj.getJSONArray(“value”);

  for (int i = 0; i < jsonArrayGold.length(); i++) { 

       Altin altin = new Altin(); 

       JSONObject jsonObject = jsonArrayGold.getJSONObject(i); 
       altin.setAdi(jsonObject.getString("adi")); 
       altin.setAlis(jsonObject.getString("alis")); 
       altin.setSatis(jsonObject.getString("satis")); 
       altin.setKey(jsonObject.getString("key")); 
       altin.setKey2(jsonObject.getString("key2")); 
       altin.setType(jsonObject.getString("type")); 
       altin.setUpDown(jsonObject.getString("upDown")); 
       list.add(altin.getAdi()); 
       list.add(altin.getAlis()); 
       list.add(altin.getSatis()); 
       altinList.add(altin); 

      }  
    } catch (JSONException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
+0

你在哪里设置适配器?你可能在某处使用.add()。 –

+0

在数组列表中添加数据之前调用arraylistist.clear()。 –

+0

我添加了我添加的部分(); – Wince

回答

0

在向列表中添加新项目之前,先清空列表然后添加项目。然后从适配器调用notifyDataSetChanged()方法。

altinList.clear(); 

for(){ 
    altinList.add(altin); 



//adapter call 
adapter.notifyDataSetChanged(); 
+0

你的意思是我在适配器上写notifyer或? – Wince

+0

当您为listView设置适配器时,请保留适配器的引用。然后将其用于数据更改。适配器已经有这种方法顺便说一句。 –

+0

顺便说一句,我试过for(){...}之前的.clear方法,它很好地工作,我应该仍然把notifyDataSetChanged()在适配器? – Wince