ListView gets empty when back icon is clicked - android

I have two activities, in the main one I have a text field and two buttons, the user can enter text in the text field.
When clicking the add button,text will be added to some ArrayList.
When clicking show in list button a new activity with its own fragment should open showing a listview that contains the names that the user has entered.
I used ArrayAdapter and this is working fine. When I am in the list activity and when I click the back button of the device everything goes fine and the data in the ArrayList is not lost.
If I click show in list again I will find the old data I entered at the first time. But, if I click the back icon provided by Android at the top left of the device screen, the ArrayListbecomes empty and when I click show in list the listview shows as empty.
Here is the main activity code,
public class MainActivity extends ActionBarActivity {
String LOG_TAG = this.getClass().getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> itemList= new ArrayList<String>();
final EditText text = (EditText) findViewById(R.id.text_field);
text.setHint("Enter name here");
Button addButton = (Button) findViewById(R.id.add_button);
Button showButton = (Button) findViewById(R.id.show_button);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name= text.getText().toString();
itemList.add(name);
if (itemList.size()==1){
Toast.makeText(getApplicationContext(), "one", Toast.LENGTH_SHORT).show();
}
Toast.makeText(getApplicationContext(), "name has been added to the list", Toast.LENGTH_SHORT).show();
}
});
showButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent (getApplicationContext(), ListActivity.class);
intent.putExtra("list", itemList);
startActivity(intent);
}
});
}//end oncreate
This is the ListActivity and its fragment code,
public class ListActivity extends ActionBarActivity {
static ArrayList <String> itemList;
String LOG_TAG = this.getClass().getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
itemList= getIntent().getStringArrayListExtra("list");
setContentView(R.layout.list_activity);
if (savedInstanceState==null){
getSupportFragmentManager().beginTransaction().add(R.id.container, new list()).commit();
}
}
public static class list extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,false);
ListView listView = (ListView) rootView.findViewById(R.id.list);
ArrayAdapter <String> adapter= new ArrayAdapter<String>(getActivity(),R.layout.fragment_main, R.id.row, itemList);
listView.setAdapter(adapter);
return rootView;
}
}
}
Can anyone please tell me why this is happening? How can I solve this issue?
Any help is appreciated.
Many thanks.

create a class Constantss.java and declare
public static ArrayList <String> itemList=null;
then inside your class
use
Constantss. itemList.add(name);
instead of
itemList.add(name);
// replace itemList with Constantss. itemList in all of your classes

I believe this is a navigation issue. So you should close your ListActivity like this:
public class ListActivity extends ActionBarActivity {
//your code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemID = item.getItemId();
switch (itemID ) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Anyway your data can be lost if android destroys your MainActivity. So if you navigate from ListActivity back to MainActivity, MainActivity will be recreated and you data get lost. So you should find the way how to save your
data permanent.
there are two common ways:
Save data to data base (SQLite)
Save data to Preferences How to store list of values in SharedPreferences

Related

get user inputs from an editText and populate listView

How can i get user inputs from one activity and populate the listView with user data in another activity. I am able to get user input and populate the listView in the same activity. but now i want to get user inputs in one form and populate the list in another activity.
the code that i used to populate the listView by getting user input is as follows
public class MainActivity extends ListActivity {
ArrayList<String> list = new ArrayList<String>();
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btnAdd);
/** Defining the ArrayAdapter to set items to ListView */
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
/** Defining a click event listener for the button "Add" */
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText edit = (EditText) findViewById(R.id.txtItem);
String name=edit.getText().toString();
list.add(name);
edit.setText("");
adapter.notifyDataSetChanged();
}
};
/** Setting the event listener for the add button */
btn.setOnClickListener(listener);
you can store your user input / data into a local database; that will allow you to access your data anywhere in the app
(recommended since you are dealing with listview).
you can use shared preferences to store data if your data is relatively small.
In your current Activity (activity contains your button), create a new Intent:
String name = "";
name = edit.getText().toString();
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("keyword",name);
startActivity(i);
Then in the NewActivity (activity contains your Listview), retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String name = extras.getString("keyword");
if(name != ""){
// adapter.notifyDataSetChanged();
}
}
It is simple way, hope this help
Declare a public method in second Activity like
SecondActivity.class
public static ArrayList<String> list = new ArrayList<String>();
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter
onCreate()
{
...
;
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
...
}
public static void ModifyList()
{
adapter.notifyDataSetChanged();
}
FirstActivity.class
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText edit = (EditText) findViewById(R.id.txtItem);
String name=edit.getText().toString();
SecondActivity.list.add(name);
edit.setText("");
SecondActivity.ModifyList();
}
};
Send your ArrayList like this from FirstActivity :
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putStringArrayListExtra("Datalist",list);
startActivity(intent);
In secondActivity Recieve the list using :
Intent i = getIntent();
list = i.getStringArrayListExtra("Datalist");
Then display it in your SecondActivitys listview

