Here is my main class and Adapter together
public class MainActivity extends Activity {
public MyAdapter adapter;
private View view;
Context context;
public ListView list;
public int t[];
public SharedPreferences prefs;
public SharedPreferences.Editor edit;
public SharedPreferences preferences;
public SharedPreferences.Editor editor;
int [] btnNums={66,150,94,72,206,489,1481,731,131,91,145,137,662,770};
int [] tuesdaynum={351,14,308,19,134,514,305,68,104,129,62,47,409,630};
int [] wednesdaynum={744,131,184,846,37,336,812,88,1020,1286,1106,551,202,156};
int [] thursdaynum={206,662,731,1281,801,351,117,770,302,526,212,1100,1060,1100};
int [] fridaynum={232,998,209,80,73,270,312,55,110,78,209,114,161,1001};
int [] saturdaynum={20,48,573,319,108,66,116,500,46,68,286,201,256,113};
int [] sundaynum={137,148,56,124,68,490,18,156,14,48,20,86,514,298};
int [] btnNumsrefs={66,150,94,72,206,489,1481,731,131,91,145,137,662,770};
int yesterday;
public void storeYesterday(Calendar a){
yesterday=a.get(Calendar.DAY_OF_MONTH);
preferences=getSharedPreferences("yesterday",0);
editor=preferences.edit();
editor.putInt("yesterday",yesterday);
editor.commit();
}
public Integer getFromYesterday(){
preferences=getSharedPreferences("yesterday",0);
int get =preferences.getInt("yesterday", 0);
return get;
}
int[] images = {R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7, R.drawable.a8, R.drawable.a9,
R.drawable.a10, R.drawable.a11, R.drawable.a12, R.drawable.a13, R.drawable.a14};
int[] tuesday = {R.drawable.elkahhar, R.drawable.elvehhab, R.drawable.errezzak, R.drawable.elvahid, R.drawable.essamed, R.drawable.elkabid, R.drawable.elkadir, R.drawable.elhakem, R.drawable.eladl,
R.drawable.ellatif, R.drawable.elbatin, R.drawable.elvali, R.drawable.ettevvab, R.drawable.elmuntekim};
int[] wednesday = {R.drawable.elmuktedir, R.drawable.esselam, R.drawable.elmukaddim, R.drawable.elmuaahir, R.drawable.elevvel, R.drawable.elmusavvir, R.drawable.elhabir, R.drawable.elhalim, R.drawable.elazim,
R.drawable.elgafur, R.drawable.ezzahir, R.drawable.elmuteali, R.drawable.elberr, R.drawable.elafuv };
int[] thursday = {R.drawable.elcebbar, R.drawable.elmutekebbir2, R.drawable.elhaliik, R.drawable.elgafffar, R.drawable.ellahir, R.drawable.errafi, R.drawable.elmuiz, R.drawable.elmuzil, R.drawable.elbasir,
R.drawable.essekur, R.drawable.malikulmulk, R.drawable.zulcelalilikram, R.drawable.elganiy, R.drawable.elmugni };
int[] friday = {R.drawable.elkebir, R.drawable.elhafiz, R.drawable.elmukit, R.drawable.elhasib, R.drawable.elcelil, R.drawable.elkerim, R.drawable.elrakib, R.drawable.elmucib, R.drawable.elaliy,
R.drawable.elhakim, R.drawable.elmuksit, R.drawable.elcami, R.drawable.elmani, R.drawable.eddar};
int[] saturday = {R.drawable.elvedud, R.drawable.elmecid, R.drawable.elbais, R.drawable.essehid, R.drawable.elhak, R.drawable.elvekil, R.drawable.elkaviy, R.drawable.elmetin, R.drawable.elveliy,
R.drawable.elhamid, R.drawable.errauf, R.drawable.ennafi, R.drawable.ennur, R.drawable.elbaki};
int[] sunday = {R.drawable.elvasi, R.drawable.elmuhsi, R.drawable.elmubdi, R.drawable.elmuid, R.drawable.elmuhyi, R.drawable.elmumit, R.drawable.elhay, R.drawable.elkayyum, R.drawable.elvacid,
R.drawable.elmacid, R.drawable.elhadi, R.drawable.elbedi, R.drawable.erresid, R.drawable.essabur };
String[] exp;
String[] exp2;
String[] exp3;
String[] exp4;
String[] exp5;
String[] exp6;
String[] exp7;
String[] mean;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit= this.getSharedPreferences("NAME", Context.MODE_APPEND).edit();
prefs = getSharedPreferences("NAME", Context.MODE_APPEND);
view=getWindow().getDecorView().getRootView();
Admob.createLoadBanner(getApplicationContext(), view);
Admob.createLoadInterstitial(getApplicationContext(), null);
Resources res=getResources();
exp= res.getStringArray(R.array.names);
exp2= res.getStringArray(R.array.faz2);
exp3= res.getStringArray(R.array.faz3);
exp4= res.getStringArray(R.array.faz4);
exp5= res.getStringArray(R.array.faz5);
exp6= res.getStringArray(R.array.faz6);
exp7= res.getStringArray(R.array.faz7);
mean=res.getStringArray(R.array.anlam);
list= (ListView) findViewById(R.id.listView);
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
Calendar cal2=Calendar.getInstance();
cal2.setTime(new Date());
int today = cal.get(Calendar.DAY_OF_MONTH);
if(today==Calendar.Monday){
adapter.setData(this,exp3,thursday,mean,thursdaynum);
}else if(today==Calendar.TUESDAY){
adapter.clear();
adapter=new MyAdapter(this,exp2,tuesday,mean,tuesdaynum);
}//goes on
if(getFromYesterday()==null){
storeYesterday(cal);
}else {
if(today!=getFromYesterday()) {
storeYesterday(cal);
adapter = new MyAdapter(this, exp, images, mean, t);
storeYesterday(cal);
edit.putInt("Count", btnNumsrefs.length);
int count = 0;
for (int i : btnNums) {
edit.putInt("IntValue_" + count++, i);
}
}
edit.commit();
}
int[] ret;
Integer count1 = prefs.getInt("Count", 0);
if(count1!=null) {
ret = new int[count1];
for (int i = 0; i < count1; i++) {
ret[i] = prefs.getInt("IntValue_" + i, i);
t=ret;
}
}
if(t!=null)
{
adapter = new MyAdapter(this, exp, images, mean, t);
}else
{
adapter = new MyAdapter(this, exp, images, mean, btnNums);
}
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
context=getApplicationContext();
}
}
class MyAdapter extends ArrayAdapter<String>{
int [] images;
String [] titleArray;
String [] descriptionArray;
int [] btnNums;
MainActivity ma;
public MyAdapter(MainActivity m, String[] titles, int imgs[], String[] descp, int[] btnNum ){
super(m, R.layout.single_row, R.id.textView,titles);
this.images=imgs;
this.titleArray=titles;
this.descriptionArray=descp;
btnNums=btnNum;
this.ma=m;
}
#Override
public View getView(final int position, final View convertView, ViewGroup parent) {
LayoutInflater inflater= (LayoutInflater) ma.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View row=inflater.inflate(R.layout.single_row, parent, false);
final TextView myTitle=(TextView)row.findViewById(R.id.textView);
myTitle.findViewById(R.id.textView);
final TextView descp=(TextView)row.findViewById(R.id.textView2);
final ImageView imageView = (ImageView) row.findViewById(R.id.imageView);
final Button button = (Button) row.findViewById(R.id.angry_btn);
final Vibrator a = (Vibrator) ma.getSystemService(Context.VIBRATOR_SERVICE);
if(ma.t!=null)
{
for(int i=0; i<14;i++){
if (btnNums[position] <= 0) {
button.setText("OK");
/* Integer index=(Integer) convertView.getTag();
ma.list.removeViewAt(index);
ma.adapter.notifyDataSetChanged();
*/
}else {
button.setText("" + ma.t[position]);
}
// button.setText("" + ma.t[position]);
imageView.setImageResource(images[position]);
myTitle.setText(titleArray[position]);
descp.setText(descriptionArray[position]);
}
}else
{
for(int i=0; i<14;i++){
if (btnNums[position] <= 0) {
button.setText("OK");
/* Integer index=(Integer) convertView.getTag();
ma.list.removeViewAt(index);
ma.adapter.notifyDataSetChanged();
*/
}else {
button.setText("" + btnNums[position]);
}
// button.setText("" + btnNums[position]);
imageView.setImageResource(images[position]);
myTitle.setText(titleArray[position]);
descp.setText(descriptionArray[position]);
}
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btnNums[position]--;
storeIntArray(btnNums);
a.vibrate(30);
int temp[]=getFromPrefs();
ma.t=temp;
for (int i = 0; i <14; i++) {
if (btnNums[position] <= 0) {
button.setText("OK");
/* Integer index=(Integer) convertView.getTag();
ma.list.removeViewAt(index);
ma.adapter.notifyDataSetChanged();
*/
}else {
button.setText("" + temp[position]);
}
imageView.setImageResource(images[position]);
myTitle.setText(titleArray[position]);
descp.setText(descriptionArray[position]);
}
}
});
return row;
}
In my app, I use custom list view with different datas. these datas come from arrays , I want to send new arrays via constructor to my adapter by checking date but it doesn't work .
after these codes I used these lines
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
Please help I am stucked.
Why don't you create a setData() method in your adapter where you're clearing the current data and setting the new one and then call the notifyDataSetChanged method.
This way you will not create and set the same adapter multiple times when you want to change the data.
I need to put a strikethrough on the text after the item has been checked. I found solutions that use setPaintFlags(descriptionView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);. I don't use a textview but instead use simple_list_item_multiple_choice for my listview so how do I solve this? Here is my entire code:
public class Surv_list extends Fragment {
final String[] OPSys = new String[]{"item1","item2","item3","item4"
};
ListView myList;
Button getChoice, clearAll;
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyUserChoice" ;
ArrayList<String> selectedItems = new ArrayList<String>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listlay, container, false);
myList = (ListView)rootView.findViewById(R.id.list);
ListView list = (ListView) rootView.findViewById(R.id.list);
clearAll = (Button)rootView.findViewById(R.id.clearall);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_multiple_choice, OPSys);
myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
myList.setAdapter(adapter);
list.setAdapter(adapter);
sharedpreferences = getActivity().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if(sharedpreferences.contains(MyPREFERENCES)){
LoadSelections();
}
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
SaveSelections();
}
});
clearAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ClearSelections();
}
});
return rootView;
}
private void SaveSelections() {
// save the selections in the shared preference in private mode for the user
SharedPreferences.Editor prefEditor = sharedpreferences.edit();
String savedItems = getSavedItems();
prefEditor.putString(MyPREFERENCES.toString(), savedItems);
prefEditor.commit();
}
private String getSavedItems() {
String savedItems = "";
int count = this.myList.getAdapter().getCount();
for (int i = 0; i < count; i++) {
if (this.myList.isItemChecked(i)) {
if (savedItems.length() > 0) {
savedItems += "," + this.myList.getItemAtPosition(i);
} else {
savedItems += this.myList.getItemAtPosition(i);
}
}
}
return savedItems;
}
private void LoadSelections() {
// if the selections were previously saved load them
if (sharedpreferences.contains(MyPREFERENCES.toString())) {
String savedItems = sharedpreferences.getString(MyPREFERENCES.toString(), "");
selectedItems.addAll(Arrays.asList(savedItems.split(",")));
int count = this.myList.getAdapter().getCount();
for (int i = 0; i < count; i++) {
String currentItem = (String) myList.getAdapter()
.getItem(i);
if (selectedItems.contains(currentItem)) {
myList.setItemChecked(i, true);
} else {
myList.setItemChecked(i, false);
}
}
}
}
private void ClearSelections() {
// user has clicked clear button so uncheck all the items
int count = this.myList.getAdapter().getCount();
for (int i = 0; i < count; i++) {
this.myList.setItemChecked(i, false);
}
// also clear the saved selections
SaveSelections();
}
}
any help would be very much appreciated.
You can create a custom adapter for your list view and in the getview method of the adpater take the handle of textview . Based on your condition you can combine the already available code to strikeout the text view.
for making your own view attributes in list view, you have to make a custom Adapter other than using default adapter. there is no way you can do this using default adapter. here you can learn how to make custom adapter. also I would suggest you to use RecyclerView instead of ListView
How would I use shared preferences to store the state of my checkbox for the next time the app is opened? I'm using a custom adapter so am guessing it has to be placed inside that but I'm not quite sure.
My Adapter:
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_adapter, values);
this.context = context;
this.values = values;
for (int i = 0; i < this.getCount(); i++) {
itemChecked.add(i, false);
}
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_adapter,
parent, false);
}
// in your code you search for the CheckBox with the id checkBox1 2 times so I assumed that you are referring to the same view.
CheckBox cBox = (CheckBox) rowView.findViewById(R.id.checkBox1);
cBox.setTextColor(0xFFFFFFFF);
cBox.setText(values[position]);
cBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
itemChecked.set(position, true);
// do some operations here
} else {
itemChecked.set(position, false);
// do some operations here
}
}
});
cBox.setChecked(itemChecked.get(position));
return rowView;
}
}
My main Activity:
public class TheKevinAndEricaBoxActivity extends Activity {
/** Called when the activity is first created. */
private String[] myString;
private String list;
private String[] myString2;
private String list2;
private static final Random rgenerator = new Random();
private static final Random rgenerator2 = new Random();
MediaPlayer mp;
final Context mContext = this;
final Context context = this;
private Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
addListenerOnButton();
myString = res.getStringArray(R.array.myArray);
list = myString[rgenerator.nextInt(myString.length)];
myString2 = res.getStringArray(R.array.myArray2);
list2 = myString2[rgenerator.nextInt(myString2.length)];
}
public void addListenerOnButton() {
final Context context2 = this;
ImageButton ibg = (ImageButton) findViewById(R.id.buttongallery);
ibg.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context2, App2Activity.class);
startActivityForResult(intent, 0);
}
});
ImageButton ib = (ImageButton) findViewById(R.id.imagebutton1);
ib.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View erica) {
AlertDialog.Builder b = new AlertDialog.Builder(
TheKevinAndEricaBoxActivity.this);
b.setMessage(myString[rgenerator.nextInt(myString.length)]);
b.setTitle(R.string.title1);
b.setIcon(R.drawable.menuiconerica);
b.setPositiveButton("Back",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});
Dialog d = b.create();
d.show();
}
});
ImageButton ib2 = (ImageButton) findViewById(R.id.imagebutton2);
ib2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View kevin) {
AlertDialog.Builder b = new AlertDialog.Builder(
TheKevinAndEricaBoxActivity.this);
b.setMessage(myString2[rgenerator2.nextInt(myString2.length)]);
b.setTitle(R.string.title2);
b.setIcon(R.drawable.menuiconkevin);
b.setPositiveButton("Back",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});
Dialog d = b.create();
d.show();
}
});
ImageButton Ib3 = (ImageButton) findViewById(R.id.imagebutton3);
Ib3.setOnClickListener(new View.OnClickListener() {
public void onClick(View lemonclick) {
mp = MediaPlayer.create(getApplicationContext(),R.raw.lemonspeech);
mp.start();
}
});
button = (Button) findViewById(R.id.button01);
// add button listener
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.list);
dialog.setTitle("The List");
// set the custom dialog components - text, image and button
//TextView text = (TextView) dialog.findViewById(R.id.TextView01);
//text.setText("Did you not read the button? :P i'm not finshed on this yet XD");
ListView listView = (ListView) findViewById(R.id.myList);
String[] values = new String[] { "value1", "value2", };
MobileArrayAdapter mAdapter = new MobileArrayAdapter(getBaseContext(), values);
ListView mListView = (ListView) dialog.findViewById(R.id.myList);
mListView.setAdapter(mAdapter);
Button dialogButton = (Button) dialog.findViewById(R.id.Button01);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
In the OnCLickListener for your Button add this:
//...
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.list);
dialog.setTitle("The List");
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefsEditor = prefs.edit();
String currentlyStored = prefs.getString("checked_list", null);
int[] savedStatus = null;
if (currentlyStored != null) {
String[] tmp = currentlyStored.split(",");
savedStatus = new int[tmp.length];
for (int i = 0; i < tmp.length; i++) {
savedStatus[i] = Integer.parseInt(tmp[i]);
}
}
adapter = new MobileArrayAdapter(this, soundnames, savedStatus);
ListView mListView = (ListView) dialog.findViewById(R.id.myList);
mListView.setAdapter(mAdapter);
//...
where:
private SharedPreferences prefs;
private SharedPreferences.Editor prefsEditor;
private MobileArrayAdapter adapter;
are fields in your class with the ListView(the adapter field will hold your adapter object that you set on the list).
Modify the constructor of your custom adapter like this:
public MobileArrayAdapter(Context context, String[] values,
int[] oldStatus) {
super(context, R.layout.adapters_simpleplay_row, values);
this.context = context;
this.values = values;
// make every CheckBox unchecked and then loop through oldStatus(if
// not null)
for (int i = 0; i < this.getCount(); i++) {
itemChecked.add(i, false);
}
if (oldStatus != null) {
for (int j = 0; j < oldStatus.length; j++) {
itemChecked.set(oldStatus[j], true);
}
}
}
Also add the following method in your custom adapter MobileArrayAdapter:
public ArrayList<Boolean> getCheckedStatus() {
return itemChecked;
}
Last in the listener for your dialogButton add this:
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String toStore = "";
ArrayList<Boolean> status = adapter.getCheckedStatus();
for (int i = 0; i < status.size(); i++) {
if (status.get(i)) {
toStore += i + ",";
}
}
prefsEditor.putString("checked_list", toStore.equals("") ? null
: toStore.substring(0, toStore.length() - 1));
prefsEditor.commit();
dialog.dismiss();
}
});
To save selections make a method saveSelections and call it in onPause() and onDestroy(), or create a Button to do the same for you...
Edit:
Since you are using a ListView which is MultipleChoice I suppose you can do this in onCreate...
listView = (ListView) findViewById(R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
android.R.id.text1, names));
Create these three methods outside onCreate()
private void LoadSelections() {
SharedPreferences sp = getPreferences(MODE_PRIVATE);
if (sp.contains(LOAD_LIST)) {
String savedItems = sp.getString(LOAD_LIST, "");
this.selectedItems.addAll(Arrays.asList(savedItems.split(",")));
int count = this.listView.getAdapter().getCount();
for (int i = 0; i < count; i++) {
String currentItem = (String) listView.getAdapter().getItem(i);
if (this.selectedItems.contains(currentItem)) {
this.listView.setItemChecked(i, true);
}
}
}
}
public void SaveSelections() {
SharedPreferences sp = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sp.edit();
String savedItems = getSavedItems();
prefEditor.putString(LOAD_LIST, savedItems);
prefEditor.commit();
}
private String getSavedItems() {
String savedItems = "";
int count = listView.getAdapter().getCount();
for (int i = 0; i < count; i++) {
if (listView.isItemChecked(i)) {
if (savedItems.length() > 0) {
savedItems += "," + listView.getItemAtPosition(i);
} else {
savedItems += listView.getItemAtPosition(i);
}
}
}
return savedItems;
}
Then in onPause(), do this:
#Override
protected void onPause() {
SaveSelections();
super.onPause();
}
Then finally in onCreate call this..
LoadSelections();
You can make a string of 0 & 1 and store it using shared preference. The number of checkboxes (or the number of view) you are using will be the length of string. You save it accordingly.
Eg:
String s[]="0000000000";
for (i=0;i<s.length();i++)
if (checkboxAtLocation(i)==true) //checkboxAtLocation() will return a boolean variable on basis of its checked or not
s[i]=1;
Now store this string.
On starting activity again, use this string to set the checkbox.
This is bit complex to implement but most efficient way as per my knowledge.
I hope it solves your doubt.
I am trying to restore the previous state of my Checkboxes. I have nearly 20 checkbox in my Activity and what i want is when i restart my app, all the checkbox should be checked if it was checked before exiting the app. As i am using a custom adapter so i found it very difficult to achieve.
Here is my adapter's code -
public class IconAdapter extends BaseAdapter
{
private Activity activity;
private Object[] data;
private ArrayList<HashMap<String,String>> listItems;
public static LayoutInflater inflater = null;
private PackageManager pm;
public ArrayList<Boolean> itemChecked = null;
public ArrayList<String> itemSelected = new ArrayList<String>();
public ArrayList<CheckBox> ctv = new ArrayList<CheckBox>();
//TextView textView;
CheckBox cb;
//ImageView imageView;
public CompleteTaskManager ctm = new CompleteTaskManager();
public IconAdapter(Activity a, ArrayList<HashMap<String,String>> items)
{
activity = a;
listItems = items;
data = items.toArray();
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pm = a.getPackageManager();
if(itemChecked==null){
itemChecked = new ArrayList<Boolean>();
for(int i = 0; i < items.size(); i++)
{
itemChecked.add(i,false);
}}
for(int i = 0; i < items.size(); i++)
{
itemSelected.add(i," ");
}
for(int i = 0; i < items.size(); i++)
{
cb = new CheckBox(a);
ctv.add(i,cb);
}
}
public int getCount() {
return listItems.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView textView;
public ImageView imageView;
public CheckBox checkBox;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = convertView;
final ViewHolder holder;
if(convertView==null)
{
row = inflater.inflate(R.layout.item, parent, false);
holder = new ViewHolder();
holder.textView = (TextView)row.findViewById(R.id.text1);
holder.checkBox = (CheckBox)row.findViewById(R.id.check); holder.checkBox.setChecked(itemChecked.get(position));
holder.imageView = (ImageView)row.findViewById(R.id.image);
row.setTag(holder);
}
else
{
holder = (ViewHolder)row.getTag();
}
String s = data[position].toString();
String[] tokens = s.split(",");
String[] mToken = tokens[0].split("=");
String taskName = mToken[1];
holder.textView.setText(taskName);
String[] mTokens = tokens[1].split("=");
final String pkgName = mTokens[1].substring(0, (mTokens[1].length() - 1));
holder.checkBox.setTag(position);
//this is how i am trying to restore the checked checkboxes.
**for(int i = 0; i < itemSelected.size(); i++)
{
if(itemSelected.contains(pkgName))
{
holder.checkBox.setChecked(true);
}
}**
ctv.set(position,holder.checkBox);
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton button, boolean b) {
Integer posClicked = (Integer)button.getTag();
if(b)
{
itemChecked.set(posClicked, true);
itemSelected.set(posClicked, pkgName);
}
else
{
itemChecked.set(posClicked,false);
itemSelected.set(posClicked, " ");
}
}
});
holder.checkBox.setChecked(itemChecked.get(position));
try{
Drawable icon = pm.getApplicationIcon(pkgName);
holder.imageView.setImageDrawable(icon);
}
catch (PackageManager.NameNotFoundException ne)
{
}
row.setId(position);
return row;
}
public boolean isChecked(int position)
{
return itemChecked.get(position);
}
public String getPkgName(int position)
{
return itemSelected.get(position);
}
public void removeItem(int position)
{
listItems.remove(position);
}
}
and here is code in which i am trying to restore my pkgNames. I picked pkgName because i have noticed position could be shuffled as my list is dynamic but pkgName related to each item will stay same.
#Override
public void onPause()
{
super.onPause();
save(notes.itemSelected);
}
#Override
public void onResume()
{
super.onResume();
ArrayList<String> checkOld = load();
for (int i = 0 ; i < checkOld.size(); i++)
{
notes.itemSelected = checkOld;
}
}
#Override
public void onRestart()
{
super.onRestart();
ArrayList<String> checkOld = load();
for (int i = 0 ; i < checkOld.size(); i++)
{
notes.itemSelected = checkOld;
}
}
private void save(final ArrayList<String> isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
for(Integer i = 0; i < isChecked.size(); i++)
{
editor.putString(i.toString(), isChecked.get(i));
}
editor.commit();
}
private ArrayList<String> load() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
ArrayList<String> reChecked = new ArrayList<String>();
for(Integer i = 0; i < notes.getCount(); i++)
{
reChecked.add(i, sharedPreferences.getString(i.toString(), " "));
}
return reChecked;
}
}
Please Please HELP!!!!
See how-do-i-save-an-android-applications-state