我想在自定义列表视图中对单击的项目使用switch case语句,但是我无法存储单击的数据值,也无法存储将其用于我的case语句的项目位置。 / p>
答案 0 :(得分:0)
您需要添加OnItemClickListener
回调:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
String value = list.getSelectedItem();
}
}
答案 1 :(得分:0)
public class MainActivity extends Activity {
ListView listItemView;
// Define string array.
String[] listItemsValue = new String[] {"Android","PHP","Web Development","Blogger","SEO","Photoshop"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listItemView = (ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2, android.R.id.text1, listItemsValue);
listItemView.setAdapter(adapter);
// ListView setOnItemClickListener function apply here.
listItemView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, listItemsValue[position], Toast.LENGTH_SHORT).show();
}
});
}
}