ListView控件只记得过滤后旧列表的位置 [英] ListView remember only the position of old list after Filtering
问题描述
我创建了一个应用程序,显示具有可点击的项目列表视图。我有一个编辑的文本进行搜索。一切是伟大的工作,但是当IM寻找一个项目,应用程序给我一个新的名单中,probleme是当上thisnew列表项的任何项目IM点击它重定向我老list.here项目的链接是我的code:
- 对于ReaderListAdapter.java:
进口的java.util.List;
进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.animation.Animation;
进口android.view.animation.AnimationUtils;
进口android.widget.BaseAdapter;
进口android.widget.ImageView;
进口android.widget.TextView; 公共类ReaderListAdapter延伸BaseAdapter {
私人列表<读者与GT; listReader = NULL;
LayoutInflater layoutInflater;
上下文语境;
私人INT lastPosition = -1;// CONSTRUCTEUR
公共ReaderListAdapter(上下文的背景下,列表与LT;读卡器> listFood){
this.listReader = listFood;
layoutInflater = LayoutInflater.from(上下文);
this.listReader = listFood;
this.context =背景;
}
@覆盖
公众诠释的getCount(){
// TODO自动生成方法存根
返回listReader.size();
} @覆盖
公共对象的getItem(INT位置){
// TODO自动生成方法存根
返回listReader.get(位置);
}@覆盖
众长getItemId(INT为arg0){
// TODO自动生成方法存根
返回将arg0;
}静态类ViewHolder {
TextView的nomView;
TextView的priceView;
ImageView的pictureView;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
ViewHolder持有人;如果(convertView == NULL){
convertView = layoutInflater.inflate(R.layout.reader_row,NULL);
持有人=新ViewHolder();
//初始化DES VUES
holder.nomView =(TextView中)convertView.findViewById(R.id.name);
holder.priceView =(TextView中)convertView.findViewById(R.id.price);
holder.pictureView =(ImageView的)convertView
.findViewById(R.id.picture); convertView.setTag(保持器);
}其他{
支架=(ViewHolder)convertView.getTag();
}
// affchier LES最近搜索convenablement丹斯leurs位置
holder.nomView.setText(listReader.get(位置).getName());
holder.priceView.setText(将String.valueOf(listReader.get(位置)
.getPrice()));
holder.pictureView.setBackgroundDrawable(listReader.get(位置).getPicture());//换R.anim.ton_effet
动画动画= AnimationUtils.loadAnimation(背景下,
(位置> lastPosition)? R.anim.up_from_bottom
:R.anim.up_from_bottom);convertView.startAnimation(动画);
lastPosition =位置;
返回convertView;
}}
有关MainActivity.java
进口的java.util.ArrayList;
进口android.app.Activity;
进口android.content.Intent;
进口android.graphics.drawable.Drawable;
进口android.net.Uri;
进口android.os.Bundle;
进口android.text.Editable;
进口android.text.TextWatcher;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.EditText;
进口android.widget.ListView; 公共类MainActivity延伸活动{
的String [] = listNames {现场1,站点2,site3};
INT [] listPrices = {1,2,3};
ArrayList的<读者与GT; listReader;
LV的ListView;
搜索的EditText; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);的setContentView(R.layout.activity_main);
LV =(ListView控件)findViewById(R.id.listReader);
搜索=(EditText上)findViewById(R.id.search);
可绘制[] = listPictures {
getResources()。getDrawable(R.drawable.a1)
getResources()。getDrawable(R.drawable.a2)
。getResources()getDrawable(R.drawable.a3)};listReader =新的ArrayList<读者与GT;();的for(int i = 0; I< listPictures.length;我++){
listReader.add(新读者第(i + 1,listNames [Ⅰ],listPictures [I]
listPrices [I]));
}lv.setAdapter(新ReaderListAdapter(getApplicationContext(),listReader));
search.addTextChangedListener(新TextWatcher(){ @覆盖
公共无效onTextChanged(CharSequence的为arg0,ARG1 INT,INT ARG2,
INT ARG3){
// TODO自动生成方法存根 } @覆盖
公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,
INT ARG2,诠释ARG3){
// TODO自动生成方法存根 } @覆盖
公共无效afterTextChanged(编辑为arg0){
// TODO自动生成方法存根
filtrer();
}});lv.setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的ID)
{
意向意图=新意图(MainActivity.this,WebActivity.class); 开关(位置){
情况下0:
intent.setData(Uri.parse(http://www.google.com));打破; 情况1:
intent.setData(Uri.parse(http://www.yahoo.com));打破; 案例2:
intent.setData(Uri.parse(http://www.quranf.com));打破;
}
如果(意向!= NULL){
startActivity(意向);
}}});} 公共无效filtrer(){
// retourner锚链塔saisie相提并论L'utilisateur
字符串名称= search.getText()的toString()。//créerUNE新式清单当然魁VA contenir拉résultatàafficher
最终的ArrayList<读者与GT; listReaderNew =新的ArrayList<读者与GT;();对(读卡器读卡器:listReader){
//思乐NOM都食品开始比肩锚链塔saisie,ajouter乐!
如果(reader.getName()。与toLowerCase()。toString()方法。包括(名)){
listReaderNew.add(读卡器);
}
}
// vider拉清单当然
lv.setAdapter(NULL);
如果(listReaderNew.size()== 0){
listReaderNew.add(新阅读器(100,PAS D'元素..réessayer!
。getResources()getDrawable(R.drawable.error),0));
}
// ajouter拉清单当然新潮
lv.setAdapter(新ReaderListAdapter(getApplicationContext(),listReaderNew));
}}
任何帮助将是从你pciated AP $ P $。谢谢如果你需要任何澄清PLZ让我知道
公共类ReaderListAdapter延伸BaseAdapter {
私人列表<读者与GT; listReader = NULL;
LayoutInflater layoutInflater;
上下文语境;
私人INT lastPosition = -1;
ArrayList的<读者与GT; ArrayList的;// CONSTRUCTEUR
公共ReaderListAdapter(上下文的背景下,列表与LT;读卡器> listFood){
this.listReader = listFood;
layoutInflater = LayoutInflater.from(上下文);
this.listReader = listFood;
this.context =背景;
this.arrayList = listFood;
}
@覆盖
公众诠释的getCount(){
// TODO自动生成方法存根
返回listReader.size();
} @覆盖
公共对象的getItem(INT位置){
// TODO自动生成方法存根
返回listReader.get(位置);
} @覆盖
众长getItemId(INT为arg0){
// TODO自动生成方法存根
返回将arg0;
} 静态类ViewHolder {
TextView的nomView;
TextView的priceView;
ImageView的pictureView;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
ViewHolder持有人;如果(convertView == NULL){
convertView = layoutInflater.inflate(R.layout.reader_row,NULL);
持有人=新ViewHolder();
//初始化DES VUES
holder.nomView =(TextView中)convertView.findViewById(R.id.name);
holder.priceView =(TextView中)convertView.findViewById(R.id.price);
holder.pictureView =(ImageView的)convertView
.findViewById(R.id.picture);convertView.setTag(保持器);
}其他{
支架=(ViewHolder)convertView.getTag();
}
// affchier LES最近搜索convenablement丹斯leurs位置
holder.nomView.setText(listReader.get(位置).getName());
holder.priceView.setText(将String.valueOf(listReader.get(位置)
.getPrice()));
holder.pictureView.setBackgroundDrawable(listReader.get(位置).getPicture());//换R.anim.ton_effet
动画动画= AnimationUtils.loadAnimation(背景下,
(位置> lastPosition)? R.anim.up_from_bottom
:R.anim.up_from_bottom);convertView.startAnimation(动画);
lastPosition =位置;
返回convertView;
}
公共无效过滤器(字符串charText){
charText = charText.toLowerCase(Locale.getDefault());
listFood.clear();
如果(charText.length()== 0){
listFood.addAll(数组列表);
}其他{
对于(CP读者:数组列表){
如果(cp.getName()。与toLowerCase(Locale.getDefault())
。载有(charText)){
listFood.add(CP);
}
}
}
notifyDataSetChanged();
}
}
*******************您MainActivity **********************
公共类MainActivity延伸活动{
的String [] = listNames {现场1,站点2,site3};
INT [] listPrices = {1,2,3};
ArrayList的<读者与GT; listReader;
LV的ListView;
搜索的EditText; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_main);
LV =(ListView控件)findViewById(R.id.listReader);
搜索=(EditText上)findViewById(R.id.search);
可绘制[] = listPictures {
getResources()。getDrawable(R.drawable.a1)
getResources()。getDrawable(R.drawable.a2)
。getResources()getDrawable(R.drawable.a3)}; listReader =新的ArrayList<读者与GT;(); 的for(int i = 0; I< listPictures.length;我++){
listReader.add(新读者第(i + 1,listNames [Ⅰ],listPictures [I]
listPrices [I]));
}
ReaderListAdapter readerListAdapternew ReaderListAdapter(getApplicationContext(),listReader)
lv.setAdapter(readerListAdapternew);
search.addTextChangedListener(新TextWatcher(){ search.addTextChangedListener(新TextWatcher(){ @覆盖
公共无效onTextChanged(CharSequence的为arg0,ARG1 INT,INT ARG2,
INT ARG3){
// TODO自动生成方法存根 } @覆盖
公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,
INT ARG2,诠释ARG3){
// TODO自动生成方法存根 } @覆盖
公共无效afterTextChanged(编辑为arg0){
字符串文本= search.getText()。的toString()
.toLowerCase(Locale.getDefault());
readerListAdapternew .filter(文本);
}});
lv.setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的ID)
{
意向意图=新意图(MainActivity.this,WebActivity.class); 开关(位置){
情况下0:
intent.setData(Uri.parse(http://www.google.com));打破; 情况1:
intent.setData(Uri.parse(http://www.yahoo.com));打破; 案例2:
intent.setData(Uri.parse(http://www.quranf.com));打破;
}
如果(意向!= NULL){
startActivity(意向);
}}});}
}
i created an app that show a listview with clickable items. i have an edit text to make the search. everything is working great but when im searching for an item, the app give my a new list, the probleme is when im clicking on any item on thisnew list items it redirect me to the link of the item in old list.here is my code: -For ReaderListAdapter.java :
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ReaderListAdapter extends BaseAdapter {
private List<Reader> listReader = null;
LayoutInflater layoutInflater;
Context context;
private int lastPosition = -1;
// constructeur
public ReaderListAdapter(Context context, List<Reader> listFood) {
this.listReader = listFood;
layoutInflater = LayoutInflater.from(context);
this.listReader = listFood;
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listReader.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listReader.get(position);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
static class ViewHolder {
TextView nomView;
TextView priceView;
ImageView pictureView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.reader_row, null);
holder = new ViewHolder();
// initialisation des vues
holder.nomView = (TextView) convertView.findViewById(R.id.name);
holder.priceView = (TextView) convertView.findViewById(R.id.price);
holder.pictureView = (ImageView) convertView
.findViewById(R.id.picture);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// affchier les données convenablement dans leurs positions
holder.nomView.setText(listReader.get(position).getName());
holder.priceView.setText(String.valueOf(listReader.get(position)
.getPrice()));
holder.pictureView.setBackgroundDrawable(listReader.get(position).getPicture());
// changer R.anim.ton_effet
Animation animation = AnimationUtils.loadAnimation(context,
(position > lastPosition) ? R.anim.up_from_bottom
: R.anim.up_from_bottom);
convertView.startAnimation(animation);
lastPosition = position;
return convertView;
}}
For MainActivity.java
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
String[] listNames = { "site1", "site2", "site3"};
int[] listPrices = { 1, 2, 3};
ArrayList<Reader> listReader;
ListView lv;
EditText search;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listReader);
search = (EditText) findViewById(R.id.search);
Drawable[] listPictures = {
getResources().getDrawable(R.drawable.a1),
getResources().getDrawable(R.drawable.a2),
getResources().getDrawable(R.drawable.a3)};
listReader = new ArrayList<Reader>();
for (int i = 0; i < listPictures.length; i++) {
listReader.add(new Reader(i + 1, listNames[i], listPictures[i],
listPrices[i]));
}
lv.setAdapter(new ReaderListAdapter(getApplicationContext(), listReader));
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
filtrer();
} });
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent=new Intent(MainActivity.this,WebActivity.class);
switch (position) {
case 0:
intent.setData(Uri.parse("http://www.google.com")) ;break;
case 1:
intent.setData(Uri.parse("http://www.yahoo.com")) ;break;
case 2:
intent.setData(Uri.parse("http://www.quranf.com")) ;break;
}
if (intent != null) {
startActivity(intent);
}} });}
public void filtrer() {
// retourner la chaine saisie par l'utilisateur
String name = search.getText().toString();
// créer une nouvelle liste qui va contenir la résultat à afficher
final ArrayList<Reader> listReaderNew = new ArrayList<Reader>();
for (Reader reader : listReader) {
// si le nom du food commence par la chaine saisie , ajouter-le !
if (reader.getName().toLowerCase().toString().contains(name)) {
listReaderNew.add(reader);
}
}
// vider la liste
lv.setAdapter(null);
if (listReaderNew.size() == 0) {
listReaderNew.add(new Reader(100, "Pas d'élements.. réessayer !",
getResources().getDrawable(R.drawable.error), 0));
}
// ajouter la nouvelle liste
lv.setAdapter(new ReaderListAdapter(getApplicationContext(), listReaderNew));
}}
Any help would be appreciated from you. thank you If you need any clarification plz let me know
public class ReaderListAdapter extends BaseAdapter {
private List<Reader> listReader = null;
LayoutInflater layoutInflater;
Context context;
private int lastPosition = -1;
ArrayList<Reader> arrayList;
// constructeur
public ReaderListAdapter(Context context, List<Reader> listFood) {
this.listReader = listFood;
layoutInflater = LayoutInflater.from(context);
this.listReader = listFood;
this.context = context;
this.arrayList=listFood;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listReader.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listReader.get(position);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
static class ViewHolder {
TextView nomView;
TextView priceView;
ImageView pictureView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.reader_row, null);
holder = new ViewHolder();
// initialisation des vues
holder.nomView = (TextView) convertView.findViewById(R.id.name);
holder.priceView = (TextView) convertView.findViewById(R.id.price);
holder.pictureView = (ImageView) convertView
.findViewById(R.id.picture);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// affchier les données convenablement dans leurs positions
holder.nomView.setText(listReader.get(position).getName());
holder.priceView.setText(String.valueOf(listReader.get(position)
.getPrice()));
holder.pictureView.setBackgroundDrawable(listReader.get(position).getPicture());
// changer R.anim.ton_effet
Animation animation = AnimationUtils.loadAnimation(context,
(position > lastPosition) ? R.anim.up_from_bottom
: R.anim.up_from_bottom);
convertView.startAnimation(animation);
lastPosition = position;
return convertView;
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
listFood.clear();
if (charText.length() == 0) {
listFood.addAll(arraylist);
} else {
for (Reader cp : arraylist) {
if (cp.getName().toLowerCase(Locale.getDefault())
.contains(charText)) {
listFood.add(cp);
}
}
}
notifyDataSetChanged();
}
}
*******************your MainActivity**********************
public class MainActivity extends Activity {
String[] listNames = { "site1", "site2", "site3"};
int[] listPrices = { 1, 2, 3};
ArrayList<Reader> listReader;
ListView lv;
EditText search;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listReader);
search = (EditText) findViewById(R.id.search);
Drawable[] listPictures = {
getResources().getDrawable(R.drawable.a1),
getResources().getDrawable(R.drawable.a2),
getResources().getDrawable(R.drawable.a3)};
listReader = new ArrayList<Reader>();
for (int i = 0; i < listPictures.length; i++) {
listReader.add(new Reader(i + 1, listNames[i], listPictures[i],
listPrices[i]));
}
ReaderListAdapter readerListAdapternew ReaderListAdapter(getApplicationContext(), listReader)
lv.setAdapter(readerListAdapternew);
search.addTextChangedListener(new TextWatcher() {
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
String text = search.getText().toString()
.toLowerCase(Locale.getDefault());
readerListAdapternew .filter(text);
} });
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent=new Intent(MainActivity.this,WebActivity.class);
switch (position) {
case 0:
intent.setData(Uri.parse("http://www.google.com")) ;break;
case 1:
intent.setData(Uri.parse("http://www.yahoo.com")) ;break;
case 2:
intent.setData(Uri.parse("http://www.quranf.com")) ;break;
}
if (intent != null) {
startActivity(intent);
}} });}
}
这篇关于ListView控件只记得过滤后旧列表的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!