Android:保存复选框状态并找到复选框位置

时间:2014-12-29 05:40:46

标签: android checkbox

我想使用保存首选项方法保存我的复选框状态,所以当我关闭应用程序时,检查的项目仍然存在,但问题是我无法获取复选框数组位置或复选框索引,请帮助我这些

继承我的主要活动计划

  package com.example.cameraapp;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class FinalChoice extends Activity implements OnItemClickListener{
     CheckBox checkbox;
     List<File> lst;
    ListView listView;
    ArrayAdapter<Model> adapter;
    List<Model> list = new ArrayList<Model>();
    Button save;
    ContextWrapper context;
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.listview1hold);

        listView = (ListView) findViewById(R.id.listView1);
        adapter = new CustomAdapter(this,getModel());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((android.widget.AdapterView.OnItemClickListener) this);
        save=(Button)findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {



                    Toast.makeText(getApplicationContext(), "FUCKYOU", Toast.LENGTH_SHORT).show();



            }
        });

    }


    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
                TextView label = (TextView) v.getTag(R.id.songsTexView);
checkbox = (CheckBox) v.getTag(R.id.checkBox1);
  Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
    }

    private String isCheckedOrNot(CheckBox checkbox) {
        if(checkbox.isChecked())
        return "is checked";
        else
        return "is not checked";
    }
    private List<Model> getModel() {

         String mp3Directory = "/Music";
         String directoryPath= Environment.getExternalStorageDirectory().getAbsolutePath()+mp3Directory;
         lst = getMP3Files(directoryPath); 

        //print in LogCat the list of .mp3:
        for(int a=0;a<lst.size();a++){
        list.add(new Model(lst.get(a).getName()));

        }
          return list;

            }

    public List<File> getMP3Files(String directory) {
        List<File> files = new ArrayList<File>();
        File folder = new File(directory);
        for (File file : folder.listFiles()) {
            if (file.isFile()) {
                if (file.getName().endsWith(".mp3") || file.getName().endsWith(".MP3")) {
                    files.add(file);
                }
            }
        }
        return files;
    }
    public void saveInSp(String key,boolean value){
        SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
        }
}

0 个答案:

没有答案