过滤listview项后,该项不会打开相应的活动

时间:2016-08-03 11:30:09

标签: java android listview search searchview

我有一个列表视图,项目对应不同的活动。在搜索所需的项目并点击它之后,位置0的项目被打开而不是与项目相对应的活动... EG:item1     ITEM2     项目3 搜索:item3,点击搜索的项目3 问题:它将打开与item1对应的活动。

这是我的代码:

package com.example.acer.aartisangrah;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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.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};
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);
    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) {
            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);
            }
        }
    });

}
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);
    }
}
}

3 个答案:

答案 0 :(得分:1)

检查以下代码并为新屏幕创建一种方法,如下所示,

  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.lenght; selectedPosition++)
        {
            if(names[selectedPosition].equalsIgnoreCase(strSelectedString))
            {
                moveToNextActivity(selectedPosition);
                break;
            }
        }    

        }
    });

    public void moveToNextActivity(int position)
    {
        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);
        }
    }

答案 1 :(得分:0)

你在这里做的是根据点击的听众位置开始新的活动。当用户搜索特定位置时,会发生什么变化,并且您的程序将根据原始列表视图的位置开始活动。而不是使用if(position == x)使用其他将保持相同的内容,即使列表视图发生更改。您可以使用代码

获取元素详细信息
  Object o = lv1.getItemAtPosition(position);
  ItemDetails obj_itemDetails = (ItemDetails)o;
  String name =obj_itemDetails.getName();

然后在开始新活动之前使用名称进行比较,如

if(name.equals(sukharta)){
Intent myIntent = new Intent(view.getContext(), Sukharta.class);
startActivityForResult(myIntent, 0);
}

我希望这会有所帮助。 samajal nahi tar parat wichara

答案 2 :(得分:0)

尝试

 Object o = lv1.getItemAtPosition(position);
 ItemDetails obj_itemDetails = (ItemDetails)o;
 String name =obj_itemDetails.getName();
 if(name.equals(sukharta)){
 Intent myIntent = new Intent(view.getContext(), Sukharta.class);
 startActivityForResult(myIntent, 0);
 }