0

In MainActivity i have one ListView which is display like:

[CheckBox] [TextView] [Button]

On the top-right side there's one add button.When i click on that button it add one item in the listview.

MyItem item = new MyItem(itemName);
myItemsList.add(item);
adapter.notifyDataSetChanged();

"MyItem" is pojo Class and "itemName" is String value. "myItemList" is ArrayList.

So this item is added in ListView perfectly but when i load activity again or restart app then value should not be display.

How can i solve this problem?

Thanks in Advance.

EDIT

Here is ArrayAdapter getView method

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    MyItem cell = (MyItem) getItem(position);
    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    SharedPreferences sharedPrefs = context.getSharedPreferences(
            "sharedPrefs", Context.MODE_PRIVATE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.listitem, null);
        holder = new ViewHolder();
        holder.txtItemName = (TextView) convertView
                .findViewById(R.id.tvItemName);
        holder.chkItem = (CheckBox) convertView.findViewById(R.id.chkItem);
        holder.chkItem
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        int getPosition = (Integer) buttonView.getTag();
                        // Here we get the position that we have set for the
                        // checkbox using setTag.
                        itemList.get(getPosition).setSelected(
                                buttonView.isChecked());
                        editor.putBoolean("CheckValue" + getPosition,
                                buttonView.isChecked());
                        editor.commit();

                        // Set the value of checkbox to maintain its state.
                    }
                });
        convertView.setTag(holder);
        convertView.setTag(R.id.tvItemName, holder.txtItemName);
        convertView.setTag(R.id.chkItem, holder.chkItem);

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

    editor = sharedPrefs.edit();
    holder.chkItem.setTag(position); // This line is important.

    holder.txtItemName.setText(itemList.get(position).getName());
    holder.chkItem.setChecked(sharedPrefs.getBoolean("CheckValue"
            + position, false));

    return convertView;

}

EDIT

public String[] items = new String[]{"A","B","C"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    lvItemName = (ListView) findViewById(R.id.lvItemName);
    imgAdd = (ImageView) findViewById(R.id.imgAdd);
    myItemsList = new ArrayList<MyItem>();

    for (int i = 0; i < items.length; i++) {
        MyItem item = new MyItem(items[i]);
        myItemsList.add(item);
    }

    adapter = new ItemAdapter(this, R.layout.listitem, myItemsList);
    lvItemName.setAdapter(adapter);
    lvItemName.setOnItemClickListener(this);

    // ImageView Add Click
    imgAdd.setOnClickListener(this);

}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    MyItem planet = adapter.getItem(position);
    planet.toggleChecked();
    ViewHolder viewHolder = (ViewHolder) view.getTag();
    viewHolder.getCheckBox().setChecked(planet.isSelected());

    Log.i("Checked::", "" + viewHolder.getCheckBox().isChecked());

}
Dhruv
  • 1,862
  • 3
  • 20
  • 38
  • Can you post your code here at where you display data in listview.. – Piyush Sep 10 '13 at 07:24
  • You dont want to show the dynamically added item – Vaibs_Cool Sep 10 '13 at 07:25
  • the problem is with your shared preferences. – Piyush Sep 10 '13 at 07:28
  • Nope.I want to show it when it's added and when i relaunch app. – Dhruv Sep 10 '13 at 07:28
  • What's Problem @PiyushGupta in SharedPreferences? Please tell me. "MyItem item = new MyItem(itemName); myItemsList.add(item); adapter.notifyDataSetChanged();" This snippiest code will be written on add button in activity. And ArrayAdapter is Custom Class that i created. – Dhruv Sep 10 '13 at 07:32
  • 1
    I think you are very unclear about what your goal is. After adding new item to your listview, and restarting application, do you want the new item still be present in listview? – hendrix Sep 10 '13 at 07:48
  • Yes that's i want. After i restarting my app the newly added item still be present in ListView. – Dhruv Sep 10 '13 at 08:59

2 Answers2

0

Do you mean to say that the newly added item (in your listview) should not be displayed when you activity loads next time?

Assuming the above:- When you load activity again (onCreate() is getting called), ideally only things you load in your ArrayList should come in the listview. So make sure that you arraylist is also refreshed (or reloaded) every time your activity is reloaded & you are not storing any added items permanently.

Amit B
  • 131
  • 1
  • 6
  • Yes @Amit. You are right. But, is it any way to solve this issue? – Dhruv Sep 10 '13 at 07:35
  • @Lawrance Can you pls share some code used for itemList? Is there any place in your code, where you are storing this itemList data ? where are you loading it ? – Amit B Sep 10 '13 at 08:00
  • @Lawrance It is clear that in onCreate(...) you are setting up your array list. So actually whenever the app will be reloaded, it should not show old data (it should only have "A", "B", "C" & not some "D" that one might have added newly). Are you sure that your onCreate(...) is getting called when you said that activity is reloaded/app is restarted?. The only thing I can think of now, is to display the whole array in logcat, after setting it up & see whether it correctly shows "A","B","C" only. – Amit B Sep 10 '13 at 09:10
0

If i understand your problem correctly, you are referring to more general problem - saving state.

There are several ways to save your activity state:

  • If you dont need your list state to be persistent, you technique described here
  • If you need your list state to be persistent (next time you start the app your list will have the same items), you can override onPause and onResume methods, and save your data in some kind of persistent storage. See code below:

    @Override protected void onPause(){ super.onPause(); // save your ArrayList to persistent storage }

    @Override protected void onResume(){ super.onResume(); // load your ArrayList }

In order to save Collection to persistent data, you should either make your collection implement Parcelable and use SharedPreferences, or use SQLite table... Refer to android link about persistent storage above...

Community
  • 1
  • 1
hendrix
  • 3,364
  • 8
  • 31
  • 46