如何获取ListView
的点击项目的名称?
public void addExercise(View view) {
LayoutInflater mInflater = LayoutInflater.from(this);
final View exercises = mInflater.inflate(R.layout.exercises, null);
final Spinner spinner = (Spinner) exercises.findViewById(R.id.sessionTypeSpinner);
final ListView listView = (ListView) exercises.findViewById(R.id.listViewexercises);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
rowItems.add(item);
}
CustomListViewAdapter adapter2 = new CustomListViewAdapter(getApplicationContext(),
R.layout.exercises_custom_row, rowItems);
listView.setAdapter(adapter2);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
});
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
final ListView listView = (ListView) exercises.findViewById(R.id.listViewexercises);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
rowItems.add(item);
}
CustomListViewAdapter adapter = new CustomListViewAdapter(getApplication(),
R.layout.exercises_custom_row, rowItems);
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
int arme = workoutDBAO.addSession(workoutId, position);
Map newRow = new HashMap();
newRow.put("id", arme);
newRow.put("type", 10 + position);
sessionAdapter.add(newRow);
}
});
break;
case 1:
ListView listView1 = (ListView) exercises.findViewById(R.id.listViewexercises);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles1.length; i++) {
RowItem item = new RowItem(images[i], titles1[i], descriptions[i]);
rowItems.add(item);
}
CustomListViewAdapter adapter1 = new CustomListViewAdapter(getApplication(),
R.layout.exercises_custom_row, rowItems);
listView1.setAdapter(adapter1);
// ListView Item Click Listener
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ArrayList<RowItem> title = rowItems;
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + title.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
});
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
new AlertDialog.Builder(this)
.setTitle(R.string.action_add_step)
.setView(exercises)
.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
int exerciseNr = spinner.getSelectedItemPosition();
}
}).show(); //
}
//OnClick on Item that you created
class ListViewItemClickHandler implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item was clicked",
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}
class SessionItemAdapter extends ArrayAdapter<Map> {
ArrayList<Map> values;
Context context;
SessionItemAdapter(Context context, ArrayList<Map> values) {
super(context, R.layout.session_list_layout_row, values);
this.values = values;
this.context = context;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.session_list_layout_row, parent, false);
TextView tv = (TextView) rowView.findViewById(R.id.stepTime);
final int rowId = (Integer) values.get(position).get("id");
int beepStringId = getResources().getIdentifier("exercise_typ_" + values.get(position).get("type"), "string", getPackageName());
rowView.setTag(rowId);
tv.setText(getResources().getString(beepStringId));
ImageButton deleteSession = (ImageButton) rowView.findViewById(R.id.session_item_del);
deleteSession.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*if (!startStopToggle.isChecked()) {*/
sessionAdapter.remove(values.get(position));
sessionAdapter.notifyDataSetChanged();
}
});
return rowView;
}
我想在 ListViewItemClickHandler 类的Toast
方法中的OnItemClick
中显示它。
所以我想在某种程度上我必须得到点击项目的名称来显示它,但我不知道如何请帮助我!
修改
我的RowItem类
public class RowItem {
private int imageId;
private String title;
private String desc;
public RowItem(int imageId, String title, String desc) {
this.imageId = imageId;
this.title = title;
this.desc = desc;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return title + "\n" + desc;
}
}
答案 0 :(得分:1)
试试这个:
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + rowItems.get(position).getTitle(),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}