tech_enthusiast
tech_enthusiast

Reputation: 693

Android List does not refresh after save button clicked

I have an application in which I am trying to refresh the list after I click save button.

The logic is something like this: 1. retrieve the fruit list 2. on the same screen , i can edit/add a new fruit name 3. if I choose to add a fruit name, it brings me some form tags, once entered , I click on save button. 4. On click on save , it should bring me the activity which retrieves the fruit list(step 1).

public class firstActivity extends Activity{
private SQLiteDatabase db;
Button saveButton;
public void onCreate(Bundle savedInstance){
// some code here
saveButton = (Button) findViewById(R.id.savedata);
db = openOrCreateDatabase("location.db",
            SQLiteDatabase.CREATE_IF_NECESSARY, null);


        saveButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String fruitName;
                fruitName = fruitName.getText().toString();
    ContentValues fruitContent = new ContentValues();
    fruitContent.put("fruit_name",fruitName);


    if (!(db.isOpen())) {
    db = openOrCreateDatabase("location.db",SQLiteDatabase.CREATE_IF_NECESSARY, null);
    }
    fruitId = (int) db.update(FRUIT_TABLE, fruitContent,"fruit_id=" + f_P_Id, null);
    db.close();
    } else {
    if (!(db.isOpen())) {
    db = openOrCreateDatabase("location.db",SQLiteDatabase.CREATE_IF_NECESSARY, null);
        }
    fruitId = (int) db.insertOrThrow(FRUIT_TABLE, null,fruitContent);

    db.close();
    }
    Log.i(TAG, "New Fruit Id:" + fruitId);

    Intent intent = new Intent(firstActivity.this,secondActivity.class);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        });
}
}

public class secondActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        Log.i(TAG, "cmg");
        setContentView(R.layout.fruitlist);

        /**
         * database code
         */
        SQLiteDatabase db;
        db = openOrCreateDatabase("location.db",
                SQLiteDatabase.CREATE_IF_NECESSARY, null);
        if (db != null)
            Log.i(TAG, "database created");
        else
            Log.i(TAG, "DAtabase null");

        //db.execSQL(CREATE_TABLE_1);


        Cursor cursor = db.query(tablename, null, null, null, null, null, null);

        int listSize = cursor.getCount();
        final String[] fruit_List = new String[listSize + 1];

        cursor.moveToFirst();
        int arrIter = 0;
        while (cursor.isAfterLast() == false) {
            Log.i(TAG, "Coumn Value:" + cursor.getString(0));
            fruitList[arrIter] = cursor.getString(1);
            cursor.moveToNext();
            arrIter++;
        }
        fruit_List[arrIter] = "New Fruit";
        Log.i(TAG, "FruitList:");
        for (int i = 0; i < fruit_List.length; i++) {
            Log.i(TAG, "Fruit:" + fruit_List[i]);
        }
        Log.i(TAG, "Count:" + cursor.getCount());
        cursor.close();
        db.close();

        ListView listView = (ListView) findViewById(R.id.list);

        if (listView != null) {
            listView.setAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, fruit_List));
        } else {
            Log.i(TAG, "Null");
        }


        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // When clicked, show a toast with the TextView text

                Intent intent = new Intent(view.getContext(),
                        firstActivity.class);
                startActivity(intent);

            }
        });

    }

}

Somehow it does not take me to secondActivity and get the new list. Can somebody help me out?

Thanks.

Upvotes: 0

Views: 989

Answers (6)

Yahya Arshad
Yahya Arshad

Reputation: 1626

Calling ListView.notifyDataseInvalidate() will refresh the ListView.

Upvotes: 1

Vaibhav Vajani
Vaibhav Vajani

Reputation: 396

@JModi: do something like below...

1) when button is clicked: Insert proper entry in database.

2) Make another method say "refreshList()" in which you do the same thing which is you use for making your list.

and call this method after you make entry in database.

--> Use your database and make the list again. --> Don't forget to use listViewObject.clear() method in start of "refreshList()" method.

I have done it and this method working fine...!! Hope it will help you...!! Best of Luck..!!

Upvotes: 0

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

For your problem you must use custom adapter.Better and best solution..

LINK for your reference

Upvotes: 0

Hesham Saeed
Hesham Saeed

Reputation: 5378

The problem is, you need to know where are you saving the list, and before you enter the second activity, you should have updated this list and access it in the other activity, whether with database / or putting Extra on this intent and then send the whole list to the second activity.

Note: if you are using adapter, use adapter.notifyDatasetChanged() function to update the list in the adapter.

Upvotes: 0

Ika
Ika

Reputation: 1748

Do you have any exception when you press the save button ? If not it probably means the click listener is not set properly so make sure you enter the onClick method whenever the save button is pressed.

You can also use the android:onClick attribute to avoid setting click listeners programatically.

In the Button section of your XML layout:

android:onClick="onSaveButtonClicked"

In your activity:

public void onSaveButtonClicked(View view){
 ...
}

Upvotes: 0

Bhavin
Bhavin

Reputation: 6010

When you want to Refresh your ListView Just Remember you will have to insert this Statement.

listviewobject.setAdapter(adapter);

This will automatically Refresh the List.

Upvotes: 0

Related Questions