作为我的应用程序的一部分,我正在尝试创建可自定义的核对表。用户可以从默认清单中选择他想要的数量,同时添加anad将新项目删除到列表中。但是,一旦用户最终确定了列表中的项目,则需要在下次使用该应用程序时保存该列表。
我尝试过几种方法,但都没有。有人可以帮我这个吗? 这是代码:
public class Dynamo extends Activity{
@Overrided
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String[] A = new String[100];
String[] B = new String[100];
final CheckBox[] cbx = new CheckBox[100];
int i,k,j,m,q;
final int n;
ScrollView sv = new ScrollView(this);
final LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
Bundle extras = getIntent().getExtras();
if (extras != null) {
A = extras.getStringArray("var");
i = extras.getInt("var2");
B = extras.getStringArray("var3");
k = extras.getInt("var4");
for(j=0;j<i;j++)
{
cbx[j] = new CheckBox(this);
ll.addView(cbx[j]);
cbx[j].setText(A[j]);
}
for (m=0;m<k;m++)
{
cbx[m] = new CheckBox(this);
ll.addView(cbx[m]);
cbx[m].setText(B[m]);
}
n=j+m;
Button delete=new Button(this);
delete.setText("Delete");
ll.addView(delete);
delete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int p = 0; p < n; p++) {
if (cbx[p].isChecked())
{
ll.removeView(cbx[p]);
Toast.makeText(getApplicationContext(), "Removing! " , Toast.LENGTH_SHORT).show();
}
}
}});
this.setContentView(sv);
}
答案 0 :(得分:0)
你应该在xml中列出你的列表以供同意....
private void save(){ SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.commit();
//you will need the following line for each checkbox you want to save data for.
editor.putBoolean("CHECKBOX", yourcheckbox.isChecked();
editor.commit();
Toast.makeText(getBaseContext(), "Data Saved...", Toast.LENGTH_SHORT).show();
}
你也需要这个方法来加载它们,在onCreate()中调用它,但在你初始化复选框之后
private void loadSavedPrefs() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
yourcheckbox.isChecked=sharedPreferences.getBoolean("CHECKBOX", false);
}