我创建了一个应用程序,显示包含可点击项目的列表视图。我有一个编辑文本来进行搜索。一切都很好,但是当我搜索一个项目时,应用程序给我一个新列表,问题是当我点击这个新列表项目上的任何项目时,它将我重定向到旧列表中项目的链接。这是我的代码: - 对于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;
}}
对于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));
}}
任何帮助都将得到您的赞赏。谢谢。如果您需要任何澄清,请告诉我们
答案 0 :(得分:0)
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();
}
}
*******************您的主要活动**********************
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);
}} });}
}