如何获取整个 ListView 的字符串数组
How to get a String-Array of the whole ListView
我有一个动态添加了 TextViews
的 ListView
,如何在 String-Array
中获取 ListView
的所有 TextView
对象?
为了进行测试,我想在 onItemClick
方法
中的 Toast
中显示整个 String-Array
创建时:
final ListView sessionLv = (ListView) findViewById(R.id.session_list);
sessionLv.setAdapter(sessionAdapter);
sessionLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView c = (TextView) view.findViewById(R.id.stepTime);
String playerChanged = c.getText().toString();
Toast toast = Toast.makeText(getApplicationContext(),
playerChanged +" wurde angeklickt!",
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
});
我的 ListView
中动态添加的 TextView
个对象的 adapter
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;
}
希望这会有所帮助,
//in your item click
sessionLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SessionItemAdapter adapter = (SessionItemAdapter) parent.getAdapter();
List<String> allPlayers = adapter.getAllPlayers();
// Log your list or whatever you want to do with it
Log.i(TAG, "allPlayers: " + allPlayers.toString());
}
});
//in your adapter getAllPlayers() would be something like this
public List<String> getAllPlayers(){
List<String> list=new ArrayList<String>();
for (Map item : values) {
int beepStringId = context.getResources().getIdentifier("exercise_typ_"
+ item.get("type"), "string", context.getPackageName());
String str = context.getResources().getString(beepStringId);
list.add(str);
}
return list;
}
我有一个动态添加了 TextViews
的 ListView
,如何在 String-Array
中获取 ListView
的所有 TextView
对象?
为了进行测试,我想在 onItemClick
方法
Toast
中显示整个 String-Array
创建时:
final ListView sessionLv = (ListView) findViewById(R.id.session_list);
sessionLv.setAdapter(sessionAdapter);
sessionLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView c = (TextView) view.findViewById(R.id.stepTime);
String playerChanged = c.getText().toString();
Toast toast = Toast.makeText(getApplicationContext(),
playerChanged +" wurde angeklickt!",
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
});
我的 ListView
TextView
个对象的 adapter
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;
}
希望这会有所帮助,
//in your item click
sessionLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SessionItemAdapter adapter = (SessionItemAdapter) parent.getAdapter();
List<String> allPlayers = adapter.getAllPlayers();
// Log your list or whatever you want to do with it
Log.i(TAG, "allPlayers: " + allPlayers.toString());
}
});
//in your adapter getAllPlayers() would be something like this
public List<String> getAllPlayers(){
List<String> list=new ArrayList<String>();
for (Map item : values) {
int beepStringId = context.getResources().getIdentifier("exercise_typ_"
+ item.get("type"), "string", context.getPackageName());
String str = context.getResources().getString(beepStringId);
list.add(str);
}
return list;
}