将复选框的状态保存在列表视图中

时间:2018-08-01 10:12:28

标签: java android android-recyclerview

我刚接触Android开发,需要一些帮助。

我想做的是将checkboxes放在列表视图中的项目旁边,并在我关闭并重新启动应用程序时保存这些选择,我曾经sharedPreferences和回收商感到困惑查看零件,希望有人可以帮助我,这是我正在使用的代码:

MainActivity:

public class MainActivity extends AppCompatActivity {

Toolbar mToolbar;
ListView mListView;
TextView mTextView;

String[] countryNames = {"Canberra, Australia", "Sydney, Australia" 
,"Brazil", "China", "France", "Germany", "India", "Italy"
            , "Mexico", "Russia", "Spain", "US"};

    int[] countryFlags = {R.drawable.flag_australia,
            R.drawable.flag_australia,
            R.drawable.flag_brazil,
            R.drawable.flag_china,
            R.drawable.flag_france,
            R.drawable.flag_germany,
            R.drawable.flag_india,
            R.drawable.flag_italy,
            R.drawable.flag_mexico,
            R.drawable.flag_russia,
            R.drawable.flag_spain,
            R.drawable.flag_us};

    String [] countryDetails = {// Canberra, Australia

"Capital of Australia" + System.lineSeparator()+"Population - 
356,585" + " " + System.lineSeparator() +"Nearest Airport : Canberra 
Airport"+ System.lineSeparator(),

//Sydney Australia
"Major City in Australia" + 
System.lineSeparator()+"Population: 4.02 Million" + System.lineSeparator() 
+" Nearest Airport: Western Sydney Airport",
            "Brazil Here",
            "Chine Here",
            "France Here",
            "Germany Here",
            "India Here",
            "Italy Here",
            "Mexico",
            "Russia",
            "Spain",
            "US" };

    @Override
protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mToolbar = (Toolbar) findViewById(R.id.toolbar);
            mToolbar.setTitle(getResources().getString(R.string.app_name));

            mListView = (ListView) findViewById(R.id.listview);
            mTextView = (TextView) findViewById(R.id.textView);

            MyAdapter myAdapter = new MyAdapter(MainActivity.this, 
            countryNames, countryFlags, countryDetails);

            mListView.setAdapter(myAdapter);
            mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent mIntent = new Intent(MainActivity.this, DetailActivity.class);
                    mIntent.putExtra("countryName", countryNames[i]);
                    mIntent.putExtra("countryFlag", countryFlags[i]);
                    mIntent.putExtra("countryDetails", countryDetails[i]);
                    startActivity(mIntent);
                }
            });
        }
    }

此布局的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:descendantFocusability="blocksDescendants">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        app:srcCompat="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
       />

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:focusable="false"
        android:text="Favorite" />

</LinearLayout>             

目前尚不确定还包括什么,将不胜感激

好吧,我设法达到了自定义适配器的目的

  public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mViewHolder = new ViewHolder();

        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) mContext.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.listview_item, 
parent, false);
            mViewHolder.mFlag = (ImageView) 
convertView.findViewById(R.id.imageView);
            mViewHolder.mName = (TextView) 
convertView.findViewById(R.id.textView);
            mViewHolder.mCheckBox = 
convertView.findViewById(R.id.check_Box);

        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }
        mContext.getSharedPreferences("FreshStart",0);
        mViewHolder.mFlag.setImageResource(flags[position]);
        mViewHolder.mName.setText(names[position]);

        SharedPreferences sp = mContext.getSharedPreferences("FreshStart", 0);
        boolean shouldBeChecked = sp.getBoolean(names[position], false);
        mViewHolder.mCheckBox.setTag(names[position]);
        convertView.setTag(R.layout.listview_item);
        mViewHolder.mCheckBox.setChecked(shouldBeChecked);


        mViewHolder.mCheckBox.setOnCheckedChangeListener(new 
        CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {
                    buttonView.setChecked(true);
                    Toast.makeText(mContext, "Saved as Favorite", Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(mContext, "No Longer set as Favorite", Toast.LENGTH_SHORT).show();
                }

            }
        });

        return convertView;
    }
static class ViewHolder {
    ImageView mFlag;
    TextView mName;
    CheckBox mCheckBox;
}

但是现在我有一个问题,当我滚动或更改活动时无法保存复选框状态,现在不确定在if(isChecked)部分中该如何做

0 个答案:

没有答案