2014-03-25 55 views
0

過濾後,我的listview出現問題,onclick由於過濾之前項目的原始位置而返回錯誤的活動,我將嘗試使用下面的 示例盡我所能:ListView帶過濾器的OnClick返回錯誤的活動

ITEM 1 (pos 0) [opens Activity A] 
ITEM 2 (pos 1) [opens Activity B] 
ITEM 3 (pos 2) [opens Activity C] 
ITEM 4 (pos 3) [opens Activity D] 

時篩選得到的唯一項目3和4我得到這個

ITEM 3 [opens activity A and NOT C] because after filtering it takes pos 0 and not 2 
ITEM 4 [opens activity B and NOT D] because after filtering it takes pos 1 and not 3 

請與下面我的代碼幫助:

public class ListViewAdapterProduct extends BaseAdapter { 

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, ProductProductprod1.class); 
       mContext.startActivity(intent0); 
      } 

      if(position == 1) { 
       Intent intent1 = new Intent(mContext, ProductProductprod2.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(); 
} 

}

回答

0

爲什麼不是你想與Product打開Activity關聯?然後你getView方法裏面,你可以做沿此線的東西...

final Class activityClass = classProductlist.get(position).getActivityClass() 
view.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
       Intent intent = new Intent(mContext, activityClass); 
       mContext.startActivity(intent); 
     }   
    }); 
+0

您好,我想具體活動從ListView項點擊: ProductProductprod1, ProductProductprod2, ProductProductprodn ....等等,這就是爲什麼我使用「if」語句 – TwoStarII