【问题标题】:Why when i refresh my list it duplicates?为什么当我刷新我的列表时它会重复?
【发布时间】:2016-12-21 15:18:54
【问题描述】:

这是我的问题: 首先我有一个这样的列表视图:

当我点击第一行(例如 prod3)时,我的对话框出现如下(这是删除点击的行):

但是当我刷新我的片段时会发生这种情况:

为什么? Android上有类似“重新验证,重新绘制”(java)的功能吗?

这是我点击行时的代码:

  public void mostraProdotto(String tito){

    final  EditText textprod;
    final  EditText prezzo;

    Button btnConf;
    Button btnDelete;
    final Dialog dialogCustom = new Dialog(getActivity());

    dialogBuilder  = new AlertDialog.Builder(getActivity());
    //process

    dialogCustom.setContentView(R.layout.spesa_pagata);
    dialogCustom.setTitle("Nome Prod");
    textprod = (EditText)dialogCustom.findViewById(R.id.textprod);
    textprod.setText(tito);
    prezzo = (EditText)dialogCustom.findViewById(R.id.txtprezzo);
    btnConf = (Button)dialogCustom.findViewById(R.id.btnConf);


    btnConf.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            String nomeprod = textprod.getText().toString();
            String strprezzo = prezzo.getText().toString();

            if(!nomeprod.isEmpty() && !strprezzo.isEmpty()) {

       ..............


               new getSpesa().execute(); //getSpesa is for see listview
                dialogCustom.cancel();



        }

        }
    });

这是填写列表的代码:

                    listView = (ListView) rootView.findViewById(R.id.list_spesa);
                    listView.setY(20);
                    adapter = new CustomListAdapterSpesa(getActivity(), movieList);
                    adapter.notifyDataSetChanged();
                    listView.setAdapter(adapter);

编辑:

这是我的自定义适配器

public class CustomListAdapterSpesa extends BaseAdapter {

private Activity activity;

private LayoutInflater inflater;
private List<MovieSpesa> movieItems;

int pos;
public CustomListAdapterSpesa(Activity activity, List<MovieSpesa> movieItems) {
    this.activity = activity;
    this.movieItems = movieItems;
}

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

@Override
public Object getItem(int location) {
    return movieItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

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

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row, null);


    TextView title = (TextView) convertView.findViewById(R.id.title);

      TextView genre = (TextView) convertView.findViewById(R.id.genre);
  convertView.findViewById(R.id.releaseYear);

    MovieSpesa m = movieItems.get(position);


    int coint = getCount();


    // title
    title.setText(m.getTitle());



    // genre


    genre.setText(m.getGenre());

    return convertView;

}

}

【问题讨论】:

  • 请添加自定义适配器
  • 刷新 ListView 时可能在 movieList 中添加的项目。只需清除movieList,然后在referh 上添加项目
  • 还有你如何在代码中初始化movieList
  • 你如何填写你的 moveList ?我认为您正在将对象添加到列表中
  • moveList.clear() ,或者使用本地对象而不是全局 @francescobocci

标签: android listview fragment refresh


【解决方案1】:

适配器写入正确,因此问题可能出在列表端。

您发布了此代码:

listView = (ListView) rootView.findViewById(R.id.list_spesa);
listView.setY(20);
adapter = new CustomListAdapterSpesa(getActivity(), movieList);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);

问题一定是movieList的使用方式,它必须有一个类范围并且不是本地范围

所以你有两种可能的解决方案:

  1. 在添加另一个items 之前使用movieList.clear();。此方法清除之前添加的所有项目。这样,列表中的唯一项目将是新插入的项目。
  2. 您可以在使用前简单地验证list。 (这意味着本地)这样每次调用填充list的方法时,都会重新创建对象本身。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 2022-10-18
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    相关资源
    最近更新 更多