如何实现自定义列表视图的搜索功能

时间:2016-08-02 17:06:00

标签: java android listview

我已将搜索栏放在我的应用中,但我无法搜索listview项目... edittext需要输入但不进行搜索。

这是我的代码:

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

public class first extends AppCompatActivity {
EditText inputSearch;
private ListView listView;
private String[] names = {"प्रार्थना", "सुखकर्ता दु:खहर्ता", "गजानना श्रीगणराया", "येई हो विठ्ठले", "लवथवती विक्राळा", "दुर्गे दुर्घट भारी", "आरती ज्ञानराजा",
        "हे भोळ्या शंकरा", "ओवाळू", "मंगेशा महारूद्रा", "आला गणराया", "शेंदुर लाल चढायो", "एकदंता", "कर्पूर गौरा", "हे गजवधना", "प्रथमेश्वरा", "जयदेवा हे  साईनाथा",
        "श्री सत्यनारायणाची आरती", "श्री महालक्ष्मीची आरती", "ॐ जय लक्ष्मी माता", "आरती संतोषी माता की", "धन्य धन्य हो प्रदक्षिणा", "सुखंकर्ता की दु:खहर्ता",
        "ॐ जय जगदीश", "हे गौरी गजानन", "घालीन लोटांगण"};
private Integer imageid[] = {R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji,
        R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji,
        R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji,
        R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);
    final CustomList customList = new CustomList(this, names, imageid);
    listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(customList);
    inputSearch = (EditText) findViewById(R.id.inputSearch);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (position == 0) {
                Intent myIntent = new Intent(view.getContext(), akalp.class);
                startActivityForResult(myIntent, 0);
            }

            if (position == 1) {
                Intent myIntent = new Intent(view.getContext(), Sukharta.class);
                startActivityForResult(myIntent, 0);
            }

            if (position == 2) {
                Intent myIntent = new Intent(view.getContext(), gajananashriganraya.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 3) {
                Intent myIntent = new Intent(view.getContext(), yehihovittale.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 4) {
                Intent myIntent = new Intent(view.getContext(), lavathavthi.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 5) {
                Intent myIntent = new Intent(view.getContext(), durgedurgat.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 6) {
                Intent myIntent = new Intent(view.getContext(), nyanraja.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 7) {
                Intent myIntent = new Intent(view.getContext(), bolya.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 8) {
                Intent myIntent = new Intent(view.getContext(), vovalo.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 9) {
                Intent myIntent = new Intent(view.getContext(), mangesha.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 10) {
                Intent myIntent = new Intent(view.getContext(), alaganraya.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 11) {
                Intent myIntent = new Intent(view.getContext(), shendorlal.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 12) {
                Intent myIntent = new Intent(view.getContext(), ekdanta.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 13) {
                Intent myIntent = new Intent(view.getContext(), karpurgaura.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 14) {
                Intent myIntent = new Intent(view.getContext(), heygajavadana.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 15) {
                Intent myIntent = new Intent(view.getContext(), prathmeshvara.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 16) {
                Intent myIntent = new Intent(view.getContext(), sainatha.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 17) {
                Intent myIntent = new Intent(view.getContext(), satyanarayan.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 18) {
                Intent myIntent = new Intent(view.getContext(), jaidevijaidevi.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 19) {
                Intent myIntent = new Intent(view.getContext(),omjaimahalakshmi.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 20) {
                Intent myIntent = new Intent(view.getContext(), maitohaarti.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 21) {
                Intent myIntent = new Intent(view.getContext(), danyadanyaho.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 22) {
                Intent myIntent = new Intent(view.getContext(), sukhartakidukharta.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 23) {
                Intent myIntent = new Intent(view.getContext(), omjaijagdish.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 24) {
                Intent myIntent = new Intent(view.getContext(), heygaurigajanana.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 25) {
                Intent myIntent = new Intent(view.getContext(), ghalinlotangan.class);
                startActivityForResult(myIntent, 0);
            }
        }
    });
 }
}

这里是CustomList.java的代码

public class CustomList extends ArrayAdapter<String> {
    private String[] names;
    private Integer[] imageid;
    private Activity context;

    public CustomList(Activity context, String[] names, Integer[] imageid) {
        super(context,R.layout.list_layout,names);
        this.context = context;
        this.names = names;
        this.imageid = imageid;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View listViewItem = inflater.inflate(R.layout.list_layout, null, true);
        TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
        ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView);
        textViewName.setText(names[position]);
        image.setImageResource(imageid[position]);
        return listViewItem;
     }
}

1 个答案:

答案 0 :(得分:0)

我想它不会进行搜索,因为你没有对EditText做任何事情。你能做什么:

  1. 在OnCreate()中设置“文本已更改”侦听器(EditText的addTextChangedListener()方法)
  2. 在onTextChanged()中,您可以获取输入
  3. 上的文本
  4. 相应地更新数据(例如names.contains()方法)
  5. 将数据提供给ListView并调用notifyDataSetChanged()。或者只是使用新数据重新创建列表。