我的复选框列表正在显示,但我想检查一些列表项目,它将保存在xml中以供进一步使用。怎么样?这里是我的列表代码,首选项..请用代码向我解释如何保存我的复选框列表。
public class MachinesCheck extends Activity implements IObserver {
private StringBuffer buffer = new StringBuffer();
private ListView mainListView ;
private ArrayList<Machine> Machines ;
private ArrayList<Machine> SelectedMachines ;
private ArrayAdapter<Machine> listAdapter ;
Vector<MDCMachineStatus> machineStatus_vector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.machinecheck);
SelectedMachines = new ArrayList<Machine>();
MachineStatusSingleton.Register(this);
getData();
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
// When item is tapped, toggle checked properties of CheckBox and Planet.
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick( AdapterView<?> parent, View item,
int position, long id) {
Machine machine = listAdapter.getItem( position );
machine.toggleChecked();
MachineViewHolder viewHolder = (MachineViewHolder) item.getTag();
viewHolder.getCheckBox().setChecked( machine.isChecked() );
if (machine.checked)
{
SelectedMachines.add(machine);
}
else
{
SelectedMachines.remove(machine);
}
}
});
listAdapter = new MachineArrayAdapter(this, Machines);
mainListView.setAdapter( listAdapter );
mainListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
private void getData(){
machineStatus_vector = MachineStatusSingleton.GetData();
//arrayListofMachines = new ArrayList<String>();
//arrayListofMachineNumbers = new ArrayList<String>();
Machines = new ArrayList<Machine>();
for(MDCMachineStatus temp: machineStatus_vector){
//arrayListofMachines.add(temp.toString());
//arrayListofMachineNumbers.add(temp.getNumber());
Machines.add(new Machine(temp.toString(), temp.getNumber()));
}
}
private static class Machine {
private String name = "" ;
private String number ="";
private boolean checked = false ;
public Machine() {}
public Machine( String name, String number ) {
this.name = name ;
this.number = number ;
}
public Machine( String name, boolean checked ) {
this.name = name ;
this.checked = checked ;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String toString() {
return name ;
}
public void toggleChecked() {
checked = !checked ;
}
}
private static class MachineViewHolder {
private CheckBox checkBox ;
private TextView textView ;
public MachineViewHolder( TextView textView, CheckBox checkBox ) {
this.checkBox = checkBox ;
this.textView = textView ;
}
public CheckBox getCheckBox() {
return checkBox;
}
public void setCheckBox(CheckBox checkBox) {
this.checkBox = checkBox;
}
public TextView getTextView() {
return textView;
}
public void setTextView(TextView textView) {
this.textView = textView;
}
}
private static class MachineArrayAdapter extends ArrayAdapter<Machine> {
private LayoutInflater inflater;
public MachineArrayAdapter( Context context, List<Machine> machineList ) {
super( context, R.layout.selectedlist, R.id.rowTextView, machineList );
// Cache the LayoutInflate to avoid asking for a new one each time.
inflater = LayoutInflater.from(context) ;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Machine to display
Machine machine = (Machine) this.getItem( position );
// The child views in each row.
CheckBox checkBox ;
TextView textView ;
// Create a new row view
if ( convertView == null ) {
convertView = inflater.inflate(R.layout.selectedlist, null);
// Find the child views.
textView = (TextView) convertView.findViewById( R.id.rowTextView );
checkBox = (CheckBox) convertView.findViewById( R.id.CheckBox01 );
// Optimization: Tag the row with it's child views, so we don't have to
// call findViewById() later when we reuse the row.
convertView.setTag( new MachineViewHolder(textView,checkBox) );
// If CheckBox is toggled, update the planet it is tagged with.
checkBox.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
Machine machine = (Machine) cb.getTag();
machine.setChecked( cb.isChecked() );
}
});
}
// Reuse existing row view
else {
// Because we use a ViewHolder, we avoid having to call findViewById().
MachineViewHolder viewHolder = (MachineViewHolder) convertView.getTag();
checkBox = viewHolder.getCheckBox() ;
textView = viewHolder.getTextView() ;
}
// Tag the CheckBox with the Planet it is displaying, so that we can
// access the planet in onClick() when the CheckBox is toggled.
checkBox.setTag( machine );
// Display planet data
checkBox.setChecked( machine.isChecked() );
textView.setText( machine.getName() );
return convertView;
}
}
public Object onRetainNonConfigurationInstance() {
return Machines ;
}
public void Update(ISubject arg0) {
// TODO Auto-generated method stub
}
}
以下是我的偏好,请检查并告诉我它是如何保存数据im xml文件的。
<PreferenceScreen
android:key="DataEntryScreen"
android:title="Data Entry Machine"
android:summary="Select a Machine">
</PreferenceScreen>
答案 0 :(得分:1)
尝试使用此代码创建xml,保存它使用此字符串并将其写入文件。
private static StringWriter createXml(ArrayList<Machine> SelectedMachines) {
StringWriter writer = new StringWriter();
XmlSerializer serializer = Xml.newSerializer();
try {
serializer.setOutput(writer);
serializer.startDocument("UTF-8", true);
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startTag(null, "Machine");
for (Machine machine : SelectedMachines) {
serializer.startTag(null, "Name");
serializer.text(machine.getName());
serializer.endTag(null, "Name");
serializer.startTag(null, "Number");
serializer.text(machine.getNumber());
serializer.endTag(null, "Number");
}
if (serializer == null)
return null;
serializer.endTag(null, "Machine");
serializer.endDocument();
serializer.flush();
writer.close();
} catch (Exception e) {
Log.e("createXml", e.toString());
}
return writer;
}