2

更新:我已经在这个帮助下解决了我的问题, 如果有人感兴趣,我会发布。

这是我的第一篇文章...请温柔。我已成功将 xml 文件中的数据解析为 ListView。我想在每一行中都有 RadioGroup,然后我会根据收音机选择做一些事情。由于 android 自己重绘,我的选择没有保存。我已经阅读了本教程,但很难将其转化为我的项目,因为它们不解析数据。

如何保存每个行项目的选择?这是我的代码:

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


}

行布局:

    <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>

列表显示:

<?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>

那么如何保存选择呢?最后,我想根据 RadioGroup 选择将新属性写入 xml 文件。如果您需要任何进一步的信息,请告诉我。我已经坚持了好几天了。由于我的新手状态,无法发布屏幕截图。

更新:创建了自定义适配器......我认为这是我需要放置 RadioButton 东西的地方......

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;

}

}

4

0 回答 0