如何从过滤结果列表中打开项目
在listView中,它打开ListView的第一项而不是搜索到的那一项
例如;如果列表视图包含:
物品1
ITEM2
项目3
item4
如果我搜索item3,它将打开item1>>这是listView中的第一个
我试图根据这篇文章修复link 我尝试了一切,但它无法正常工作
MainActivity
public class MainActivity extends Activity {
ImageView m,m2;
ImageButton v1,v2,v3,v4;
private long backPressedTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// TODO Auto-generated method stub
v1 = (ImageButton) findViewById(R.id.imageButton1);
v2 = (ImageButton) findViewById(R.id.imageButton2);
v3=(ImageButton)findViewById(R.id.imageButton3);
v4=(ImageButton)findViewById(R.id.imageButton4);
v1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(MainActivity.this, Search.class);
startActivity(i);
}
});
v2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i2 = new Intent(MainActivity.this, ListViewAnd.class);
startActivity(i2);
}
});
v3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i3=new Intent(MainActivity.this, ImageTextListViewActivity.class);
startActivity(i3);
}
});
v4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i4=new Intent(MainActivity.this, Sharus.class);
startActivity(i4);
}
});
}
@Override
public void onBackPressed() { // to prevent irritating accidental logouts
long t = System.currentTimeMillis();
if (t - backPressedTime > 2000) { // 2 secs
backPressedTime = t;
Toast.makeText(this, " press to Exit ",
Toast.LENGTH_SHORT).show();
} else { // this guy is serious
// clean up
super.onBackPressed(); // bye
}
}
}
OfficesAdapter
public class OfficesAdapter extends ArrayAdapter<Office> implements Filterable{
private ArrayList<Office> items;
private ArrayList<Office> fitems;
LayoutInflater vi;
ArrayFilter mFilter = null;
public OfficesAdapter(Context context,int textViewResourceId, ArrayList<Office> items) {
super(context, textViewResourceId, items);
this.items = items;
this.fitems = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null) {
v = vi.inflate(R.layout.office, null);
}
Office o = fitems.get(position);
if (o != null) {
TextView num = (TextView) v.findViewById(R.id.textNo);
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
if (num != null) {
num.setText(o.getOfficeNo());}
if (tt != null) {
tt.setText(o.getNameAr());}
if(bt != null){
bt.setText(o.getNameEn() );}
}
return v;
}
@Override
public Filter getFilter() {
if (mFilter == null)
mFilter = new ArrayFilter();
return mFilter;
}
@Override
public Office getItem(int position) {
return fitems!=null ? fitems.get(position) : null;
}
@Override
public int getCount() {
return fitems!=null ? fitems.size() : 0;
}
private class ArrayFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// We implement here the filter logic
if (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
results.values = items;
results.count = items.size();
}
else {
// We perform filtering operation
ArrayList<Office> nList = new ArrayList<Office>();
for (Office o : items) {
if (o.getNameEn().toUpperCase().contains(constraint.toString().toUpperCase()) || o.getNameAr().toUpperCase().contains(constraint.toString().toUpperCase()))
nList.add(o);
}
results.values = nList;
results.count = nList.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
// Now we have to inform the adapter about the new list filtered
if (results.count == 0)
notifyDataSetInvalidated();
else {
fitems = (ArrayList<Office>) results.values;
notifyDataSetChanged();
}
}}}
搜索
public class Search extends Activity {
private OfficesDataSource datasource;
Context context;
Activity activity;
ListView lview = null;
EditText filterText = null;
OfficesAdapter adapter=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
context = this;
activity = this;
lview = (ListView) findViewById(R.id.lvOffices);
filterText = (EditText) findViewById(R.id.search_box);
filterText.addTextChangedListener(filterTextWatcher);
datasource = new OfficesDataSource(this);
datasource.open();
ArrayList<Office> values = datasource.getAllOffices("");
adapter = new OfficesAdapter(lview.getContext(), R.layout.office, values);
//send adapter to list view lvItems to view the items
lview.setAdapter(adapter);
lview.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switch( arg2 )
{
case 0: Intent newActivity = new Intent(Search.this, S00.class);
startActivity(newActivity);
break;
case 1: Intent i1 = new Intent(Search.this, S01.class);
startActivity(i1);
break;
//.... etc....
}
}
});
}
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s);
}
};
}