Android搜索工作不正常

时间:2014-02-24 15:25:26

标签: java android eclipse listview search

以前工作过,不记得我改变了什么,差不多一年过去了,过滤器正在工作,当输入正确的提示时,但是当点击它时它给出了所有这些的相同答案!对不起长代码我只是初学者并且不知道它最重要的是什么。

XML代码:

<EditText
    android:id="@+id/EditText01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search" >
</EditText>

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
</ListView>

Java代码:

 public class CribActivity extends Activity {

    private ListView lv1;

    private EditText et;

    private String lv_arr[]={
            "Intelsat 18 180° в.д.",
            "Sony 19 169° в.д",
            "Megapack 8 166° в.д."

    };
    private ArrayList<String> array_sort= new ArrayList<String>();
    int textlength=0;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        lv1 = (ListView) findViewById(R.id.listView);
        et = (EditText) findViewById(R.id.EditText01);
        // Получим идентификатор ListView
        lv1 = (ListView)findViewById(R.id.listView);
        //устанавливаем массив в ListView
        lv1.setAdapter(
                new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
        lv1.setTextFilterEnabled(true);


        et.addTextChangedListener(new TextWatcher()
        {
            public void afterTextChanged(Editable s)
            {
                // Abstract Method of TextWatcher Interface.
            }
            public void beforeTextChanged(CharSequence s,
                    int start, int count, int after)
            {

            }
            public void onTextChanged(CharSequence s,
                    int start, int before, int count)
            {
                textlength = et.getText().length();
                array_sort.clear();
                for (int i = 0; i < lv_arr.length; i++)
                {
                    if (textlength <= lv_arr[i].length())
                    {
                        if(et.getText().toString().equalsIgnoreCase(
                                (String)
                                lv_arr[i].subSequence(0,
                                        textlength)))
                        {
                            array_sort.add(lv_arr[i]);
                        }
                    }
                }
                lv1.setAdapter(new ArrayAdapter<String>
                (CribActivity.this,
                        android.R.layout.simple_list_item_1, array_sort));
            }
        });


        lv1.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> a, View v, int position, long id) 
            {

                String itemname = new Integer(position).toString();  

                Intent intent = new Intent();

                intent.setClass(CribActivity.this, ViewActivity.class);

                Bundle b = new Bundle();

                Activity
                b.putString("defStrID", itemname); 

                intent.putExtras(b);


                startActivity(intent);
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

onItemClick中使用:

String itemname = new Integer(id).toString();

而不是:

String itemname = new Integer(position).toString();

答案 1 :(得分:0)

与绝对指数和相对指数相矛盾!现在它起作用了! 这是代码

lv1.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> a, View v, int position,
                long id) {
            String name = array_sort.get(position);
            for (int i = 0; i < lv_arr.length; i++) {

                    if (name
                            .equalsIgnoreCase(
                                    (String) lv_arr[i])) {
                        position = i; 

                        break;
                    }

            }


            // Позиция элемента, по которому щелкнули
            String itemname = new Integer(position).toString();

            Intent intent = new Intent();

            intent.setClass(CribActivity.this, ViewActivity.class);

            Bundle b = new Bundle();

            // defStrID содержит строку, которую отправим через itemname в
            // другое Activity
            b.putString("defStrID", itemname);

            intent.putExtras(b);

            // запускаем Intent
            startActivity(intent);
        }
    });