Add items to listview from other activity

scenario:
First mainactivity launches and from the menu option user launches second activity using intent and there he adds some text to edittext and get that edittext value using intent to the first activity and add that value to the listview.
FirstActivity:
public class MainActivity extends Activity {
ListView lv;
EditText et;
String AddedTask ;
ArrayList<Model> modelList;
CustomAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
if (intent.hasExtra("NewTask")) {
AddedTask = this.getIntent().getExtras().getString("NewTask");
lv = (ListView) findViewById(R.id.listViewData);
String name = AddedTask;
Model md = new Model(name);
modelList.add(md);
adapter = new CustomAdapter(getApplicationContext(), modelList);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.action_add_task:
Intent i = new Intent(MainActivity.this, AddTask.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Second Activity:
public class AddTask extends Activity {
Button addtask;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_task);
// get action bar
ActionBar actionBar = getActionBar();
// Enabling Up / Back navigation
actionBar.setDisplayHomeAsUpEnabled(true);
addtask = (Button) findViewById(R.id.btnaddlist);
findViewById(R.id.btnaddlist).setOnClickListener(
new View.OnClickListener() {
public void onClick(View arg0) {
EditText edit = (EditText) findViewById(R.id.tskname);
Intent i = new Intent(AddTask.this,
MainActivity.class);
//Bundle bundle = new Bundle();
String TaskName = edit.getText().toString();
//bundle.putString("NewTask", TaskName);
i.putExtra("NewTask", TaskName);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//i.putExtras(bundle);
startActivity(i);
}
});
}
}
Now my problem is I'm able to add the data to the listview but each time I come back to mainactivity the previous data which was added is lost and updating the old data with my new data.
I have searched for many SO answers and most of them suggest to add adapter.notifyDataSetChanged(); which I have already tried and nothing worked.
I have done by checking the adapter is null or updating the data this way and getting null pointer exception:
if ( adapter== null )
{
adapter = new CustomAdapter(getApplicationContext(), modelList);
lv.setAdapter(adapter);
}
Can anyone say me how do I get this working ?
new View.OnClickListener() {
public void onClick(View arg0) {
EditText edit = (EditText) findViewById(R.id.tskname);
Intent i = new Intent(AddTask.this,
MainActivity.class);
//Bundle bundle = new Bundle();
String TaskName = edit.getText().toString();
//bundle.putString("NewTask", TaskName);
i.putExtra("NewTask", TaskName);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//i.putExtras(bundle);
startActivity(i);
}
});
You are starting a new Activity each time you want to add an item.
Consider using
startActivityForResult()
Link to Android Documentation
Even if startActivityForResult() is IMHO the best way to fix your problem, i'll give you a hint why it doesn't show your "old" data.
You don't finish your first Activity when going to the Second. You could aswell just finish your second activity and your MainActivity would run into
onResume()
therefor check the Android lifecycle
Don't use putextra and intent techniques. Follow below technique.
In main activity create sharedpref as follows:
public static final String SharedPref = "MyPreferences";
In activity 2 insert this code.
SharedPreferences settings=null;//declaration
settings=getSharedPreferences(SharedPref,0);//initialization
String TaskName= edit.getText().toString();
SharedPreferences.Editor editor = settings.edit();
editor.putString("NewTask", TaskName);
editor.commit();
finishfromchild(AddTask.this);
In main activity:
SharedPreferences settings=null;//declaration
In onCreate
//now initialise settings
settings=getSharedPreferences(SharedPref,0);//SharedPref is the foldername
//of your sharedpreferences(you create it first)
//Now create onResume method
public void onResume()
{
super.onResume();
AddedTask=settings.getString("NewTask", "");
lv = (ListView) findViewById(R.id.listViewData);
String name = AddedTask;
Model md = new Model(name);
modelList.add(md);
adapter = new CustomAdapter(getApplicationContext(), modelList);
lv.setAdapter(adapter);
}
Hope this works. I did the same in my previous project.

