我在android中的listview遇到了一个令人困惑的问题。我从数据库中填充了我的列表,这些结果需要根据给定的条件进行过滤。显示默认列表,然后可以根据推送的特定按钮过滤列表。所有这些过滤都正常进行。当我点击列表中的一个项目时,我的问题就出现了。该位置未正确返回(我通过使用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);
}
答案 0 :(得分:1)
使用公式:
wantedPos = position - listView.getFirstVisiblePosition()
)