Android 将列表字符串值与数组列表进行比较

Android 将列表字符串值与数组列表进行比较,android,listview,Android,Listview,我在每一行中都有带有文本复选框的listview。我正在单击按钮将选定值保存在列表中,然后在打开应用程序时关闭应用程序。我正在检查列表中的所有值是否都在listview中。如果是,则我希望将这些值的复选框设置为选中。如何做到这一点?有人能帮我吗 public类MainActivity扩展了AppCompatActivity{ MyCustomAdapter dataAdapter=null; str序列,str1; 共享参考优先; @凌驾 创建时受保护的void(Bundle savedInst

我在每一行中都有带有文本复选框的listview。我正在单击按钮将选定值保存在列表中,然后在打开应用程序时关闭应用程序。我正在检查列表中的所有值是否都在listview中。如果是,则我希望将这些值的复选框设置为选中。如何做到这一点?有人能帮我吗

public类MainActivity扩展了AppCompatActivity{
MyCustomAdapter dataAdapter=null;
str序列,str1;
共享参考优先;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayListView();
选中按钮单击();
}
私有void displayListView()
{
//国家列表
ArrayList stateList=新的ArrayList();
州-州=新州(“AP”,“安得拉邦”,假);
stateList.add(_states);
_州=新州(“DL”、“德里”,假);
stateList.add(_states);
_状态=新状态(“GA”、“Goa”、false);
stateList.add(_states);
_国家=新国家(“JK”、“查谟和克什米尔”,假);
stateList.add(_states);
_州=新州(“卡”、“卡纳塔克邦”,假);
stateList.add(_states);
_州=新州(“吉隆坡”、“喀拉拉邦”,假);
stateList.add(_states);
_州=新州(“RJ”,“拉贾斯坦邦”,假);
stateList.add(_states);
_州=新州(“WB”,“西孟加拉邦”,假);
stateList.add(_states);
dataAdapter=新的MyCustomAdapter(this,R.layout.state\u info,stateList);
ListView ListView=(ListView)findViewById(R.id.listView1);
setAdapter(dataAdapter);
}
私有无效检查按钮单击()
{
按钮myButton=(按钮)findViewById(R.id.findSelected);
myButton.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
StringBuffer responseText=新的StringBuffer();
responseText.append(“选择了以下内容…\n”);
StringBuilder sb=新的StringBuilder();
ArrayList stateList=dataAdapter.stateList;
对于(int i=0;i 0){
str=sb.substring(0,sb.length()-1);
}
}
}
Toast.makeText(getApplicationContext(),
responseText,Toast.LENGTH_LONG).show();
系统输出打印项次(str);
pref.edit().putString(“key”,str).apply();
}
});
}
}
公共类MyCustomAdapter扩展了ArrayAdapter{
公共ArrayList状态列表;
公共MyCustomAdapter(上下文上下文、int textViewResourceId、ArrayList状态列表)
{
super(上下文、textViewResourceId、状态列表);
this.stateList=新的ArrayList();
this.stateList.addAll(stateList);
这是.getSelectedList();
}
私有类视窗持有者
{
文本视图代码;
复选框名称;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
ViewHolder=null;
Log.v(“ConvertView”,String.valueOf(position));
if(convertView==null)
{
LayoutInflater vi=(LayoutInflater)getContext().getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
convertView=vi.充气(R.layout.state_info,null);
holder=新的ViewHolder();
holder.code=(TextView)convertView.findViewById(R.id.code);
holder.name=(复选框)convertView.findViewById(R.id.checkBox1);
convertView.setTag(支架);
holder.name.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v)
{
复选框cb=(复选框)v;
States _state=(States)cb.getTag();
/*Toast.makeText(getApplicationContext(),“单击复选框:”+cb.getText()+“是”+cb.isChecked(),
Toast.LENGTH_LONG).show()*/
_state.setSelected(cb.isChecked());
}
});
}
其他的
{
holder=(ViewHolder)convertView.getTag();
}
States state=stateList.get(位置);
holder.code.setText(“(“+state.getCode()+”);
holder.name.setText(state.getName());
holder.name.setChecked(state.isSelected());
持有者名称设置标签(状态);
返回视图;
}
私有void getSelectedList(){
pref=get Context().getSharedReferences(“MYPref”,Context.MODE\u PRIVATE);
String text=pref.getString(“键”,“键”);
List selectedList=Arrays.asList(text.split(“,”);
for(状态:this.stateList){
if(selectedList.contains(state.getName())){
state.setSelected(true);
}
}
}
}
公共类状态{
字符串代码=null;
字符串名称=null;
所选布尔值=false;
公共状态(字符串代码、字符串名称、选择的布尔值){
this.code=代码;
this.name=名称;
this.selected=selected;
}
公共字符串getCode(){
返回码;
}
公共无效设置码(字符串码){
this.code=代码;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公选{
返回选中的;
}
已选择公共无效设置(已选择布尔值){
this.selected=selected;
}
}
检查按钮中单击

str = sb.toString();
pref.edit().putString("key", str).apply();

我理解你的问题。让我们在适配器中做一个小的更改

public class MyCustomAdapter extends ArrayAdapter<States> {

    public ArrayList<States> stateList;

    public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<States> stateList)
    {
        super(context, textViewResourceId, stateList);
        this.stateList = new ArrayList<States>();
        this.stateList.addAll(stateList);
        this.getSelectedList();

    }

    private void getSelectedList()
    {

        pref = context.getSharedPreferences("MYPref", Context.MODE_PRIVATE);
        String text = pref.getString("key","");
        List<String> selectedList = Arrays.asList(text.split(","));

        for (State state: this.stateList) {
            if (selectedList.contains(state.getName())) {
                state.setSelected(true);
            }
        }
    }
...
}
公共类MyCustomAdapter扩展了ArrayAdapter{
公共ArrayList状态列表;
公共MyCustomAdapter(上下文,
str = sb.toString();
pref.edit().putString("key", str).apply();
public class MyCustomAdapter extends ArrayAdapter<States> {

    public ArrayList<States> stateList;

    public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<States> stateList)
    {
        super(context, textViewResourceId, stateList);
        this.stateList = new ArrayList<States>();
        this.stateList.addAll(stateList);
        this.getSelectedList();

    }

    private void getSelectedList()
    {

        pref = context.getSharedPreferences("MYPref", Context.MODE_PRIVATE);
        String text = pref.getString("key","");
        List<String> selectedList = Arrays.asList(text.split(","));

        for (State state: this.stateList) {
            if (selectedList.contains(state.getName())) {
                state.setSelected(true);
            }
        }
    }
...
}