Can't refresh the fragment after combing back from another activity

I'm working on the fragment, when I click a button, it will turn to another activity, after the activity saved data to database, it will return back to the fragment, however, the fragment can't show the newest data, I need to go to the activity of the fragment, then come back to the fragment again, it will show newest data.
Here is my code of fragment. I thought that because I put the setAdapter in the onActivityCreated() method, I tried to put it in onCreate() and onCreateView(), both will return nullpointer error.
public class TodoFragment extends ListFragment {
private TodoDataSource datasource;
private Todo item;
List<Todo> todoList;
MyCustomAdapter adapt;
ListView listTask;
EditText t;
public TodoFragment(){
}
/*#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//setHasOptionsMenu(true);
datasource = new TodoDataSource(getActivity());
datasource.open();
todoList = datasource.getAllTodos();
adapt= new MyCustomAdapter(getActivity(), R.layout.todo_list, todoList);
//ListView listTask =(ListView) rootView.findViewById(android.R.id.list);
listTask.setAdapter(adapt);
}*/
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// if (activity != null) {
// Create an instance of the custom adapter for the GridView. A static array of location data
// is stored in the Application sub-class for this app. This data would normally come
// from a database or a web service.
datasource = new TodoDataSource(getActivity());
datasource.open();
todoList = datasource.getAllTodos();
adapt= new MyCustomAdapter(getActivity(), R.layout.todo_list, todoList);
//ListView listTask =(ListView) rootView.findViewById(android.R.id.list);
listTask.setAdapter(adapt);
Button button = (Button) getActivity().findViewById(R.id.simpleadd);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//String summary = t.getText().toString();
Intent i = new Intent(getActivity(), TodoDetailActivity.class);
//Bundle bundle = new Bundle();
// bundle.putString("Summary", summary);
//i.putExtras(bundle);
startActivity(i);
}
});
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_todo, container, false);
t = (EditText) rootView.findViewById(R.id.simple);
listTask = (ListView) rootView.findViewById(android.R.id.list);
return rootView;
}
#Override
public void onResume() {
datasource.open();
super.onResume();
adapt.notifyDataSetChanged();
}
#Override
public void onPause() {
datasource.close();
super.onPause();
}
}
Here is part of my another activity code.
it will update the database and return to the fragment.
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (TextUtils.isEmpty(mTitleText.getText().toString())) {
makeToast();
} else {
final String category = mCategory.getSelectedItem().toString();
final String summary = mTitleText.getText().toString();
final String description = mBodyText.getText().toString();
datasource.createTodo(category, summary, description);
setResult(RESULT_OK);
finish();
}
}
});
It looks like you aren't updating your data source todoList before calling notifyDataSetChanged() in onResume().
Edit: Clear the list first, and then re-populate. Also add logging to help track down the issue:
#Override
public void onResume() {
super.onResume();
datasource.open();
todoList.clear();
todoList.addAll(datasource.getAllTodos());
adapt.notifyDataSetChanged();
Log.d("MyApp", "Size of data: " + todoList.size() );
}
Once you have added that code, try adding a few records, and watch your logcat to see if the size goes up. You can also cycle through each record, if you post your Todo class I can give you some example code.

