簡體   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