清除ListView

时间:2014-07-18 12:56:16

标签: android adapter

这是我的适配器代码

 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,而不是在顶部添加新数据。

这是我添加的地方。     尝试{             JSONObject jObj = new JSONObject(response);                 jsonArrayGold = jObj.getJSONArray(&#34; value&#34;);

            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();
    } 

1 个答案:

答案 0 :(得分:0)

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

altinList.clear();

for (){
    altinList.add(altin);



//adapter call
adapter.notifyDataSetChanged();