如何获取ListView中所选值项的字符串值

时间:2012-04-01 18:28:56

标签: android android-layout

我的项目中有一个Listview,我想在列表视图中找到所选项目的字符串值。

2 个答案:

答案 0 :(得分:0)

这是一个简单的例子。

@Override
protected void onListItemClick(ListView l, View v, int position, long thisID)
{
  super.onListItemClick(l, v, position, thisID);
  // Get the item that was clicked
  Object o = this.getListAdapter().getItem(position);
  String keyword = o.toString();
}

答案 1 :(得分:0)

来自您的活动

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);
            }
        });
    }
}