我花了几个小时研究它,尝试了不同的方法,但我最接近的是获取列表中第一项的文本 - 而不是选定的文本。
这是我的getView方法:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.sweetName = (TextView) convertView
.findViewById(R.id.sweetName);
holder.sweetPic = (ImageView) convertView
.findViewById(R.id.sweetPic);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.sweetPic.setImageResource(sweetPicResource[position]);
holder.sweetName.setText(sweetsNames[position]);
return convertView;
}
这是来自活动的onClickListener:
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View view,int position, long arg3) {
String selectedSweet = listView.getItemAtPosition(position).toString();
TextView textView = (TextView) listView.findViewById(R.id.sweetName);
String text = textView.getText().toString();
Toast.makeText(getApplicationContext(), "Selected item: " + text + " - " + position, Toast.LENGTH_SHORT).show();
}
}
);
请问任何想法?
答案 0 :(得分:2)
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View view,int position, long arg3) {
String selectedSweet = listView.getItemAtPosition(position).toString();
TextView textView = (TextView) view.findViewById(R.id.sweetName);
String text = textView.getText().toString();
Toast.makeText(getApplicationContext(), "Selected item: " + text + " - " + position, Toast.LENGTH_SHORT).show();
}
}
);