2

I am retrieving the string data from an element clicked in my listview.

The element has two rows, one named "current" and one named "name". in my listItemOnClick(), i am getting the item that was clicked, and then doing toString() on it. what i am getting is something like this:

{current=SOMETHING, name=SOMETHING}

My question is how do i separate these? Here is my onclick code:

    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);
}

I want to disply only the current for example. Thanks!

EDIT

My List Variables:

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

Creating the list:

       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();
    }
2
  • What sort of objects are actually in the list? The best thing would be to get a more-specific class than Object for whatever o is. Commented May 9, 2012 at 15:47
  • In the list there is only text. I added some code Commented May 9, 2012 at 15:49

3 Answers 3

4

You could do it that way (ugly and prone to future error when / if the format changes) - add error checks in case the string does not have the right format:

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

Following your edit, it seems that o is a Map so you could also write (much better):

Map<String, String> map = (Map<String, String>) this.getListAdapter().getItem(position);
String current = map.get("current");
String name = map.get("name");
Sign up to request clarification or add additional context in comments.

Comments

3

Wow, everyone is taking the long way round. Get the data directly from the view. The View v in this case is your layout row, so using that you can find the individual textviews using findViewById and the get the text from them. Using your code it would be like this:

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();
}

Hope this helps!

Comments

0

It should not be very difficult:

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.