从列表视图获取视图到字符串
Get View from Listview to String
我有一个列表视图,我需要在单击列表视图时获取每行的字符串值。
这是我的代码
public void ListDrwaer() {
List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("lectureraccount");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("lecturerName");
String code = jsonChildNode.optString("lecturerCode");
String outPut = name + " --- " + code;
employeeList.add(createEmployee("lecturer", outPut));
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,android.R.layout.simple_list_item_1,new String[] { "lecturer" }, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
String str = parent.getAdapter().getItem(position).toString();
System.out.println(str);
}});
}
我管理得到这样的输出。
{lecturer=MAGESWARY A/P MUNIANDI --- C-MAGESWARY}
我的问题是如何得到这样的输出
C-MAGESWARY
就是这样。我是编程新手,了解不多。我需要完全更改我的代码吗?如果是,如何?
谢谢。
您可以在 onItemClick
方法中使用 split
执行此操作,如下所示:
String str = parent.getAdapter().getItem(position).toString();
String[] parts = str.split("---");
String part1 = parts[0];
String part2 = parts[1]; // This is the part you want
String output = part2.replaceAll("\s+","") //trim whitespace
String output2 = output.replace("}", ""); // remove the "}" character
System.out.println(output2);
输出:
C-MAGESWARY
这是一个 Java 问题。看看linkClass String Oracle。除了前面的答案,还要查看方法 substring、indexOf 和 lastIndexof。
由于您可以控制 ListView 中的字符串格式,因此您可以轻松完成。只需在您的示例 "C-MAGESWARY" 之前添加唯一字符,例如 "!@" 。然后您可以在字符串中搜索这些字符。为以后的应用记住这个技巧。
我有一个列表视图,我需要在单击列表视图时获取每行的字符串值。
这是我的代码
public void ListDrwaer() {
List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("lectureraccount");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("lecturerName");
String code = jsonChildNode.optString("lecturerCode");
String outPut = name + " --- " + code;
employeeList.add(createEmployee("lecturer", outPut));
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,android.R.layout.simple_list_item_1,new String[] { "lecturer" }, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
String str = parent.getAdapter().getItem(position).toString();
System.out.println(str);
}});
}
我管理得到这样的输出。
{lecturer=MAGESWARY A/P MUNIANDI --- C-MAGESWARY}
我的问题是如何得到这样的输出
C-MAGESWARY
就是这样。我是编程新手,了解不多。我需要完全更改我的代码吗?如果是,如何? 谢谢。
您可以在 onItemClick
方法中使用 split
执行此操作,如下所示:
String str = parent.getAdapter().getItem(position).toString();
String[] parts = str.split("---");
String part1 = parts[0];
String part2 = parts[1]; // This is the part you want
String output = part2.replaceAll("\s+","") //trim whitespace
String output2 = output.replace("}", ""); // remove the "}" character
System.out.println(output2);
输出:
C-MAGESWARY
这是一个 Java 问题。看看linkClass String Oracle。除了前面的答案,还要查看方法 substring、indexOf 和 lastIndexof。 由于您可以控制 ListView 中的字符串格式,因此您可以轻松完成。只需在您的示例 "C-MAGESWARY" 之前添加唯一字符,例如 "!@" 。然后您可以在字符串中搜索这些字符。为以后的应用记住这个技巧。