ArrayAdapter本身没有刷新(只更新了最后一个元素)

时间:2012-12-04 17:41:24

标签: java android android-layout

正如我的帖子所述,尽管调用"adapter.notifyDataSetChanged();",我似乎无法让我的ArrayAdapter更新整个列表。只更新列表中的最后一个元素。

有人可以在下面看到我做错了什么吗?

这是我的自定义适配器

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class WeatherAdapter extends ArrayAdapter<Weather>{

    Context context; 
    int layoutResourceId;    
    Weather data[] = null;
    private WeatherHolder holder;
    private Weather weather;


    public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new WeatherHolder();
            holder.imgIcon = (TextView)row.findViewById(R.id.imgen);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (WeatherHolder)row.getTag();
        }

        weather = data[position];
        holder.txtTitle.setText(weather.getName());
        //holder.imgIcon.setText(Double.toString(weather.getBuyingRate()));
        return row;
    }

    public void update(String buttonPressed){
        if(buttonPressed == "Köp Kurs"){
                            holder.imgIcon.setText(Double.toString(weather.getBuyingRate()));//This updates only the last element in list but I want to update every element in the list



        }
        else if(buttonPressed == "Antal"){              holder.imgIcon.setText(Double.toString(weather.getNrOfSharesInPortfolio()));//This updates only the last element in list but I want to update every element in the list


        }
    }

    static class WeatherHolder
    {
        TextView imgIcon;
        TextView txtTitle;
    }
}

这是我的主类,当我调用“Update()”方法时,只更新列表中的最后一个元素而不更新其他元素。如何更新整个列表而不仅仅是列表中的最后一个元素?

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

    private ListView listView1;
    private Button goButton;
    private String[] listheader = {"Köp Kurs","Antal"};
    private WeatherAdapter adapter;
    private int totalElemInlist = listheader.length;
    private int currentelemInList=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Weather weather_data[] = new Weather[]
        {
            new Weather("ABB", 56.0, 300),
            new Weather("Volvo", 89.0,500),
            new Weather("Astra Zeneca", 98.55, 50)
        };

        adapter = new WeatherAdapter(this, 
                R.layout.listview_item_row, weather_data);


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

        View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
        listView1.addHeaderView(header);
        goButton = (Button) findViewById(R.id.testButton);
        goButton.setText(listheader[currentelemInList]);

        listView1.setAdapter(adapter);


        goButton.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                String buttonPressed = (String) ((Button) view).getText();

                goThroughList(buttonPressed);
                System.out.println("Button Clicked" + buttonPressed);
            }
          });
    }

    private void goThroughList(String buttonPressed){
        currentelemInList++;
        if(currentelemInList>=totalElemInlist){
            currentelemInList=0;
        }
        goButton.setText(listheader[currentelemInList]);

        if(buttonPressed == "Köp Kurs"){
            System.out.println("Köp kurs");
            adapter.update(buttonPressed);
            adapter.notifyDataSetChanged();//This only updates the last element in list
        }
        else if(buttonPressed == "Antal"){
            System.out.println("Antal");
            adapter.update(buttonPressed);
            adapter.notifyDataSetChanged();//This only updates the last element in list
        }
        System.out.println(currentelemInList);
    }
}

3 个答案:

答案 0 :(得分:0)

你应该像这样比较字符串。

buttonPressed.equals("Köp Kurs")

答案 1 :(得分:0)

  

如何更新整个列表而不仅仅是列表中的最后一个元素?

您必须在getView()中进行更改。每当用户滚动ListView或调用getView()时,notifyDataSetChanged()都会重绘每一行。因此,必须在那里进行更改,否则它们将被删除。

首先在适配器中创建一个新的字段变量:

private String currentImage;

第二次更改update()以控制imgIcon上的内容:

public void update(String buttonPressed){
    currentImage = buttonPressed;
    notifyDataSetChanged();
}

上次更改getView()以显示每行的当前图片:

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

    weather = data[position];
    holder.txtTitle.setText(weather.getName());

    if(currentImage.equals("Köp Kurs")){
        holder.imgIcon.setText(Double.toString(weather.getBuyingRate()));
    }
    else if(currentImage.equals("Antal")){
        holder.imgIcon.setText(Double.toString(weather.getNrOfSharesInPortfolio()));
    }
    return row;
}

(您也可以简化goThroughList(),因为if-else包含完全相同的代码。)

答案 2 :(得分:0)

我可能错了,但我认为问题是因为你只持有持有人的最后一个元素。适配器中的成员变量“holder”应该是数组或列表,并且您应该保存所有元素,并且在update方法中,您应该遍历holder数组并更改文本

public void update(String buttonPressed){
    for(int index =0 ; index < holder.size();index++){
        if(buttonPressed == "Köp Kurs"){
                            holder[index].imgIcon.setText(Double.toString(weather.getBuyingRate()));//This updates only the last element in list but I want to update every element in the list



        }
        else if(buttonPressed == "Antal"){              holder[index].imgIcon.setText(Double.toString(weather.getNrOfSharesInPortfolio()));//This updates only the last element in list but I want to update every element in the list


        }

这应该可以解决您的问题