android - 如果单击复选框,我的 list 应该保存在 Xml 中如何?

标签 android preferences checkboxlist

我的复选框列表正在显示,但我想检查一些将保存在 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>

最佳答案

尝试使用此代码创建 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;
    }

关于android - 如果单击复选框,我的 list 应该保存在 Xml 中如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9225475/

相关文章:

css - 为什么 Font-Size 和 CssClass 属性在 aspx 页面的 CheckBoxList 中不起作用?

android - 当从android上传图像文件到wcf时,图像文件被破坏

android - 如果更改了首选项,则刷新 Activity

java - 如何在屏幕旋转时保存和检索变量?

javascript - JavaScript 中的自定义计时器

Android 偏好不保存

ASP.NET:带有文本框的复选框列表?

Java SWT 检查列表框

android - 在android中制作倾斜 View

安卓 : add numbers to map markers