BaseAdapter.notifyDataSetChanged不会更新其ListView

时间:2016-08-31 11:29:05

标签: java android listview

在此之前,我使用了setAdapter和更新数据,但这会重置整个视图及其当前滚动位置,因此我想使用notifyDataChanged。我遵循了以下问题:BaseAdapter NotifyDatasetChanged() getView() Not WorkingnotifyDataSetChange not working from custom adapter。我没有创建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());
        }
    });
}

2 个答案:

答案 0 :(得分:1)

在适配器类中添加一个方法

<ul>
  <li>Not this</li>
  <ul>
    <li>I want to select this</li>
  </ul>
</ul>

然后从runOnUIThread调用此方法。

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

答案 1 :(得分:1)

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

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