问题描述:

What I'm trying to do is set the text of the item I click on into the myEditText. Not sure how to access the specific item in ListView though...

 ListView myListView = (ListView)findViewById(R.id.myListView);

final EditText myEditText = (EditText)findViewById(R.id.myEditText);

...

myListView.setAdapter(aa);

myListView.setOnClickListener(new OnClickListener(){

public void onClick(View v){

myEditText.setText("You clicked something!");

}

});

网友答案:

I'm a newbie but recently did this, so if you can understand my code snippet, something like this:

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, array_of_something));       

ListView lv = getListView();
lv.setTextFilterEnabled(true);

lv.setOnItemClickListener(new OnItemClickListener() {     

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int arg2, long arg3) {

        String item_clicked_on = (String)((TextView) view).getText();                                            

        }
      });

Maybe this helps?

网友答案:

Don't do any of those. When using a listview, set on onItemClickListener and it will give you the position of the listitem that was clicked.

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {

now position is the position of the listitem that was clicked.

网友答案:

put this in your onCreate method.

ListView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_items, ListViewArray));

here list_item is the xml file which defines the look and feel of items in list,

instead you can also use Android's inbuilt layout which is "android.R.layout.simple_list_item_1" and you can use it like this,

ListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ListViewArray));

now when you click on the list items, handle like this (outside onCreate),

ListView.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    //either use toast to show item text        
    Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
    //else go to a new activity like this,
    Intent intent = new Intent();
    intent.setClass(view.getContext(), AnotherClass.class);
    startActivity(intent);
} } );

You can define what you want to show in new activity when clicked on list item.

相关阅读:
Top