繁体   English   中英

BaseAdapter.notifyDataSetChanged不会更新其ListView

[英]BaseAdapter.notifyDataSetChanged doesn't update its ListView

在此之前,我将setAdapter与更新数据一起使用,但这会重置整个视图及其当前滚动位置,因此我想使用notifyDataChanged。 我遵循以下问题: BaseAdapter NotifyDatasetChanged()getView()不起作用notifyDataSetChange从自定义适配器不起作用 我没有创建arraylist的新实例,而是对其进行了更新。
但是它仍然不起作用。


我的基本适配器:

public class ListViewAdapter extends BaseAdapter{
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
ImageView txtFourth;
public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
    super();
    this.activity=activity;
    this.list=list;
}

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

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

@Override
public long getItemId(int position) {
    return position; //this was 0, I changed according to Vlad's suggestion
}

public void setNewList(ArrayList<HashMap<String, String>> list) {
    this.list.clear();
    this.list.addAll(list);
    notifyDataSetChanged();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater=activity.getLayoutInflater();
    if(convertView == null){
        convertView=inflater.inflate(R.layout.row, null);

        txtFirst=(TextView) convertView.findViewById(R.id.cellID);
        txtSecond = (TextView) convertView.findViewById(R.id.speedInfo);
        txtThird = (TextView) convertView.findViewById(R.id.gpsPointInfo);
        txtFourth=(ImageView) convertView.findViewById(R.id.status);
    }
    HashMap<String, String> map=list.get(position);
    txtFirst.setText(map.get(Constants.FIRST_COLUMN));
    txtSecond.setText(map.get(Constants.SECOND_COLUMN));
    txtThird.setText(map.get(Constants.THIRD_COLUMN));
    txtFourth.setImageResource(Integer.valueOf(map.get(Constants.FOURTH_COLUMN)));

    return convertView;
}


}

我创建和更新列表视图的类包括:

public class CellListViewTool {
    ArrayList<HashMap<String,String>> cellList;
    ListView cellListView;
    Activity mainActivity;
    ArrayList<QRCell> qrCells;
    ListViewAdapter adapter;

    public CellListViewTool(Activity mainActivity, ArrayList<QRCell> qrCells, ListView cellListView) {
        this.qrCells = qrCells;
        this.cellListView = cellListView;
        this.mainActivity = mainActivity;
        createCellListView();
    }

    private void createCellListView() {
        cellList = new ArrayList<>();

        for (QRCell qrCell : qrCells) {
            addRowToListView(qrCell, cellList);
        }
        mainActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter = new ListViewAdapter(mainActivity, cellList);
                cellListView.setAdapter(adapter);
            }
        });

    }

    public synchronized void updateCellListView() {
        final ArrayList<HashMap<String, String>> newCellList = new ArrayList<>();

        for (QRCell QRCell : qrCells) {
            addRowToListView(QRCell, newCellList);
        }

        mainActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Log.d("HI", "run: refresh");
                adapter.setNewList(newCellList);
                Log.d("HI", "run: " + adapter.getItem(0).toString());
            }
        });
    }

    private void addRowToListView(QRCell qrCell, ArrayList<HashMap<String, String>> cellList) {
        String cellID = String.valueOf(qrCell.getCellID()),
                currentSpeed,
                currentPoints,
                targetSpeed = String.valueOf(qrCell.getTargetSpeed()),
                targetPoints = String.valueOf(qrCell.getTargetPoints());
        String speed;
        String points;
        if (qrCell.getStatus() != Constants.TestStatus.NOT_TESTED){
            currentSpeed = String.format("%.2f", qrCell.getMaxSpeed());
            currentPoints = String.valueOf(qrCell.getCurrentPoints());
        } else {
            currentSpeed = mainActivity.getString(R.string.not_active);
            currentPoints = mainActivity.getString(R.string.not_active);
        }
        speed = currentSpeed + "/ " + targetSpeed;
        points = currentPoints + "/ " + targetPoints;
        Constants.TestStatus status = qrCell.getStatus();

        HashMap<String, String> temp = new HashMap<>();
        temp.put(Constants.FIRST_COLUMN, "L" + cellID);
        temp.put(Constants.SECOND_COLUMN, speed);
        temp.put(Constants.THIRD_COLUMN, points);
        temp.put(Constants.FOURTH_COLUMN, String.valueOf(getStatusResID(status)));
        cellList.add(temp);
    }
    private int getStatusResID(Constants.TestStatus status) {
        switch (status) {
            case SUCCESS:
                return R.drawable.success_icon;
            case NOT_TESTED:
                return R.drawable.not_tested;
            case RUNNING:
                return R.drawable.running_icon;
            default:
                return R.drawable.failed_icon;
        }
    }
}

有人能帮我吗? 谢谢。


更新

我使用ArrayAdapter,但仍然无法正常工作。 我的ArrayAdapter类:

public class ListViewAdapter extends ArrayAdapter {

public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
ImageView txtFourth;
public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
    super(activity, 0, list);
    this.activity=activity;
    this.list=list;
}

public void setNewList(ArrayList<HashMap<String, String>> list) {
    this.list.clear();
    this.list.addAll(list);
    notifyDataSetChanged();
}

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

    LayoutInflater inflater=activity.getLayoutInflater();

    if(convertView == null){

        convertView=inflater.inflate(R.layout.row,null);

        txtFirst=(TextView) convertView.findViewById(R.id.cellID);
        txtSecond = (TextView) convertView.findViewById(R.id.speedInfo);
        txtThird = (TextView) convertView.findViewById(R.id.gpsPointInfo);
        txtFourth=(ImageView) convertView.findViewById(R.id.status);
    }

    HashMap<String, String> map=list.get(position);
    txtFirst.setText(map.get(Constants.FIRST_COLUMN));
    txtSecond.setText(map.get(Constants.SECOND_COLUMN));
    txtThird.setText(map.get(Constants.THIRD_COLUMN));
    txtFourth.setImageResource(Integer.valueOf(map.get(Constants.FOURTH_COLUMN)));

    return convertView;
}


}

在下面的此方法中,我记录了适配器的第一项并进行了更新。 但是我不知道为什么它不会在UI视图中更新...

public synchronized void updateCellListView() {
    final ArrayList<HashMap<String, String>> newCellList = new ArrayList<>();

    for (QRCell QRCell : qrCells) {
        addRowToListView(QRCell, newCellList);
    }

    mainActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Log.d("HI", "run: refresh");
            adapter.setNewList(newCellList);
            Log.d("HI", "run: " + adapter.getItem(0).toString());
        }
    });
}

在适配器类中添加方法

public void setNewList(ArrayList<HashMap<String, String>> newList){
   this.list.clear();
   this.list.addAll(newList);
   notifyDataSetChanged()
}

然后从runOnUIThread调用此方法。

public synchronized void updateCellListView() {
    final ArrayList<HashMap<String,String>> newCellList = new ArrayList<>();

    for (QRCell qrCell : qrCells) {
        addRowToListView(qrCell, newCellList);
    }

    mainActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            adapter.setNewList(newCellList);
        }
    });
}

如果您还没有听说过,则有一个新的更好的ListViews替代品,称为RecyclerView。 这是一个教程https://developer.android.com/training/material/lists-cards.html

可能值得研究,我们一直在使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM