【问题标题】:Android - separating List stringsAndroid - 分隔列表字符串
【发布时间】:2012-05-09 15:44:19
【问题描述】:

我正在从列表视图中单击的元素中检索字符串数据。

元素有两行,一行名为“current”,另一行名为“name”。 在我的 listItemOnClick() 中,我正在获取被单击的项目,然后对其执行 toString()。我得到的是这样的:

{current=SOMETHING, name=SOMETHING}

我的问题是如何将这些分开?这是我的点击代码:

    protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    Object o = this.getListAdapter().getItem(position);
    String current = o.toString();

    ((TextView) findViewById(R.id.check)).setText(current);
}

例如,我只想显示当前的。谢谢!

编辑

我的列表变量:

    static final ArrayList<HashMap<String,String>> listItems = 
        new ArrayList<HashMap<String,String>>();;
SimpleAdapter adapter;

创建列表:

       for(int i=0; i<num_enter; i++){
    final int gi = i;
    adapter=new SimpleAdapter(this, listItems, R.layout.custom_row_view,new String[]{"name", "current"},  new int[] {R.id.text1, R.id.text2});
    setListAdapter(adapter);
    HashMap<String,String> temp = new HashMap<String,String>();
    temp.put("name", name[i]);
    temp.put("current", "Value: " + Integer.toString(current[i]));
    listItems.add(temp);
    adapter.notifyDataSetChanged();
    }

【问题讨论】:

  • 列表中实际包含哪些类型的对象?无论o 是什么,最好的办法是获得比Object 更具体的类。
  • 列表中只有文字。我添加了一些代码

标签: android listview textview


【解决方案1】:

你可以这样做(当/如果格式发生变化时,丑陋且容易在未来出现错误) - 添加错误检查以防字符串格式不正确:

String s = "{current=CURRENT, name=NAME}";
s = s.substring(1, s.length() - 1); //removes { and }
String[] items = s.split(",");
String current = items[0].split("=")[1]; //CURRENT
String name = items[1].split("=")[1]; //NAME

在您进行编辑之后,似乎 o 是一个 Map,因此您也可以编写(好多了):

Map<String, String> map = (Map<String, String>) this.getListAdapter().getItem(position);
String current = map.get("current");
String name = map.get("name");

【讨论】:

    【解决方案2】:

    哇,每个人都在走很长的路。直接从视图中获取数据。在这种情况下, View v 是您的布局行,因此您可以使用 findViewById 找到各个文本视图并从中获取文本。使用您的代码将是这样的:

    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
    
        TextView nameTxt = (TextView) v.findViewById(R.id.Text1);
        TextView currentTxt = (TextView) v.findViewById(R.id.Text2);
        String name = nameTxt.getText().toString();
        String current = currentTxt.getText().toString();
    }
    

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      应该不难:

      protected void onListItemClick(ListView l, View v, int position, long id) {
      // TODO Auto-generated method stub
      super.onListItemClick(l, v, position, id);
      Object o = this.getListAdapter().getItem(position);
      String current = o.toString();
      
      // first remove the first and last brace
      current = current.substring(1,current.length()-1);
      // then split on a comma
      String[] elements = current.split(",");
      // now for every element split on =
      String[] subelements = elements[0].split("=");
      String key1 = subelements[0];
      String value1 = subelements[1];
      
      subelements = elements[1].split("=");
      String key2 = subelements[0];
      String value2 = subelements[1];
      
      
      ((TextView) findViewById(R.id.check)).setText(current);
      }
      

      【讨论】:

        猜你喜欢
        • 2010-10-19
        • 1970-01-01
        • 2011-06-09
        • 1970-01-01
        • 2011-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-23
        相关资源
        最近更新 更多