Android Passing values from one activity to other activity (string) or from one class to another

I have two class Profile.class and Details.class,
In profile class i have used a spinner with values like (ATM,Banking,Personal,Others etc)
and a button (OK).
on clicking ok button it will go to next activity that is details activity where i will be taking some details like-name,description etc.
after filling the details i have given a button (save).
on clicking button save i will be saving the name and description in database but i want to save the profile name also along with details. i am unable to transfer selected spinner text from Profile.class to Details.class
how to transfer?
create.class code
public class Create extends Activity {
public ArrayList<String> array_spinner;
Button button4;
String spinnertext;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
array_spinner=new ArrayList<String>();
array_spinner.add("ATM");
array_spinner.add("Bank");
array_spinner.add("Mail");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, array_spinner);
adapter.setNotifyOnChange(true);
spinner.setAdapter(adapter);
spinner.setLongClickable(true);
spinner.setOnLongClickListener(new OnLongClickListener(){
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}}
);
button4 = (Button)findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent4 = new Intent(view.getContext(), Details.class);
startActivityForResult(myIntent4, 0);
myIntent4 .putExtra("key", array_spinner.getSelectedItem().toString());
startActivity(myIntent4);
}
});
}}
details.class code
public class Details extends Activity {
EditText editText4,editText5,editText6;
Button button8,button9,button10;
TextView textView7;
String et4,et5,et6;
//SQLite Database db;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
String spinnervalue = getIntent().getExtras().getString("Key");
please kindly explain me what is this "key"?
You can use :
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("YourValueKey", yourData.getText().toString());
then you can get it from your second activity by :
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");
example
this is what you have to write in your first activity
Intent i = new Intent(getApplicationContext(), Product.class);
i.putExtra("productname", ori);
i.putExtra("productcost", position);
i.startActivityForResult(i,0);
then in your next activity you need to have this code
String productname,productcost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product);
tv1= (TextView)findViewById(R.id.tv1);
tv2= (TextView)findViewById(R.id.tv2);
Bundle extras= getIntent().getExtras();
if(extras!=null)
{
position = extras.getString("position"); // get the value based on the key
tv1.setText(productname);//use where ever you want
productname = extras.getString("productname"); // get the value based on the key
tv2.setText(productname);
}
First of all take a spinner and provide value to them what you want and then the selected spinner value change it to string value and this string variable will be used in OK button to pass value through use of Intent or Shared preference to take this value to another activity and through there you can use it in database to display this value.
If you want to send data to another activity, you can do it using intent.
Bundle bund = new Bundle();
bund.putString("myKey",name);
Intent intent = new Intent(Profile.this, Detail.class);
intent.putExtras(bund);
startActivity(intent);
Now in Detail class, receive this data in onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
.......
String nameReceived = getIntent().getExtras().getString("myKey");
}
I have given the example of passing String to another activity however, you can pass boolean, int, double etc to another activity. See the full list on here

How to add a item in List View at run time

I am new at Android and have a list to add and remove items at run time.
I am using the following code to add a new item:
public class MainActivity extends Activity {
private ListView list;
private Button btAdd;
private ArrayAdapter<String[]> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)this.findViewById(R.id.ListView1);
btAdd=(Button)this.findViewById(R.id.button1);
String [] name={"new Item"};
adapter=new ArrayAdapter<String[]>(this,R.id.ListView1);
adapter.add(name);
btAdd.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
list.setAdapter(adapter);
}
});
}
What is the problem with this?
1) set your adapter in onCreate: list.setAdapter(adapter);
2) Add//Remove your item in your OnClickListener and then call adapter.notifyDatasetChanged()

Categories

Resources