使用Sharedpreference for Recyclerview Card中包含的Toggle / Checkbox

时间:2018-06-04 15:57:07

标签: android

我制作了一个小型应用,可以在打开和关闭切换时发送消息。 生成要由外部解析器读取的消息。

然而,作为我的第一个应用程序,我无法定位共享阅读器阅读器并了解在何处实现该方法以根据共享偏好中保存的内容更新按钮的状态。

我知道相关领域是:

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder>  {

private LayoutInflater inflater;
private ArrayList<Model> imageModelArrayList;
private ArrayList<Model> arraylist;
private Context context;
private String user;

public Adapter(Context context, ArrayList<Model> imageModelArrayList, String user){

    inflater = LayoutInflater.from(context);
    this.imageModelArrayList = imageModelArrayList;
    this.arraylist = new ArrayList<Model>();
    this.arraylist.addAll(MainActivity.imageModelArrayList);
    this.user= user;
    this.context = context; 
}

@Override
public Adapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.rv_item, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(final Adapter.MyViewHolder holder, final int position) {

    holder.time.setText(imageModelArrayList.get(position).getName());
    holder.chkSelected.setChecked(imageModelArrayList.get(position).isSelected());
    holder.chkSelected.setTag(imageModelArrayList.get(position));

    final String[] t = {null};
    holder.chkSelected.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ToggleButton tb = (ToggleButton) v;
            Model contact = (Model) tb.getTag();

            contact.setSelected(tb.isChecked());
            t[0] = imageModelArrayList.get(position).getName();
            imageModelArrayList.get(position).setSelected(tb.isChecked());

            Toast.makeText(
                    v.getContext(),
                      t[0] + tb.getText() + " is "
                            + "sent.", Toast.LENGTH_LONG).show();

            new SimpleMail().sendEmail("vladyslav.y@gmail.com", "Toggle DATA", user+"  "+t[0]+"  " +tb.getText());
        }
    });
}

这是我的主要

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {

private RecyclerView recyclerView;
public static ArrayList<Model> imageModelArrayList;
private Adapter adapter;
private SearchView editsearch;
private Button btnSelection;
private List<Model> mArrayList;
private String[] myImageNameList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.recycler);
    String user=getIntent().getExtras().getString("user");


    myImageNameList =new String[200];
    for (int i=0; i<200;i++ ){
        for (int j=0; j<=i; j+=2) {
            myImageNameList[i] = new String("C-" + "Parque"+j + "-" +  3*i);
        }
    }

    imageModelArrayList = populateList();
    Log.d("hjhjh", imageModelArrayList.size() + "");
    adapter = new Adapter(this, imageModelArrayList, user);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));

    recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new ClickListener() {


        @Override
        public void onClick(View view, final int position) {
                //Edit
            //Toast.makeText(MainActivity.this, imageModelArrayList.get(position).getName(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onLongClick(View view, int position) {
        }
    }));


    editsearch = (SearchView) findViewById(R.id.search);
    editsearch.setOnQueryTextListener(this);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            open_email_activity();
        }
    });
}

我不知道是否应该在MainActivity中实现该方法

  recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new ClickListener() {


        @Override
        public void onClick(View view, final int position) {
                //Edit
            //Toast.makeText(MainActivity.this, imageModelArrayList.get(position).getName(), Toast.LENGTH_SHORT).show();
        }

或在主要的Oncreate。

或者也许在适配器中:

holder.chkSelected.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ToggleButton tb = (ToggleButton) v;
            Model contact = (Model) tb.getTag();

            contact.setSelected(tb.isChecked());
            t[0] = imageModelArrayList.get(position).getName();
            imageModelArrayList.get(position).setSelected(tb.isChecked());

            Toast.makeText(
                    v.getContext(),
                      t[0] + tb.getText() + " is "
                            + "sent.", Toast.LENGTH_LONG).show();

            new SimpleMail().sendEmail("vladyslav.y@gmail.com", "Toggle DATA", user+"  "+t[0]+"  " +tb.getText());
        }
    });

提前感谢您,并对基本问题感到抱歉,但应用程序编译并运行严格,直到我尝试将Sharedpreference置于其中任何一个字段中。

0 个答案:

没有答案