我正在使用带有可滑动标签栏的片段,其中一个片段我正在添加编辑文本,我通过它搜索listview项目。 我通过字符串资源中的srting数组添加listview项。 问题开始时,我滑动片段并进入我使用listview的片段或选项卡栏中的列表视图中的项目自动生成以前项目的新副本,即项目变得双倍或三倍或任何时候的次数我的片段是在我在片段中添加了listview oncreatview时创建的。
我正在添加listview的片段代码
public class HingDict extends Fragment {
ListView list;
ListViewAdapter adapter;
EditText editsearch;
Resources res;
ArrayList<Dictionary> arraylist = new ArrayList<Dictionary>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.hingdict,container,false);
res=getResources();
String hinglish[]=res.getStringArray(R.array.hinglish);
String english[]=res.getStringArray(R.array.english);
list = (ListView)view.findViewById(R.id.listview);
for (int i = 0; i < hinglish.length; i++)
{
Dictionary dc = new Dictionary(hinglish[i],
english[i]);
arraylist.add(dc);
}
adapter = new ListViewAdapter(getActivity(), arraylist);
list.setAdapter(adapter);
editsearch = (EditText)view. findViewById(R.id.search);
editsearch.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
adapter.filter(text);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
});
return view;
}
}
listview适配器类的代码
public class ListViewAdapter extends BaseAdapter {
Context mContext;
LayoutInflater inflater;
private List<Dictionary> dictionarylist = null;
private ArrayList<Dictionary> arraylist;
public ListViewAdapter(Context context, List<Dictionary> dictionarylist) {
mContext = context;
this.dictionarylist = dictionarylist;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<Dictionary>();
this.arraylist.addAll(dictionarylist);
}
public class ViewHolder {
TextView hinglish;
TextView english;
//TextView impression;
}
@Override
public int getCount() {
return dictionarylist.size();
}
@Override
public Dictionary getItem(int position) {
return dictionarylist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
holder.hinglish = (TextView) view.findViewById(R.id.hinglish);
holder.english = (TextView) view.findViewById(R.id.english);
// holder.impression=(TextView)view.findViewById(R.id.impression);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.hinglish.setText(dictionarylist.get(position).getHinglish());
holder.english.setText(dictionarylist.get(position).getEnglish());
//holder.impression.setText(dictionarylist.get(position).getImpression());
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(mContext, SingleItemView.class);
intent.putExtra("hinglish",(dictionarylist.get(position).getHinglish()));
intent.putExtra("english",(dictionarylist.get(position).getEnglish()));
//intent.putExtra("impression",(dictionarylist.get(position).getImpression()));
mContext.startActivity(intent);
}
});
return view;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
dictionarylist.clear();
if (charText.length() == 0) {
dictionarylist.addAll(arraylist);
}
else
{
for (Dictionary dc : arraylist)
if (dc.getHinglish().toLowerCase(Locale.getDefault()).contains(charText))
{
dictionarylist.add(dc);
}
}
notifyDataSetChanged();
}
}
答案 0 :(得分:0)
尝试在添加for循环之前调用arraylist.clear();
:
arraylist.clear();
for (int i = 0; i < hinglish.length; i++)
{
Dictionary dc = new Dictionary(hinglish[i],
english[i]);
arraylist.add(dc);
}