问题描述
这是我的MainActivity.java,其中我在item上定义onclicklistener.在这里,我总是在过滤后得到错误的item值,如果listview中有10个item,那么在过滤后,如果我得到3个item,正确的位置是3 ,6,9但我得到0,1,2.
Here is my MainActivity.java where i define onclicklistener on item.here always i am getting wrong value of item after filter,if there is 10 item in listview then after filter if i get 3 item and the correct position is like 3,6,9 but i am getting 0,1,2.
请帮助我.
package com.focusmedica.maadiabetes;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
private MainAdapter adapter;
private static ListView lvFiltered;
private static ArrayList<DataModel> data=new ArrayList<>();;
MyDatabase handler;
private EditText etSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvFiltered =(ListView)findViewById(R.id.lvFiltered);
etSearch = (EditText)findViewById(R.id.etSearch);
etSearch.setText("");
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
handler=new MyDatabase(this);
data=handler.getChapterDetails();
adapter = new MainAdapter(this,data);
lvFiltered.setAdapter(adapter);
lvFiltered.setTextFilterEnabled(true);
lvFiltered.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, SubActivity.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
if(cs.length()>0) {
etSearch.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.delete, 0);
}else{
etSearch.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
}
// When user changed the Text
adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
etSearch.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(etSearch.getCompoundDrawables()[2]!=null){
if(event.getX() >= (etSearch.getRight()- etSearch.getLeft() - etSearch.getCompoundDrawables()[2].getBounds().width())) {
etSearch.setText("");
}
}
}
return false;
}
});
}
}
这是适配器类
package com.focusmedica.maadiabetes;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by windev on 8/2/2016.
*/
public class MainAdapter extends BaseAdapter implements Filterable {
ArrayList<DataModel> dataSet;
Context context;
DataModel content;
public ArrayList<DataModel> orig;
public MainAdapter(Context context, ArrayList dataSet) {
super();
this.context = context;
this.dataSet = dataSet;
}
@Override
public int getCount() {
return dataSet.size();
}
@Override
public Object getItem(int position) {
return dataSet.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder {
private TextView tvChapterName;
private ImageView ivChapterIcon;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
final ViewHolder viewHolder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.card_layout, null);
viewHolder = new ViewHolder();
viewHolder.tvChapterName = (TextView) view.findViewById(R.id.tvChapterName);
viewHolder.ivChapterIcon=(ImageView)view.findViewById(R.id.ivChapterIcon);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
content = dataSet.get(position);
viewHolder.tvChapterName.setText(content.getChapterName());
viewHolder.ivChapterIcon.setImageResource(R.drawable.caticon);
return view;
}
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<DataModel> results = new ArrayList<>();
final ArrayList<DataModel> ids = new ArrayList<>();
if (orig == null)
orig = dataSet;
if (constraint != null) {
if (orig != null && orig.size() > 0) {
for (final DataModel g : orig) {
if (g.getChapterName().toLowerCase().contains(constraint.toString())||
g.getChapterName().toUpperCase().contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
dataSet = (ArrayList<DataModel>) results.values;
notifyDataSetChanged();
}
};
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
推荐答案
要获取过滤列表中某项的旧"位置(相当于该项ID):
To get the "old" position (which is equivalent to the item ID) for an item in the filtered list:
@Override
public long getItemId(int position) {
int itemID;
// orig will be null only if we haven't filtered yet:
if (orig == null)
{
itemID = position;
}
else
{
itemID = orig.indexOf(dataSet.get(position));
}
return itemID;
}
现在将此方法与OnItemClickListener
一起使用:
Now use this method with the OnItemClickListener
:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, SubActivity.class);
intent.putExtra("position", adapter.getItemId(position) );
startActivity(intent);
}
注意:如果您需要使用notifyDatasetChanged()
,最好也重写此方法:
Note: if you ever need to use notifyDatasetChanged()
it might be a good idea to override this method as well:
@Override
public void notifyDatasetChanged()
{
// either this or: orig = dataSet;
orig = null;
super.notifyDatasetChanged();
}
这篇关于过滤器listView后,我无法获取项目在android中的实际位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!