如果单击复选框,我的清单应该保存在 Xml 中 如何?
我的复选框列表正在显示,但我想检查一些列表项,该列表项将保存在 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
}
}
这是我的偏好,请检查并告诉我它如何在 xml 文件中保存数据。
<PreferenceScreen
android:key="DataEntryScreen"
android:title="Data Entry Machine"
android:summary="Select a Machine">
</PreferenceScreen>
My checkboxlist are displaying but i want to check some list item which will save in xml for further use. how ?here is my list code, preference .. please explain me with code that how it will save my checkbox list.
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
}
}
and here is my preference please check and tell me how it saved the data im xml file.
<PreferenceScreen
android:key="DataEntryScreen"
android:title="Data Entry Machine"
android:summary="Select a Machine">
</PreferenceScreen>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用此代码创建 xml,使用此字符串保存它并将其写入文件。
Try this code to create xml, to save it use this string and write it to file.