Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayAdapter未刷新自身(仅更新最后一个元素)_Java_Android_Android Layout - Fatal编程技术网

Java ArrayAdapter未刷新自身(仅更新最后一个元素)

Java ArrayAdapter未刷新自身(仅更新最后一个元素),java,android,android-layout,Java,Android,Android Layout,正如我的帖子所述,尽管调用了“adapter.notifyDataSetChanged();”只更新了列表中的最后一个元素,但我似乎无法让我的ArrayAdapter更新整个列表 有人能看到下面我做错了什么吗 这是我的自定义适配器 import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import andr

正如我的帖子所述,尽管调用了
“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;
    }
}

您应该像这样比较字符串

buttonPressed.equals("Köp Kurs")
如何更新整个列表而不是列表中的最后一个元素

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);
    }
}
您必须在
getView()
中进行更改
getView()
每当用户滚动ListView或调用
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包含完全相同的代码。)

我可能错了,但我认为问题在于您只保留了保持架中的最后一个元素。适配器中的成员变量“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


        }
公共作废更新(字符串按钮按下){
对于(int index=0;index

这应该可以解决您的问题

使用
equals()
来比较
字符串
!Luksprog做对了…但在这里是不相关的…字段
holder
存储在getView中设置的最后一个holder(通过
holder=new WeatherHolder();
holder=(WeatherHolder)行。getTag()
…只需使用上一个问题中的我的答案…(你走错了方向…最好不要触摸适配器外的行视图)这不是该问题的答案…你指望免费声誉分数吗?(因为你只是在获得高投票率的Luksprog评论后重新重复)在这个特定的例子中,我非常确定字符串是被插入的,所以…事实上,您可以使用==运算符…但强烈建议不要使用它(因为它取决于虚拟机插入了哪些字符串)。。。