共享首选项可以应用于自定义ListView吗?

时间:2016-03-30 07:56:51

标签: android android-sharedpreferences

我的Custom list view中有共享偏好。我使用它来保持按钮上的数字,但我有一个条件,当按钮变为0时行消失但是当我向下滚动它返回到它的旧形式并再次对它的用户可见。是否可以为SharedPreference中的视图应用相同的逻辑,以保持最后的情况?

我的代码如下:

import static android.view.View.INVISIBLE;

public class MainActivity extends Activity {

    public  MyAdapter adapter;

    Context context;

    public  ListView list;

    public int t[];

    public SharedPreferences prefs;
    public SharedPreferences.Editor edit;

    int [] btnNums={100,150,94,72,206,489,1481,731,131,91,145,137,662,770,196,351,258,131,180,1281};

    int[] images = {R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7, R.drawable.a8, R.drawable.a9,
            R.drawable.a10, R.drawable.a11, R.drawable.a12, R.drawable.a13, R.drawable.a14, R.drawable.a15, R.drawable.a16, R.drawable.a17, R.drawable.a18, R.drawable.a19, R.drawable.a20, R.drawable.a21};
    String[] exp;
    String[] mean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit= this.getSharedPreferences("NAME", Context.MODE_APPEND).edit();
        prefs = getSharedPreferences("NAME", Context.MODE_APPEND);

        Resources  res=getResources();
        exp= res.getStringArray(R.array.names);
        mean=res.getStringArray(R.array.anlam);
        list= (ListView) findViewById(R.id.listView);


//
//        edit.putInt("Count", btnNums.length);
//        int count = 0;
//        for (int i: btnNums){
//            edit.putInt("IntValue_" + count++, i);
//        }
//        edit.commit();
//

        int[] ret;


        int count1 = prefs.getInt("Count", 0);
        ret = new int[count1];
        for (int i = 0; i < count1; i++){
            ret[i] =prefs.getInt("IntValue_"+ i, i);

        }

        t=ret;

        if(t!=null)
        {
            adapter=new MyAdapter(this,exp,images,mean,t);
        }else
        {
            adapter=new MyAdapter(this,exp,images,mean,btnNums);
        }

        list.setAdapter(adapter);

        context=getApplicationContext();
    }
}

class  MyAdapter  extends ArrayAdapter<String>{

    int [] images;
    String [] titleArray;
    String [] descriptionArray;
    int [] btnNums;
    MainActivity ma;

    public MyAdapter(MainActivity m, String[] titles, int imgs[], String[] descp, int[] btnNum ){
        super(m, R.layout.single_row, R.id.textView,titles);
        this.images=imgs;
        this.titleArray=titles;
        this.descriptionArray=descp;
        btnNums=btnNum;
        this.ma=m;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater= (LayoutInflater) ma.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View row=inflater.inflate(R.layout.single_row, parent, false);

        final TextView myTitle=(TextView)row.findViewById(R.id.textView);

        myTitle.findViewById(R.id.textView);

        final TextView descp=(TextView)row.findViewById(R.id.textView2);
        final ImageView imageView = (ImageView) row.findViewById(R.id.imageView);
        final Button button = (Button) row.findViewById(R.id.angry_btn);
        final Vibrator a = (Vibrator) ma.getSystemService(Context.VIBRATOR_SERVICE);

        if(ma.t!=null)
        {
            for(int i=0; i<21;i++){
                button.setText("" + ma.t[position]);
                imageView.setImageResource(images[position]);
                myTitle.setText(titleArray[position]);
                descp.setText(descriptionArray[position]);
            }
        }else
        {
            for(int i=0; i<21;i++){
                button.setText("" + btnNums[position]);
                imageView.setImageResource(images[position]);
                myTitle.setText(titleArray[position]);
                descp.setText(descriptionArray[position]);
            }
        }


        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                btnNums[position]--;

                storeIntArray(btnNums);

                a.vibrate(30);
                if (btnNums[position] == 0) {
                    button.setEnabled(false);
                    button.setVisibility(INVISIBLE);
                    row.setVisibility(INVISIBLE);

                }

                int temp[]=getFromPrefs();
                ma.t=temp;

                for (int i = 0; i < 21; i++) {
                    button.setText("" + temp[position]);
                    imageView.setImageResource(images[position]);
                    myTitle.setText(titleArray[position]);
                    descp.setText(descriptionArray[position]);

                }
            }
        });

        return row;

    }

    @Override
    public long getItemId(int position) {

        return super.getItemId(position);
    }

    public  void storeIntArray(int[] array){

        ma.edit.putInt("Count", array.length);
        int count = 0;
        for (int i: array){
            ma.edit.putInt("IntValue_" + count++, i);
        }
        ma.edit.commit();
    }

    public int[] getFromPrefs(){

        int[] ret;


        int count = ma.prefs.getInt("Count", 0);
        ret = new int[count];
        for (int i = 0; i < count; i++){
            ret[i] =ma.prefs.getInt("IntValue_"+ i, i);
        }
        return ret;
    }

}

1 个答案:

答案 0 :(得分:0)

首先,请格式化您的代码,使其更具可读性。

问题:Shared Preference是否可以应用于自定义ListView

答案:可以在Listview中使用/访问共享偏好设置。

在阅读您的代码后,我认为问题在于您Listview and its adapter的实施。滚动下来后它回到原始状态的原因。请注意,滚动时始终会调用GetView。 您可以参考此主题:ListView & ViewHolder Pattern