0

enter image description here

I searched existing tutorial. Im following that code. I want to show particular list from listview, while Im clicking. But i implement onclicklistener inside my code. It won't working. If i search t, it will shows two,three,ten. But i clicked two it doesn't shows only two.

MyCode:

public class ListViewSearchExample extends Activity {

    private ListView lv;
    private EditText et;
    private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE",
    "SIX", "SEVEN", "EIGHT", "NINE", "TEN" };
    private ArrayList<String> array_sort= new ArrayList<String>();
    int textlength=0;


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

        lv = (ListView) findViewById(R.id.ListView01);
        et = (EditText) findViewById(R.id.EditText01);
        //lv.setAdapter(new ArrayAdapter<String>(this,
        //android.R.layout.simple_list_item_1, listview_array));

        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)
        {
        // Abstract Method of TextWatcher Interface.
        }
        public void onTextChanged(CharSequence s,
        int start, int before, int count)
        {
        textlength = et.getText().length();
        array_sort.clear();
        for (int i = 0; i < listview_array.length; i++)
        {
        if (textlength <= listview_array[i].length())
        {
        if(et.getText().toString().equalsIgnoreCase(
        (String)
        listview_array[i].subSequence(0,
        textlength)))
        {
             array_sort.add(listview_array[i]);
        }
        }
        }

        lv.setAdapter(new ArrayAdapter<String>
        (ListViewSearchExample.this,
        android.R.layout.simple_list_item_1, array_sort));
        lv.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long rowId) {
                 //TODO Auto-generated method stub
            //parent.getItemAtPosition(position);
                lv.getItemAtPosition(position);

            }

        });


            }
        });
        }
4

1 回答 1

0

试试这样

lstaaplist.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub

            String name = parent.getItemAtPosition(position).toString();
            long id1 = parent.getItemIdAtPosition(position);


        }
    });
于 2012-09-14T04:41:25.200 回答