I have a main activity with a ListView. I then have a second activity with a text box for the user input and a button. When the user enters the input and clicks on the button, it should add the item to the ListView in the main activity but for some reason the ListView doesn't refresh with the new data. Any idea on how to go about this ?
Asked
Active
Viewed 80 times
-5
-
2Did you try search? Check this http://stackoverflow.com/questions/2250770/how-to-refresh-android-listview?rq=1 – Jan 17 '16 at 07:41
-
I have a complete example which shall answer this question, http://stackoverflow.com/questions/34637554/sending-data-from-a-second-activity-to-the-first-one-doesnt-work-android/34640796#34640796 – Pankaj Nimgade Jan 17 '16 at 08:24
-
you can check the code on github as well https://github.com/pankajnimgade/Tutorial/blob/master/app/src/main/java/activities/list/first/NoteListActivity.java – Pankaj Nimgade Jan 17 '16 at 08:26
-
Thanks Pankaj - will try your sample out – user3596206 Jan 17 '16 at 08:29
-
@user3596206, you are welcome :) – Pankaj Nimgade Jan 17 '16 at 09:00
2 Answers
0
Create an adapter class for your listView.
class MyAdapter extends BaseAdapter {
ArrayList<Item> items;
// Override all other needed methods.
//Add method to add items to list from outside.
public void addItems(Item item) {
items.add(item);
adapter.notifyDataSetChanged();
}
}
Inside your activity // Crate MyAdapter instance an assign to listview like-
listView,setAdapter(adapter);
Item item= new Item();
//Add necessary properties in item. and add item to list.
adapter.addItem(item);
This is the overall idea . Look around this. Should work.

Krishna Satwaji Khandagale
- 2,235
- 16
- 25
-
public void addOccasion(View view){ Intent intent = new Intent(getApplicationContext(), New.class); startActivityForResult(intent, 33); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==33 && resultCode==RESULT_OK){ adapter.notifyDataSetChanged(); } } and then in the second activity: Intent i = getIntent(); i.putExtra("data", "something"); setResult(RESULT_OK,i); finish(); – user3596206 Jan 17 '16 at 08:23
-
You can do that but changing listview content is important i.e you must first update listadapter data n then call notify dataset changed. otherwise will not work. – Krishna Satwaji Khandagale Jan 17 '16 at 08:25
-
Suppose you are passing an arralist to adapter while creating adapter. then you must modify that arraylist before calling adapter.notifyDatasetChanged() . That's what i have written in answer . – Krishna Satwaji Khandagale Jan 17 '16 at 08:30
0
Your second activity B, can't see your data resource in activity a, therefore, when you add new item in activity B you must notify activity A
that a change has occurred. This you can obtain using broadcastreceive. When you add a item in activity B sendBroadcastreceive to activity A. Activity A receive the change add it to dataresource and only after notify your adapter.
See the example below. it work!
MainActivity.java
public class MainActivity extends AppCompatActivity {
static String[] bike = new String[]{
"Ducati", "Honda", "BMW", "Bimota", "Kawasaky", "Yahamaha", "Suzuky", "Harlay-Davinson",
"Fantic", "Morini", "Hasquarna", "Aprilia", "Guzzi", "Beta", "Borile", "Buell", "Kymco", "Ktm"
};
ArrayList<String> alBike = new ArrayList<>(Arrays.asList(bike));
private ArrayAdapter adapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView);
Button button = (Button) findViewById(R.id.button2);
adapter = new myadapter(getBaseContext(),0);
listView.setAdapter(adapter);
IntentFilter intentFilter =new IntentFilter();
intentFilter.addAction("ITEM_ADDED");
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// Log.i("bogo",action);
switch (action) {
case "ITEM_ADDED":
String newitem = intent.getStringExtra("newitem");
alBike.add(newitem);
adapter.notifyDataSetChanged();
break;
}
}
};
registerReceiver(broadcastReceiver, intentFilter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),FormActivity.class));
}
});
}
public class myadapter extends ArrayAdapter<String> {
public myadapter(Context context, int resource) {
super(context, resource);
}
@Override
public int getCount() {
return alBike.size();
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.listviewrow, parent, false);
TextView textView = (TextView) row.findViewById(R.id.textView);
textView.setText(alBike.get(position));
return row;
}
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bogo.com.additemtolistview.MainActivity">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new item"
android:id="@+id/button2"
android:layout_above="@+id/listView"
android:layout_alignParentEnd="true"
android:layout_marginEnd="198dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true" />
</LinearLayout>
FormActivity.java (YOUR ACTIVITY B)
package bogo.com.additemtolistview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class FormActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.editText);
String newitem = editText.getText().toString();
Intent intent = new Intent("ITEM_ADDED");
intent.putExtra("newitem",newitem);
sendBroadcast(intent);
finish();
}
});
}
}
activity_form.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="bogo.com.additemtolistview.FormActivity">
<EditText
android:layout_width="220dp"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="78dp"
android:layout_marginTop="60dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/editText" />
</RelativeLayout>
listviewrow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:textColor="@android:color/black"
android:id="@+id/textView" />
</LinearLayout>
</LinearLayout>

Bogojob
- 135
- 1
- 10
-
Thanks for that Bogojob. Just one question, what actually us listviewrow.xml used for? – user3596206 Jan 18 '16 at 07:43
-
i have used listviewrow.xml. it contains LinearLayout and a simple TextView. Anyway, any listviewrow you want use the concept and the same. You see listviewrow.xml – Bogojob Jan 18 '16 at 10:28