我有一个listview,其中自定义行是从自定义BaseAdaptor填充的。点击任意一行,我打开一个带有片段的新活动。在我将过滤器功能添加到此列表之前,一切正常。当我搜索列表然后单击一个项目时,它不会打开与过滤结果相关联的活动。它会打开与原始列表中该位置的项目相关的活动。
EG。 - 原始列表:AA,BA,CC,DA,ED,FF
搜索:'A'过滤结果:AA,BA,DA
但是当我点击项目DA时,它会打开CC的活动。极度刺激。我在适配器上调用了notifyDataSetChanged()。
主要活动
public class MainActivity_list extends FragmentActivity
implements HeadlinesFragment.OnHeadlineSelectedListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
// Check whether the activity is using the layout version with
// the fragment_container FrameLayout. If so, we must add the first fragment
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create an instance of ExampleFragment
HeadlinesFragment firstFragment = new HeadlinesFragment();
// In case this activity was started with special instructions from an Intent,
// pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
}
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Capture the article fragment from the activity layout
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
} else {
// If the frag is not available, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
标题片段中的
// The container Activity must implement this interface so the frag can deliver messages
public interface OnHeadlineSelectedListener {
/** Called by HeadlinesFragment when a list item is selected */
public void onArticleSelected(int position);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Notify the parent activity of selected item
mCallback.onArticleSelected(position);
// Set the item as checked to be highlighted when in two-pane layout
}
自定义适配器
public class CustomAdapter extends BaseAdapter implements Filterable {
Context context;
ArrayList<RowItem> rowItem;
ArrayList<RowItem> mStringFilterList;
ValueFilter valueFilter;
CustomAdapter(Context context, ArrayList<RowItem> rowItem) {
this.context = context;
this.rowItem = rowItem;
mStringFilterList = rowItem;
}
@Override
public int getCount() {
return rowItem.size();
}
@Override
public Object getItem(int position) {
return rowItem.get(position);
}
@Override
public long getItemId(int position) {
return rowItem.indexOf(getItem(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.mylist, null);
}
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
TextView txtTitle = (TextView) convertView.findViewById(R.id.item);
RowItem row_pos = rowItem.get(position);
// setting the image resource and title
txtTitle.setText(row_pos.getTitle());
Picasso
.with(context)
.load(Ipsum.url[position])
.fit() // will explain later
.centerCrop()
.into(imgIcon);
return convertView;
}
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<RowItem> filterList = new ArrayList<RowItem>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ( (mStringFilterList.get(i).getTitle().toUpperCase() )
.contains(constraint.toString().toUpperCase())) {
RowItem rowItem = new RowItem(mStringFilterList.get(i)
.getTitle());
filterList.add(rowItem);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
rowItem = (ArrayList<RowItem>) results.values;
notifyDataSetChanged();
}
}
答案 0 :(得分:0)
问题是过滤数据集中的位置指向原始数据集中的不同对象。如果您不想更改主逻辑,您可以做的是将onArticleSelected
更改为实例RowItem
而不是position
,并将onListItem更改为
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Notify the parent activity of selected item
mCallback.onArticleSelected((RowItem)l.getItemAtPostion(position));
// Set the item as checked to be highlighted when in two-pane layout
}
这将检索传递正确对象到您的活动
答案 1 :(得分:0)
对于那些使用public void onItemClick(AdapterView父视图,View视图,int位置,长ID)的人
private void setUpAdapter()
{
adapter2 = new CustomAdapter2(kafani, getApplicationContext());
listView.setAdapter(adapter2);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("C pozicija ",""+ position) ;
Log.e("C id",""+id) ;
// Kaf kafana = (Kaf) listView.getItemAtPosition(position);
Kaf kafana = (Kaf) kafani.get((int) id); // where kafani is ORIGINAL LIST, so I use the id
String pozicija = String.valueOf(id);
// String pozicija = String.valueOf(position);
Intent myIntent = new Intent(IndiecZaKaf.this, KafDetails.class);
myIntent.putExtra("parent", "A");
myIntent.putExtra("pozicija", pozicija);
myIntent.putExtra("imek",kafana.getImek());
myIntent.putExtra("adresa",kafana.getAdresa());
myIntent.putExtra("tel",kafana.getTelBroj());
startActivity(myIntent);
}
});