【发布时间】:2016-08-06 06:39:13
【问题描述】:
我想以这样一种方式过滤 listView,即当用户在 listView 项目中出现的 editText 中键入单词/字符串时...该项目应该被过滤...。在下面的代码中,搜索从项目的初始字符......例如:items:sachin,dhoni,yuvraj......如果我搜索 v......搜索过滤器应该显示我 yuvraj......
这是我的代码:
package com.example.acer.aartisangrah;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class first extends AppCompatActivity {
EditText editText;
private ListView listView;
private String[] names = {"प्रार्थना",
"सुखकर्ता दु:खहर्ता",
"गजानना श्रीगणराया",
"येई हो विठ्ठले",
"लवथवती विक्राळा",
"दुर्गे दुर्घट भारी",
"आरती ज्ञानराजा",
"हे भोळ्या शंकरा",
"ओवाळू आरती",
"मंगेशा महारूद्रा",
"आला गणराया",
"शेंदुर लाल चढायो",
"एकदंता",
"कर्पूर गौरा",
"हे गजवधना",
"प्रथमेश्वरा",
"जयदेवा हे साईनाथा",
"श्री सत्यनारायणाची आरती",
"श्री महालक्ष्मीची आरती",
"ॐ जय लक्ष्मी माता",
"आरती संतोषी माता की",
"धन्य धन्य हो प्रदक्षिणा",
"सुखंकर्ता की दु:खहर्ता",
"ॐ जय जगदीश",
"जय श्री विश्वकर्मा",
"हे गौरी गजानन",
"घालीन लोटांगण"};
private int imageid[] = {R.drawable.ganeshaji,
R.drawable.ganeshaji,
R.drawable.ganpati,
R.drawable.vittal,
R.drawable.shivji,
R.drawable.durgamaa,
R.drawable.ganeshaji,
R.drawable.shivji,
R.drawable.krishna,
R.drawable.ganeshaji,
R.drawable.ganeshaji,
R.drawable.ganeshaji,
R.drawable.ganeshaji,
R.drawable.ganeshaji,
R.drawable.ganeshaji,
R.drawable.ganeshaji,
R.drawable.saibaba,
R.drawable.satyanarayanadev,
R.drawable.laxmimaa,
R.drawable.laxmimaa,
R.drawable.santoshimaa,
R.drawable.ganeshaji,
R.drawable.ganeshaji,
R.drawable.ganeshaji,
R.drawable.ganpati,
R.drawable.ganeshaji,
R.drawable.ganeshaji};
int textlength = 0;
ArrayList<String> text_sort = new ArrayList<String>();
ArrayList<Integer> image_sort = new ArrayList<Integer>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
ActionBar actionBar=getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.mipmap.ic_launcher);
listView = (ListView) findViewById(R.id.listView);
editText = (EditText) findViewById(R.id.inputSearch);
listView.setAdapter(new MyCustomAdapter(names, imageid));
editText.addTextChangedListener(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) {
textlength = editText.getText().length();
text_sort.clear();
image_sort.clear();
for (int i = 0; i < names.length; i++) {
if (textlength <= names[i].length()) {
if (editText.getText().toString().equalsIgnoreCase((String)names[i].subSequence(0,textlength))) {
text_sort.add(names[i]);
image_sort.add(imageid[i]);
}
}
}
listView.setAdapter(new MyCustomAdapter(text_sort, image_sort));
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.textViewName);
String strSelectedString = tv.getText().toString();
for (int selectedPosition = 0; selectedPosition < names.length; selectedPosition++) {
if (names[selectedPosition].equalsIgnoreCase(strSelectedString)) {
moveToNextActivity(selectedPosition);
break;
}
}
}
});
}
public void moveToNextActivity(int position) {
if (position == 0) {
Intent myIntent = new Intent(getApplicationContext(), akalp.class);
startActivityForResult(myIntent, 0);
}
if (position == 1) {
Intent myIntent = new Intent(getApplicationContext(), Sukharta.class);
startActivityForResult(myIntent, 0);
}
if (position == 2) {
Intent myIntent = new Intent(getApplicationContext(), gajananashriganraya.class);
startActivityForResult(myIntent, 0);
}
if (position == 3) {
Intent myIntent = new Intent(getApplicationContext(), yehihovittale.class);
startActivityForResult(myIntent, 0);
}
if (position == 4) {
Intent myIntent = new Intent(getApplicationContext(), lavathavthi.class);
startActivityForResult(myIntent, 0);
}
if (position == 5) {
Intent myIntent = new Intent(getApplicationContext(), durgedurgat.class);
startActivityForResult(myIntent, 0);
}
if (position == 6) {
Intent myIntent = new Intent(getApplicationContext(), nyanraja.class);
startActivityForResult(myIntent, 0);
}
if (position == 7) {
Intent myIntent = new Intent(getApplicationContext(), bolya.class);
startActivityForResult(myIntent, 0);
}
if (position == 8) {
Intent myIntent = new Intent(getApplicationContext(), vovalo.class);
startActivityForResult(myIntent, 0);
}
if (position == 9) {
Intent myIntent = new Intent(getApplicationContext(), mangesha.class);
startActivityForResult(myIntent, 0);
}
if (position == 10) {
Intent myIntent = new Intent(getApplicationContext(), alaganraya.class);
startActivityForResult(myIntent, 0);
}
if (position == 11) {
Intent myIntent = new Intent(getApplicationContext(), shendorlal.class);
startActivityForResult(myIntent, 0);
}
if (position == 12) {
Intent myIntent = new Intent(getApplicationContext(), ekdanta.class);
startActivityForResult(myIntent, 0);
}
if (position == 13) {
Intent myIntent = new Intent(getApplicationContext(), karpurgaura.class);
startActivityForResult(myIntent, 0);
}
if (position == 14) {
Intent myIntent = new Intent(getApplicationContext(), heygajavadana.class);
startActivityForResult(myIntent, 0);
}
if (position == 15) {
Intent myIntent = new Intent(getApplicationContext(), prathmeshvara.class);
startActivityForResult(myIntent, 0);
}
if (position == 16) {
Intent myIntent = new Intent(getApplicationContext(), sainatha.class);
startActivityForResult(myIntent, 0);
}
if (position == 17) {
Intent myIntent = new Intent(getApplicationContext(), satyanarayan.class);
startActivityForResult(myIntent, 0);
}
if (position == 18) {
Intent myIntent = new Intent(getApplicationContext(), jaidevijaidevi.class);
startActivityForResult(myIntent, 0);
}
if (position == 19) {
Intent myIntent = new Intent(getApplicationContext(), omjaimahalakshmi.class);
startActivityForResult(myIntent, 0);
}
if (position == 20) {
Intent myIntent = new Intent(getApplicationContext(), maitohaarti.class);
startActivityForResult(myIntent, 0);
}
if (position == 21) {
Intent myIntent = new Intent(getApplicationContext(), danyadanyaho.class);
startActivityForResult(myIntent, 0);
}
if (position == 22) {
Intent myIntent = new Intent(getApplicationContext(), sukhartakidukharta.class);
startActivityForResult(myIntent, 0);
}
if (position == 23) {
Intent myIntent = new Intent(getApplicationContext(), omjaijagdish.class);
startActivityForResult(myIntent, 0);
}
if (position == 24) {
Intent myIntent = new Intent(getApplicationContext(), vishwakarma.class);
startActivityForResult(myIntent, 0);
}
if (position == 25) {
Intent myIntent = new Intent(getApplicationContext(), heygaurigajanana.class);
startActivityForResult(myIntent, 0);
}
if (position == 26) {
Intent myIntent = new Intent(getApplicationContext(), ghalinlotangan.class);
startActivityForResult(myIntent, 0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id==R.id.action_about){
Intent myIntent = new Intent(getApplicationContext(), aboutusActivity.class);
startActivityForResult(myIntent, 0);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if(editText.getText().toString().length()>0){
editText.setText("");
listView.setSelection(0);}
else
finish();
}
class MyCustomAdapter extends BaseAdapter {
String[] data_text;
int[] data_image;
MyCustomAdapter() {
}
MyCustomAdapter(String[] names, int[] imageid) {
data_text = names;
data_image = imageid;
}
MyCustomAdapter(ArrayList<String> names, ArrayList<Integer> imageid) {
data_text = new String[names.size()];
data_image = new int[imageid.size()];
for (int i = 0; i < names.size(); i++) {
data_text[i] = names.get(i);
data_image[i] = imageid.get(i);
}
}
public int getCount() {
return data_text.length;
}
public String getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.list_layout, parent, false);
TextView textview = (TextView) row.findViewById(R.id.textViewName);
ImageView imageview = (ImageView) row.findViewById(R.id.imageView);
textview.setText(data_text[position]);
imageview.setImageResource(data_image[position]);
return (row);
}
}
}
【问题讨论】:
-
使用this通用适配器
标签: java android listview listview-filter