I have been two days trying with the differents solutions that I've found here but any works for me. That I want to do is 'quite simple'.
I have a custom listview with a checkbox. I want to save with the sharedpreferences if the checkbox are enabled or disabled. Then, if the user select one checkbox this have to be enable when come back to the screen.
This is my code
This is my class:
public class NewspaperList extends AppCompatActivity {
private MyCustomAdapter dataAdapter = null;
private WebView browser;
private static SharedPreferences preferences;
public static final String MARCA = "periodico.marca";
public static final String DIARIO_AS = "periodico.as";
private String marca;
private String as;
CheckBox cb;
TextView newspaperName;
CheckBox checkbox;
ArrayList<Country> countryList;
private ArrayList<CheckBox> mCheckBoxes = new ArrayList<CheckBox>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newspaper_listview);
//Generate list View from ArrayList
displayListView();
}
private void displayListView() {
//Array list of countries
countryList = new ArrayList<Country>();
Country country = new Country("Marca","http://ift.tt/1SsDuMZ", false);
countryList.add(country);
country = new Country("As","http://as.com/futbol/", false);
countryList.add(country);
AdView mAdView = (AdView) findViewById(R.id.adViewList);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
//create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this, R.layout.newspaper_list_row, countryList);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
Country country = (Country) parent.getItemAtPosition(position);
if (country.getName().equals("Marca")) {
Intent myIntent = new Intent(NewspaperList.this, Webview.class);
myIntent.putExtra("newspaper_url", country.getUrl());
startActivity(myIntent);
} else {
}
}
});
}
private class MyCustomAdapter extends ArrayAdapter<Country> {
private ArrayList<Country> countryList;
public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<Country> countryList) {
super(context, textViewResourceId, countryList);
this.countryList = new ArrayList<Country>();
this.countryList.addAll(countryList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService( Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.newspaper_list_row, null);
newspaperName = (TextView) convertView.findViewById(R.id.newspaperName);
checkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);
Country country = countryList.get(position);
checkbox.setTag(country.getName());
preferences = PreferenceManager.getDefaultSharedPreferences(NewspaperList.this);
marca = preferences.getString(MARCA, "");
mCheckBoxes.add(checkbox);
if (checkbox.getTag().equals(marca)) {
checkbox.setChecked(true);
}else if (checkbox.getTag().equals(marca)){
checkbox.setChecked(true);
}
checkbox.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Country country = (Country) cb.getTag();
if (((CheckBox)v).isChecked()) {
for (int i = 0; i < mCheckBoxes.size(); i++) {
if (mCheckBoxes.get(i) == v){
addPreferencesAndDataBase(country.getName());
}else{
}
}
}
else {
}
}
});
}
else {
}
Country country = countryList.get(position);
newspaperName.setText(country.getName());
checkbox.setChecked(country.isSelected());
checkbox.setTag(country);
return convertView;
}
}
public void addPreferencesAndDataBase(String name){
SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = data.edit();
if(name.equals("Marca")){
Toast.makeText(getApplicationContext(), name , Toast.LENGTH_SHORT).show();
editor.putString(MARCA, name);
editor.commit();
}else if(name.equals("As")){
Toast.makeText(getApplicationContext(), name , Toast.LENGTH_SHORT).show();
editor.putString(as, name);
editor.commit();
}
}
public void getAllPreferences(){
preferences = PreferenceManager.getDefaultSharedPreferences(this);
marca = preferences.getString(MARCA, "");
as = preferences.getString(DIARIO_AS, "");
}
}
Aucun commentaire:
Enregistrer un commentaire