简体   繁体   中英

Get Selected String from the ListView

I have ListView in my project which has ListItems as in the form of String like this,

Complete Task By Today
Assign Projects To Developers
Meeting Cancelled

if click any one of the item from ListView means i want that particular String should be displayed in my EditText(i,e if click meeting cancelled, i need this particular String to display in EditText).Any one guide me,This is how i'm trying to Achieve.enter code here Thanks in advance.

final ListView listView = (ListView) findViewById(R.id.taskListDesc); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

            System.out.println(adapter.getItemAtPosition(selectedItem));

            Intent intent = new Intent(getApplicationContext(),
                    ViewTaskActivity.class);
            startActivity(intent);
        }
    });
}
ArrayAdapter<String> adapter;

adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, profielNames); 
setListAdapter(adapter);

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {

  super.onListItemClick(l, v, position, id);

  // Get the data associated with selected item
  Object item = l.getItemAtPosition(position);
  String myitem = item.toString();
  edittxt.setText("Selected item is :"+ myitem); // You can Set EditText from Here.
 }

You can even try this.....

public class ExampleActivity extends Activity {

String str_arr[] = {"A", "B", "C"};


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

    ListView listView = (ListView) findViewById(R.id.listView1);

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

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position,
                long id) {
            // TODO Auto-generated method stub
            Object o = adapter.getItemAtPosition(position);
            String str_text = o.toString();

            Intent intent = new Intent(ExampleActivity.this, NewExample.class);
            intent.putExtra("StrValue", str_text);
            startActivity(intent);
        }

    });
 }
 }

public class NewExample extends Activity {

EditText editText;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newmain);

    editText = (EditText) findViewById(R.id.editText1);

    String value = getIntent().getStringExtra("StrValue").toString();

    editText.setText(value);
}

}

Hope this helps.......

Njoy coding.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM