这是一个常见问题,但已解决的问题不适用于我的代码,因此我想粘贴自己的代码,因为我注意到Android开发有100万种方法来实现相同的结果,这有点困难,但无论如何...
每当我过滤列表视图时,过滤器之后都不会维护列表视图项的位置,过滤后的项将从0开始分配一个新位置,因此将位置0的原始列表视图项替换为该位置上的新归档项。
例如:listview = aONE [0], aTWO [1], aTHERE[2], bONE [3], b TWO[4], bTHREE [5].
如果输入“ b”,则新列表为bONE [0], bTWO [1], bTHREE [2] and NOT bONE [3], bTWO [4], bTHREE [5],
下面是我的代码:
public class ListViewAdapterProduct extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<ClassProductList> classProductlist = null;
private ArrayList<ClassProductList> arraylist;
public ListViewAdapterProduct(Context context, List<ClassProductList> classProductlist) {
mContext = context;
this.classProductlist = classProductlist;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<ClassProductList>();
this.arraylist.addAll(classProductlist);
}
public class ViewHolder {
TextView title;
TextView description;
ImageView photo;
}
@Override
public int getCount() {
return classProductlist.size();
}
@Override
public ClassProductList getItem(int position) {
return classProductlist.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.list_view_single_row, null);
holder.title = (TextView) view.findViewById(R.id.textLarge);
holder.description = (TextView) view.findViewById(R.id.textSmall);
holder.photo= (ImageView) view.findViewById(R.id.imageThumb);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.title.setText(classProductlist.get(position).getTitle());
holder.description.setText(classProductlist.get(position).getDescription());
holder.photo.setImageResource(classProductlist.get(position).getImage());
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(position == 0) {
Intent intent0 = new Intent(mContext, ProductMyactivity1.class);
mContext.startActivity(intent0);
}
if(position == 1) {
Intent intent1 = new Intent(mContext, ProductMyactivity2.class);
mContext.startActivity(intent1);
}
}
});
return view;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
classProductlist.clear();
if (charText.length() == 0) {
classProductlist.addAll(arraylist);
}
else
{
for (ClassProductList Product : arraylist)
{
if (Product.getTitle().toLowerCase(Locale.getDefault()).contains(charText))
{
classProductlist.add(Product);
}
}
}
notifyDataSetChanged();
}
}
谢谢
最佳答案
以下代码解决了我在ListViewAdapter中的问题
// Listen for ListView Item Click
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Send single item click data to SingleItemView Class
String mytitle=holder.listContent.getText().toString();
if(mytitle.equals("murakoze")) {
// Play song
mp.reset();// stops any current playing song
mp = MediaPlayer.create(mContext, R.raw.murakoze);// create's
// new
// mediaplayer
// with
// song.
mp.start(); // starting mediaplayer
}
}
});
return view;
}