比较列表字符串值与数组列表

时间:2016-08-02 07:15:16

标签: android listview

我在每行的复选框中都有带有文本的列表视图。我在按钮单击时将所选值保存在列表中,然后在我打开应用程序时关闭应用程序我正在检查列表视图中列表的所有值是否存在。如果是,那么我想将这些值的复选框设置为选中。怎么做?任何人都可以帮助我吗?

public class MainActivity extends AppCompatActivity {

    MyCustomAdapter dataAdapter = null;
    String str, str1;
    SharedPreferences pref;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        displayListView();

        checkButtonClick();


    }

    private void displayListView()
    {





        //Array list of countries
        ArrayList<States> stateList = new ArrayList<States>();

        States _states = new States("AP","Andhra Pradesh",false);
        stateList.add(_states);
        _states = new States("DL","Delhi",false);
        stateList.add(_states);
        _states = new States("GA","Goa",false);
        stateList.add(_states);
        _states = new States("JK","Jammu & Kashmir",false);
        stateList.add(_states);
        _states = new States("KA","Karnataka",false);
        stateList.add(_states);
        _states = new States("KL","Kerala",false);
        stateList.add(_states);
        _states = new States("RJ","Rajasthan",false);
        stateList.add(_states);
        _states = new States("WB","West Bengal",false);
        stateList.add(_states);






        dataAdapter = new MyCustomAdapter(this,R.layout.state_info, stateList);
        ListView listView = (ListView) findViewById(R.id.listView1);

        listView.setAdapter(dataAdapter);


    }

    private void checkButtonClick()
    {

        Button myButton = (Button) findViewById(R.id.findSelected);

        myButton.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {

                StringBuffer responseText = new StringBuffer();
                responseText.append("The following were selected...\n");

                StringBuilder sb = new StringBuilder();

                ArrayList<States> stateList = dataAdapter.stateList;



                for(int i=0;i<stateList.size();i++)
                {

                    States state = stateList.get(i);

                    if(state.isSelected())

                    {



                        responseText.append("\n" + state.getName());


                        sb.append(state.getName());

                        sb.append(",");
                        if (sb.length() > 0 ) {
                             str = sb.substring(0, sb.length()-1);
                        }



                    }
                }

                Toast.makeText(getApplicationContext(),
                        responseText, Toast.LENGTH_LONG).show();

                System.out.println(str);

                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 class ViewHolder
    {
        TextView code;
        CheckBox name;
    }

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

        ViewHolder holder = null;

        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null)
        {

            LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = vi.inflate(R.layout.state_info, null);

            holder = new ViewHolder();
            holder.code = (TextView) convertView.findViewById(R.id.code);
            holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);

            convertView.setTag(holder);

            holder.name.setOnClickListener( new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    CheckBox cb = (CheckBox) v;
                    States _state = (States) cb.getTag();

                 /*   Toast.makeText(getApplicationContext(), "Clicked on Checkbox: " + cb.getText() + " is " + cb.isChecked(),
                            Toast.LENGTH_LONG).show();*/

                    _state.setSelected(cb.isChecked());
                }
            });



        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }


        States state = stateList.get(position);



        holder.code.setText(" (" + state.getCode() + ")");
        holder.name.setText(state.getName());
        holder.name.setChecked(state.isSelected());

        holder.name.setTag(state);

        return convertView;
    }

     private void getSelectedList(){

         pref = get Context().getSharedPreferences("MYPref", Context.MODE_PRIVATE);
String text = pref.getString("key","");
List<String> selectedList = Arrays.asList(text.split(","));
for(States state: this.stateList){
    if(selectedList.contains(state.getName())){
       state.setSelected(true);
    }


 }

      }

}
public class States  {

    String code = null;
    String name = null;
    boolean selected = false;

    public States(String code, String name, boolean selected) {
        this.code = code;
        this.name = name;
        this.selected = selected;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }
}

2 个答案:

答案 0 :(得分:0)

CREATE TABLE IF NOT EXISTS `blogs` (
  `blog_id` int(10) NOT NULL AUTO_INCREMENT,
  `blog_title` text NOT NULL,
  `blog_content` text NOT NULL,
  `created_on` datetime NOT NULL,// remove length here
  `created_by` int(20) NOT NULL,
  `updated_on` datetime NOT NULL,// remove length here
  `updated_by` int(20) NOT NULL,
  PRIMARY KEY (`blog_id`)
)Engine=InnoDB  AUTO_INCREMENT=14

删除这些行

for (int i = 0; i < stateList.size(); i++) {
    States state = stateList.get(i);
    if (list.contains(state.getName()))
       state.setSelected(true);
}
if (sb.length() > 0 ) { str = sb.substring(0, sb.length()-1); } 中的

checkButtonClick

答案 1 :(得分:0)

我理解你的问题。让我们在您的适配器中进行一些小改动。

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);
            }
        }
    }
...
}