为什么当我刷新列表时它会重复?
Why when i refresh my list it duplicates?
这是我的问题:
首先我有一个这样的列表视图:
当我单击第一行(例如 prod3)时,会出现这样的对话框(这是用于删除单击的行):
但是当我刷新我的片段时发生了这件事:
为什么?在 Android ?
上有一个类似 "revalidate, repaint" (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 = (ListView) rootView.findViewById(R.id.list_spesa);
listView.setY(20);
adapter = new CustomListAdapterSpesa(getActivity(), movieList);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
问题一定出在如何使用 movieList
上,它必须有一个 class-scope 而 不是局部范围.
所以你有两个可能的解决方案:
- 在添加另一个
items
之前使用 movieList.clear();
。此方法清除所有先前添加的项目。这样,列表中的唯一项目将是新插入的项目。
- 您可以在使用前简单地实例化
list
。 (这意味着在本地)这样每次调用填充list
的方法时,都会重新创建对象本身。
希望这对您有所帮助。
这是我的问题: 首先我有一个这样的列表视图:
当我单击第一行(例如 prod3)时,会出现这样的对话框(这是用于删除单击的行):
但是当我刷新我的片段时发生了这件事:
为什么?在 Android ?
上有一个类似 "revalidate, repaint" (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 = (ListView) rootView.findViewById(R.id.list_spesa);
listView.setY(20);
adapter = new CustomListAdapterSpesa(getActivity(), movieList);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
问题一定出在如何使用 movieList
上,它必须有一个 class-scope 而 不是局部范围.
所以你有两个可能的解决方案:
- 在添加另一个
items
之前使用movieList.clear();
。此方法清除所有先前添加的项目。这样,列表中的唯一项目将是新插入的项目。 - 您可以在使用前简单地实例化
list
。 (这意味着在本地)这样每次调用填充list
的方法时,都会重新创建对象本身。
希望这对您有所帮助。