更新:我已经在this的帮助下解决了我的问题。如果有兴趣的话,我会发布。
这是我的第一篇文章...请保持温柔。 我已经成功地将数据从xml文件解析为ListView。我想在每一行中都有RadioGroup,然后我将根据无线电选择做一些事情。由于android重绘自己,我的选择不会保存。我已阅读this教程但很难转换到我的项目中,因为它们没有解析数据。
如何保存每个行项目的选择?这是我的代码:
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>
的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>
那么如何保存选择?最后,我想根据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;
}
}