我刚开始编写Android应用程序,非常感谢您提供一些帮助:
我正在开发一个应用程序,允许用户使用“CHOICE_MODE_MULTIPLE”从一系列复选框中选择一组标签 - 但是当用户点击时,我很难弄清楚如何保存这些复选框保存按钮“save.setOnClickListener(onSave)”
我已经阅读了一些关于“getCheckedItemIds”但是很难找到任何具体的例子,说明它是如何工作的以及如何使用它。对此的任何帮助都将非常感激。
我在下面附上了我的代码,以便了解我正在尝试做什么
public class PhysicsCollector extends TabActivity {
//EditText exampleName=null;
List<Example> model=new ArrayList<Example>();
ArrayAdapter<Example> adapter=null;
private ListView elementTags;
private String et_items[] = {"Friction", "Two Body Motion", "Constant Acceleration",
"Newton's First Law", "Newton's Second Law", "Collision"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
elementTags = (ListView)findViewById(R.id.ListView01);
elementTags.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, et_items));
elementTags.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//exampleName=(EditText)findViewById(R.id.exampleName);
Button save=(Button)findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView list=(ListView)findViewById(R.id.example);
adapter=new ArrayAdapter<Example>(this,
android.R.layout.simple_list_item_1, model);
list.setAdapter(adapter);
TabHost.TabSpec spec=getTabHost().newTabSpec("tag1");
spec.setContent(R.id.example);
spec.setIndicator("List", getResources()
.getDrawable(R.drawable.list));
getTabHost().addTab(spec);
spec=getTabHost().newTabSpec("tag2");
spec.setContent(R.id.details);
spec.setIndicator("Details", getResources()
.getDrawable(R.drawable.example));
getTabHost().addTab(spec);
getTabHost().setCurrentTab(0);
}
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
Example e=new Example();
EditText name=(EditText)findViewById(R.id.exampleName);
e.setName(name.getText().toString());
public long[] getCheckedItemIds ();
adapter.add(e);
}
};
}
下面是“Example”类,用于从onSave保存数组实例:
package org.encorelab.s3;
public class Example {
private String name="";
public String getName(){
return(name);
}
public void setName(String name) {
this.name=name;
}
public String toString() {
return(getName());
}
}