0

当用户单击特定列表时,我想显示列表视图查询单独的页面。我为显示列表视图编写了程序,并实现了 onitemclick。但是当我单击列表视图时,它不会显示查询。例如:如果我点击一个,我想显示一个:一个是第一个数字。

我的代码:

public class ListViewSearchExample extends Activity {

    private ListView lv;
    private EditText et;
    private TextView tv;
    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          

                lv.getItemAtPosition(position);          


                //lv.setBackgroundColor((position & 1) == 1 ? Color.WHITE : Color.LTGRAY);

            }

        });


            }
        });
        }
4

1 回答 1

1

在您的 onItemClick 中尝试这样的操作:

String number = listview_array[position];
if (number.equals("ONE")) {
    et.setText("One is the first number");
}
else if (number.equals("TWO")) {
...
}
于 2012-09-22T05:41:26.543 回答