2011-07-21 15 views
0

アンドロイドのリストビューに混乱する問題があります。私はリストをデータベースから取り込み、これらの結果を所定の基準に基づいてフィルタリングする必要があります。デフォルトのリストが表示され、プッシュされた特定のボタンに基づいてリストをフィルタリングできます。このフィルタリングはすべて適切に行われています。私の問題は、リスト内の項目の1つをクリックすると始まります。位置が正しく返されていません(Toast.makeText(this、 "+ position、Toast.LENGTH_SHORT).show();を使用してチェックしています)アンドロイドのリストビューで位置が正しく変更されない

私は関連性があると思われるコードを投稿しました問題。どんな助けも素晴らしいだろう!

@Override 
public void onClick(View v) { 
    switch(v.getId()){ 
    case R.id.search: 
     Intent i = new Intent(v.getContext(), SearchScreen.class); 
     startActivityForResult(i, 0); 
     break; 
    case R.id.directory: 

     break; 
    case R.id.todo: 
     Intent i1 = new Intent(v.getContext(), ToDoScreen.class); 
     startActivityForResult(i1, 0); 

     break; 
    case R.id.favorites: 
     Intent i2 = new Intent(v.getContext(), FavsScreen.class); 
     startActivityForResult(i2, 0); 

     break; 
    case R.id.coupon: 
     Intent i3 = new Intent(v.getContext(), CouponScreen.class); 
     startActivityForResult(i3, 0); 

     break; 
    case R.id.info: 
     Intent i4 = new Intent(v.getContext(), InfoScreen.class); 
     startActivityForResult(i4, 0); 

     break; 
    case R.id.creative: 
     countybar.setVisibility(View.VISIBLE); 
     business.setChamber(0); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.downtown: 
     countybar.setVisibility(View.GONE); 
     business.setChamber(1); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.chamber: 
     countybar.setVisibility(View.VISIBLE); 
     business.setChamber(2); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.all: 
     business.setCounty(0); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.cayuga: 
     business.setCounty(1); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.cortland: 
     business.setCounty(2); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.herkimer: 
     business.setCounty(3); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.jefferson: 
     business.setCounty(4); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.lewis: 
     business.setCounty(5); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.madison: 
     business.setCounty(6); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.monroe: 
     business.setCounty(7); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.oneida: 
     business.setCounty(8); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.onondaga: 
     business.setCounty(9); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.oswego: 
     business.setCounty(10); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.seneca: 
     business.setCounty(11); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.lawrence: 
     business.setCounty(12); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.tompkins: 
     business.setCounty(13); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 
    case R.id.yates: 
     business.setCounty(14); 
     category = business.filterCategoryString(); 
     adap.notifyDataSetChanged(); 

     break; 



    } 



} 




@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
// TODO Auto-generated method stub 
    super.onListItemClick(l, v, position, id); 
    Toast.makeText(this, category.elementAt(position) + position + " onListItemClick", Toast.LENGTH_SHORT).show(); 
    business.setCategory(category.elementAt(position)); 
} 

public static class EfficientAdapter extends BaseAdapter implements Filterable { 
private LayoutInflater mInflater; 
private Context context; 

public EfficientAdapter(Context context) { 
// Cache the LayoutInflate to avoid asking for a new one each time. 
mInflater = LayoutInflater.from(context); 
this.context = context; 
} 

/** 
* Make a view to hold each row. 
* 
* @see android.widget.ListAdapter#getView(int, android.view.View, 
*  android.view.ViewGroup) 
*/ 
public View getView(final int position, View convertView, ViewGroup parent) { 
// A ViewHolder keeps references to children views to avoid 
// unneccessary calls 
// to findViewById() on each row. 
    ViewHolder holder; 

// When convertView is not null, we can reuse it directly, there is 
// no need 
// to reinflate it. We only inflate a new View when the convertView 
// supplied 
// by ListView is null. 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.list_items, null); 

// Creates a ViewHolder and store references to the two children 
// views 
// we want to bind data to. 
     holder = new ViewHolder(); 
     holder.textLine = (TextView) convertView.findViewById(R.id.textLine); 
     holder.iconLine = (ImageView) convertView.findViewById(R.id.rightarrow); 


     convertView.setOnClickListener(new OnClickListener() { 
      private int pos = position; 

      @Override 
      public void onClick(View v) { 
       Toast.makeText(context, category.elementAt(pos) + pos + " On Click", Toast.LENGTH_SHORT).show(); 
       business.setCategory(category.elementAt(pos)); 
       Intent intent = new Intent(v.getContext(), ListingScreen.class); 
       context.startActivity(intent); 
      } 
     }); 


     convertView.setTag(holder); 
    } else { 
// Get the ViewHolder back to get fast access to the TextView 
// and the ImageView. 
     holder = (ViewHolder) convertView.getTag(); 
    } 

// Bind the data efficiently with the holder. 
    holder.textLine.setText(category.elementAt(position)); 

    return convertView; 
} 

static class ViewHolder { 
    TextView textLine; 
    ImageView iconLine; 
    Button buttonLine; 
} 

@Override 
public android.widget.Filter getFilter() { 
// TODO Auto-generated method stub 
    return null; 
} 

@Override 
public long getItemId(int position) { 
// TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public int getCount() { 
// TODO Auto-generated method stub 
    return category.size(); 
} 

@Override 
public Object getItem(int position) { 
// TODO Auto-generated method stub 
    return category.elementAt(position); 
} 

答えて

1

式を利用します。

wantedPos = position - listView.getFirstVisiblePosition() 

;)

+0

私は、リストから項目をクリックすると、問題は、それが上で、getViewメソッド内のonClickメソッドに応答していますさListViewオブジェクトへのアクセス権がありません。 – coder

+1

listView.setOnItemClickedListener()をオーバーライドします。アダプターにはありません。 –

関連する問題