2

UPDATE: I have solved my problem with the help of this I'll post if anyone interested.

this is my first post...please be gentle. I have successfully parsed data from an xml file into a ListView. I want to have RadioGroup inside each row and then I will do some stuff based off radio selection. Since android redraws itself, my selections are not saved. I've read this tutorial but it is hard to translate into my project because they are not parsing data.

How can I save selections for each row item? Here is my code:

public class OOPActivity extends ListActivity {

static final String path = "oop.xml";

//xml node keys
static final String KEY_STUDENT = "student"; //parent node
static final String KEY_ID = "student_id";
static final String KEY_NAME = "name";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ArrayList<HashMap<String, String>> students = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser(this);

    String xmlString = parser.getXML(path); //get XML

    Document doc = parser.getDomElement(xmlString); //get DOM element

    NodeList nl = doc.getElementsByTagName(KEY_STUDENT);

    //looping through all item nodes <student>
    for(int i=0; i < nl.getLength(); i++){
        //creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        //adding each child node to Hashmap key => value
        map.put(KEY_STUDENT, parser.getValue(e,  KEY_STUDENT));
        map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
        map.put(KEY_ID, parser.getValue(e, KEY_ID));

        students.add(map);
    }


    ListAdapter adapter = new SimpleAdapter(this, students,
            R.layout.student_items,
            new String[] { KEY_NAME, KEY_ID }, new int[] {
            R.id.name, R.id.id});



    setListAdapter(adapter);


}

Row Layout:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/light_blue_back"
    android:orientation="vertical" >
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textSize="26dip"
        android:textStyle="bold"
        >
</TextView>
<TextView
        android:id="@+id/id"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textSize="16dip"
        android:textStyle="bold"
        >
    </TextView>
    <RadioGroup
        android:id="@+id/student_state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    <RadioButton
        android:id="@+id/chk_present"
        android:text="Present"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
         />
    <RadioButton
        android:id="@+id/chk_absent"
        android:text="Absent"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
         />
    <RadioButton 
        android:id="@+id/chk_tardy"
        android:text="Tardy"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        />
    <RadioButton
        android:id="@+id/chk_excused"
        android:text="Excused Absence"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
         />
    <RadioButton
        android:id="@+id/chk_left"
        android:text="Left Early"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
         />
    </RadioGroup>
</LinearLayout>

ListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >"

     <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:background="@drawable/blue_back"
         >
    </ListView>


</LinearLayout>

So how to save selections? In the end I want to write a new attribute into the xml file based off RadioGroup selection. Please let me know if you need any further information. I've been stuck on this for days. Cannot post screen shot because of my newbie status.

UPDATE: created custom adapter....I think this is where I need to put RadioButton stuff...

public class MyAdapter extends ArrayAdapter<HashMap<String, String>> {
private final Activity context;
private final ArrayList<HashMap<String, String>> values;

static class ViewHolder{
    public TextView name;
    public TextView id;
    protected CheckBox checkbox;
}

public MyAdapter(Activity context, ArrayList<HashMap<String, String>> values){
    super(context, R.layout.student_items, values);
    this.context = context;
    this.values = values;
}


public View getView(int position, View convertView, ViewGroup parent){
    View rowView = convertView;
    if(rowView == null){
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.student_items, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.name = (TextView)rowView.findViewById(R.id.name);
        viewHolder.id = (TextView)rowView.findViewById(R.id.id);
        rowView.setTag(viewHolder);
    }
    ViewHolder holder = (ViewHolder) rowView.getTag();
    HashMap<String, String> s = values.get(position);



    //holder.name.setText(s.get(position).toString());
    holder.name.setText(s.values().toString());
    //holder.name.setText(s.toString());
    System.out.println(s);

    return rowView;

}

}

Community
  • 1
  • 1
Mayfly
  • 21
  • 3

0 Answers0