2016-08-02 13 views
0

Ich möchte die Positionen der ausgewählten Kontrollkästchen bei Klick auf die Schaltfläche in der Datenbank speichern und die App schließen. Wenn Sie diese Positionen verwenden, stellen Sie die ausgewählten Status von Kontrollkästchen wieder her, wenn ich die App öffne. Kann mir jemand dabei helfen? Positionen des ausgewählten Kontrollkästchens in sqlite db speichern und den Status des Kontrollkästchens wiederherstellen

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

 
     pref = getSharedPreferences("MYPref", Context.MODE_PRIVATE); 
 
     str1 = pref.getString("key",""); 
 
     List<String> list = Arrays.asList(str1.split(",")); 
 
     System.out.println(list); 
 

 
     displayListView(); 
 

 
     checkButtonClick(); 
 

 

 
    } 
 

 
    private void displayListView() 
 
    { 
 

 
     //HERE I WANT TO RETRIEVE THE POSITIONS TO SET EARLIER SELECTED CHECKOX AGAIN SELECTED 
 
\t \t 
 
     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"); 
 

 
       
 

 
       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()); 
 

 

 
         // HERE I WANT TO SAVE POSITIONS 
 

 

 

 
        } 
 
       } 
 

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

 
       
 

 

 
      } 
 
     }); 
 
    } 
 

 

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

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

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

+0

so sind Sie Ihre SharedPreferences Datenbank? Warum verwenden Sie nicht SQLiteDatabase – kggoh

+0

Können Sie mir zeigen, wie Sie die Positionen speichern und checkboxes mit sqlite wiederherstellen? –

Antwort

0

Sind Ihre Daten riesig sein? Wenn Ja, benutze SQLlite. Wenn nur das, was Sie angegeben haben, nur für das Laden von App-Parametern, ist Share Preference besser. Probieren Sie die folgenden Funktionen aus.

public static <T> T readPreference(Class<T> clazz, String key) { 
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Application.getInstance()); 
     if (clazz.equals(String.class)) { 
      return (T) preferences.getString(key, ""); 
     } else if (clazz.equals(Integer.class)) { 
      return (T) (Integer) preferences.getInt(key, 0); 
     } else if (clazz.equals(Boolean.class)) { 
      return (T) (Boolean) preferences.getBoolean(key, false); 
     } else if (clazz.equals(Float.class)) { 
      return (T) (Float) preferences.getFloat(key, -0.0f); 
     } 

     return null; 
    } 

public static void writePreference(String key, Object value) { 
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Application.getInstance()); 
     SharedPreferences.Editor edit = preferences.edit(); 
     if (value instanceof Integer) { 
      edit.putInt(key, (Integer) value); 
     } else if (value instanceof Boolean) { 
      edit.putBoolean(key, (Boolean) value); 
     } else if (value instanceof Float) { 
      edit.putFloat(key, (Float) value); 
     } else if (value instanceof Long) { 
      edit.putLong(key, (Long) value); 
     } else if (value instanceof String) { 
      edit.putString(key, (String) value); 
     } 

     edit.apply(); 
    } 
+0

Ja, meine Daten von listview stammen vom Webservice. Also könnte es groß sein. –

+0

Dann müssen Sie sqllite verwenden: [link] http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ – kggoh

Verwandte Themen