更新:私はこの助けを借りて私の問題を解決しました 。興味のある人は投稿します。
初めての投稿です…お手柔らかにお願いします。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;
}
}