Listview does not populate first time - android

I'm creating a ListView by populating an ArrayList in open activity and opening it as a ListView in another activity.
When I open the second activity My ListView only populates when I open the activity, the Get_Issue activity opens blank. When I go back and re-enter the activity (Calling the Get_Issue.class for the second time). Then the ListViewis populated.
I guess the screen is loading before the listView is created/populated?
How can I improve my code so that the listView is populate the first time I call the Get_Issue activity. I do not want to open an empty activity.
class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
DatabaseReference databaseIssue;
public static List<Issue> issueList = new ArrayList<>();
public static List<Issue> getIssueList() {
return issueList;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
getData = (Button) findViewById(R.id.getData);
databaseIssue = FirebaseDatabase.getInstance().getReference("Issue");
}
private void getTheData(){
databaseIssue.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
issueList.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Issue getData = postSnapshot.getValue(Issue.class);
issueList.add(getData);}}
});
}
public void onClick(View view) {
if(view == getData){
getTheData();
startActivity(new Intent(this, Get_Issue.class));
}
}
}
The adapter class
public class Get_Issue extends AppCompatActivity {
public void populateListView() {
List<Issue> issues = ProfileActivity.getIssueList();
ArrayAdapter<Issue> adapter = new ArrayAdapter<>(
this,
R.layout.da_item,
issues);
ListView list = findViewById(R.id.listView);
list.setAdapter(adapter);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.issue_list);
populateListView();
}
}

Related

Unable to update item in Firebase Database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am new to android i have made a project to store members information in one activity and in another activity i display members in listView and on onlong clicking listview i open another activity to update members information but when i click on update button it adds that record instead of Updating.
My Firebase Structure :
My activity for storing :
public class MainActivity extends AppCompatActivity {
EditText txtname,txtage,txtheight,txtphone;
Button btnsave,btnRead;
Member member;
DatabaseReference reff;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtname = findViewById(R.id.txtname);
txtage =findViewById(R.id.txtage);
txtphone =findViewById(R.id.txtphone);
txtheight =findViewById(R.id.txtheight);
btnsave = (Button)findViewById(R.id.btnsave);
btnRead =(Button)findViewById(R.id.btnRead);
btnRead.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity();
}
private void openActivity() {
Intent intent = new Intent(MainActivity.this, Retreivedata.class);
startActivity(intent);
}
});
member = new Member();
reff = FirebaseDatabase.getInstance().getReference().child("Member");
btnsave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AddUsers();
}
});
}
private void AddUsers(){
String name = txtname.getText().toString().trim();
String age = txtage.getText().toString().trim();
String phone = txtphone.getText().toString().trim();
String ht = txtheight.getText().toString().trim();
if(!TextUtils.isEmpty(name)) {
String id = reff.push().getKey();
Member member = new Member(id, name, age, phone, ht);
reff.child(id).setValue(member);
Toast.makeText(this,"User Inserted Successfully",Toast.LENGTH_LONG).show();
txtheight.setText("");
txtphone.setText("");
txtage.setText("");
txtname.setText("");
}else {
txtname.setError("Enter Name");
}
}
}
My activity for displaying records in listview:
public class Retreivedata extends AppCompatActivity {
ListView listView;
FirebaseDatabase database;
DatabaseReference ref;
ArrayList<Member> list;
ArrayAdapter<Member> adapter;
Member member;
Button btnDlt;
Boolean a=false;
String val="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retreivedata);
member = new Member();
listView =(ListView)findViewById(R.id.listView);
btnDlt = (Button) findViewById(R.id.btnDlt);
database = FirebaseDatabase.getInstance();
ref = database.getReference("Member");
list = new ArrayList<>();
adapter = new ArrayAdapter<Member>(this,R.layout.list_white_text,R.id.userInfo, list);
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dts: dataSnapshot.getChildren())
{
member = dts.getValue(Member.class);
list.add(member);
}
listView.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
btnDlt.setEnabled(false);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Boolean a=true;
Member member=adapter.getItem(position);
Toast.makeText(Retreivedata.this,"Do u want to delete this record!!",Toast.LENGTH_LONG).show();
if (a==true)
{
btnDlt.setEnabled(true);
}else{
btnDlt.setEnabled(false);
}
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Member member= adapter.getItem(position);
Intent intent = new Intent(Retreivedata.this, Update.class);
intent.putExtra("tem", member);
startActivity(intent);
return false;
}
});
btnDlt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ref.child(member.getMemberId()).removeValue();
Toast.makeText(Retreivedata.this, "Record deleted Successfully",
Toast.LENGTH_LONG).show();
adapter.remove(member);
adapter.clear();
}
});
}
}
My activity for updating record:
public class Update extends AppCompatActivity {
EditText EditTxtName,EditTxtAge,txtPhone,txtHeight;
Member member;
FirebaseDatabase db;
DatabaseReference reff;
Button btnUpdate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
EditTxtName = (EditText)findViewById(R.id.EditTxtName);
EditTxtAge = (EditText)findViewById(R.id.EditTxtAge);
txtPhone = (EditText)findViewById(R.id.txtPhone);
txtHeight = (EditText)findViewById(R.id.txtHeight);
btnUpdate = (Button)findViewById(R.id.btnUpdate);
final Member member= (Member) getIntent().getSerializableExtra("tem");
EditTxtName.setText(member.getName());
EditTxtAge.setText(member.getAge());
txtPhone.setText(member.getPhone());
txtHeight.setText(member.getHeight());
reff = db.getInstance().getReference().child("Member");
btnUpdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Update();
}
});
}
private void Update(){
final String mnam = EditTxtName.getText().toString().trim();
final String mage = EditTxtAge.getText().toString().trim();
final String mph = txtPhone.getText().toString().trim();
final String mhei = txtHeight.getText().toString().trim();
final String ID = reff.getKey();
if(TextUtils.isEmpty(mnam)) {
EditTxtName.setText("Plz enter name");}
if(TextUtils.isEmpty(mage)) {
EditTxtName.setText("Plz enter age");}
else{
final Member member = new Member(ID,mnam,mage,mph,mhei);
reff.child("Member").child(ID).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
reff = FirebaseDatabase.getInstance().getReference();
reff.child("Member").child(ID).child("name").setValue(mnam);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Toast.makeText(Update.this,"Updated",Toast.LENGTH_LONG).show();
txtHeight.setText("");
txtPhone.setText("");
EditTxtName.setText("");
}
}
}
But after updating record it creates another record.Suggest me what changes has to be done on update button click.
Thnaks!!
First I assume that you implement Serializable for your Member class:
class Member implements Serializable{
.............
.............
.............
}
Since you are passing the member object to the update activity, why not use the ID in it to update the data:
//the update activity
public class Update extends AppCompatActivity {
//this is the member that you pass
Member member;
......
......
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.........
.........
//this is how you retrieve it
member= (Member) getIntent().getSerializableExtra("tem");
//this is your ref
reff = db.getInstance().getReference().child("Member");
........
........
}
//when you update
private void Update(){
final String mnam = EditTxtName.getText().toString().trim();
final String mage = EditTxtAge.getText().toString().trim();
final String mph = txtPhone.getText().toString().trim();
final String mhei = txtHeight.getText().toString().trim();
//update the name field
reff.child(member.getMemberId()).child("name").setValue(mnam);
}
Your problem is this line of code final String ID = reff.getKey()
That call generates a new ID which you then use in your update of the db. You need to replace the call to getKey with the ID of the member you want to update.

Pass Arraylist String using intent over 2 activities

I have 3 activities, activity 1 is fetching data from firebase, activity 2 is splash screen and activity 3 is show the data. I want to pass the data from activity 1 to activity 3 using intent. but it doesnt work. If i dont use activity 2 it works, but i want to use activity 2 as well.
Activity 1 fetching data from firebase
matchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDbref = FirebaseDatabase.getInstance().getReference();
mkomref = mDbref.child("Students");
string = matchKriteria.getText().toString();
Query query = mkomp.orderByChild("Komp").equalTo(string);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
if(nameList.size()>0)
nameList.clear();
for (DataSnapshot data : dataSnapshot.getChildren()) {
nameList.add(data.child("Name").getValue(String.class));
}
Intent intent = new Intent(FirstActivity.this, LoadingActivity.class);
intent.putExtra("value",nameList);
startActivity(intent);
}
}
Activity 2 splash screen
public class LoadingActivity extends AppCompatActivity {
public static int SPLASH_TIME_OUT= 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent =new Intent(LoadingActivity.this, ResultActivity.class);
startActivity(intent);
finish();
}
},SPLASH_TIME_OUT);
Activity 3 show the result
public class ResultActivity extends AppCompatActivity {
private ArrayList<String> nameList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bedrift_match_resultat);
Intent intent = getIntent();
if (intent != null)
nameList = intent.getStringArrayListExtra("value");
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
if (nameList.size() > 0) {
CustomAdapter adapter = new CustomAdapter(this, nameList);
recyclerView.setAdapter(adapter);
}
}
}
i think it didn't work because, in your LoadingActivity you didn’t retrieve data from FirstActivity and you’re not sending it to ResultActivity. So, your namelist in ResultActivity is null
You can try this:
public class LoadingActivity extends AppCompatActivity {
public static int SPLASH_TIME_OUT= 5000;
private ArrayList<String> nameList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading);
if (getIntent() != null) {
namelist.addAll(getIntent().getStringArrayListExtra("value");
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent =new Intent(LoadingActivity.this, ResultActivity.class);
intent.putExtra("value",nameList);
startActivity(intent);
finish();
}
},SPLASH_TIME_OUT);

Android - Pass an intent from one activity to another that implements a callback class with Firebase

I have two activities: AddUser and ToDo. ToDo implements a class with callback. ToDo allows the user to create a to do list, and the to do items will be displayed instantly in a recyclerView. User can add, update, or delete to do items in ToDo.
AddUser.java
public class AddUser extends AppCompatActivity implements View.OnClickListener{
private DatabaseReference mUserRef;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_user);
mUserRef = FirebaseDatabase.getInstance().getReference().child("users");
EditText etUserid = (EditText) findViewById(R.id.etUserid);
EditText etUsername = (EditText) findViewById(R.id.etUsername);
Button btnNext = (Button) findViewById(R.id.btnNext);
btnNext.setOnClickListener(this);
}
public void addUser(UserDetails userDetails){
userPushKey = mUserRef.push().getKey();
mUserRef.child(userPushKey).setValue(userDetails);
}
#Override
public void onClick(View v){
if(v == btnNext){
String inputUserid = etUserid.getText().toString();
String inputUsername = etUsername.getText().toString();
addUser(new UserDetails(inputUserid, inputUsername));
Intent intent = new Intent(AddUser.this,ToDo.class);
intent.putExtra("userKeyRef", userPushKey);
startActivity(intent);
}
}
}
ToDo.java
public class ToDo extends AppCompatActivity implements UserTodoAdapter.Callback {
private UserTodoAdapter mAdapter;
#Override
protcted void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo);
mAdapter = new UserTodoAdapter(this);
RecyclerView view = (RecyclerView) findViewById(R.id.recycler_view);
view.setHasFixedSize(true);
view.setAdapter(mAdapter);
}
#Override
public void onEdit(final UserTodo userTodo){
// some functions here
}
}
UserTodoAdapter.java
public class UserTodoAdapter extends RecyclerView.Adapter<UserTodoAdapter.ViewHolder> {
private List<UserTodo> mUserTodo;
private Callback mCallback;
private DatabaseReference mUserTodoRef;
public UserTodoAdapter(Callback callback) {
mCallback = callback;
mUserTodo = new ArrayList<>();
// need to get the push key from AddUser activity
mUserTodoRef = FirebaseDatabase.getInstance.getReference().child(users).child("Need the push key here").child("todo");
mUserTodoRef.addChildEventListener(new TodoChildEventListener());
}
private class TodoChildEventListener implements ChildEventListener{
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s){
// action here
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s){
// action here
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot){
// action here
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s){
// action here
}
#Override
public void onCancelled(DatabaseError databaseError){
// action here
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_custom_view, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position){
final UserTodo userTodo = mUserTodo.get(position);
holder.mTodoTitle.setText(userTodo.getTodoTitle());
holder.mTodoDesc.setText(userTodo.gerTodoDesc());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCallback.onEdit(userTodo);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
removeTodo(mUserTodo.get(position));
return true;
}
});
}
#Override
public int getItemCount(){
return mUserTodo.size();
}
public interface Callback{
public void onEdit(UserTodo userTodo);
}
class ViewHolder extends RecyclerView.ViewHolder{
private TextView mTodoTitle;
private TextView mTodoDesc;
public ViewHolder(View itemView){
super(itemView);
mTodoTitle = (TextView) itemView.findViewById(R.id.tvTodoTitle);
mTodoDesc = (TextView) itemView.findViewById(R.id.tvTodoDesc);
}
}
public void addTodo(UserTodo userTodo){
mUserTodoRef.push().setValue(userTodo);
}
public void updateTodo(UserTodo userTodo, String newTodoTitle, String newTodoDesc){
userTodo.setTodoTitle(newTodoTitle);
userTodo.setTodoDesc(newTodoDesc);
mUserTodoRef.child(userTodo.getTodoKey()).setValue(userTodo);
}
public void removeTodo(UserTodo userTodo){
mUserTodoRef.child(userTodo.getTodoKey()).removeValue();
}
}
After the user clicked on Next button in AddUser activity, the user data is straightly added to Firebase, and the user will be redirected to ToDo page where the user can add to do items. How to pass the push key created in AddUser, so that when the user add the to do items, the items will be added under the user?
Is using intent the right way?
Please don't ask me why I need to let user add to do list right after the user is created. It's needed this way.
Thanks
Edit: I'm sorry I should mention that the intent should be passed to UserTodoAdapter class, so that in the Firebase database reference of UserTodoAdapter, I can point the reference to the key passed from AddUser.
I have classes UserDetails and UserTodo, for activities AddUser and ToDo respectively to handle data in Firebase.
Eventually the data will look like this:
{
"users":{
"push_id":{
"userid":"123456",
"username":"My User",
"todo_s":{
"push_id":{
"todo1":"Title1",
"todo_desc":"Description"
},
"push_id":{
"todo2":"Title2",
"todo_desc":"Description"
},
}
},
}
}
Passing via intent (from AddUser to ToDo) is fine. Or you can save it to local storage like SharedPreferences so your user doesn't have to create new user if the user has created a new user.
To pass the key value from your ToDo activity to the adapter, modify the adapter's constructor to accept a key parameter
public UserTodoAdapter(Callback callback, String key) {
mCallback = callback;
mUserTodo = new ArrayList<>();
mUserTodoRef = FirebaseDatabase.getInstance.getReference().child(users).child(key).child("todo");
}
And in the ToDo, instantiate the adapter by passing the string extra from the previous activity (AddUser).
mAdapter = new UserTodoAdapter(this, getIntent().getStringExtra("key"));

Listview does not automatically refresh after getting list data from DataStore

I have a ListView which I populate with data from DataStore or from my local database.
I am checking some condition that will determine whether I will fetch data from the DataStore or database. When I fetch from the database the ListView automatically refreshes itself, but when I fetch from the DataStore it does not. I then have to click my TextView, which is below ListView, and when I click it the soft keyboard appears and then my ListView is populated with data from DataStore.
My activity that has the ListView:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xyz);
list_View = (ListView) findViewById(R.id.data_listView);
list_View.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
list_View.setMultiChoiceModeListener(new Selector());
adapter = new MyAdapter(context,Long.valueOf(id),isOnline());
list_View.setAdapter(adapter);
list_View.setSelection(adapter.getCount() - 1);
adapter.registerDataSetObserver(new DataSetObserver() {
#Override
public void onChanged() {
super.onChanged();
list_View.setSelection(adapter.getCount() - 1);
}
LoadDataTask ldt = new LoadDataTask();
ldt.execute("123456789");
}
private void loadDataFromDataStore(final Long contactId){
final ArrayList<Data> data = new ArrayList<>();;
d("loadingdataFromDatasore");
GetDataTask task = new GetDataTask(new ApiTask.ResultListener() {
#Override
public void successHook(Object o) {
if (o instanceof GetDataResponse) {
GetDataResponse res = (GetDataResponse) o;
if (res.getData() != null && res.getData().getItems() != null) {
for (ListDataItem i : res.getData().getItems()) {
Data dp = new Data(i.getPosition(), i.getMessage(), i.getDateCreated(),i.getMessageId(),1);
adapter.addFromOtherThread(dp);
}
}
d("Messages loaded from server: " + adapter.getCount());
}
}
}
public class LoadDataTask extends AsyncTask<String,String,Void> {
#Override
protected Void doInBackground(String... params){
if(isOnline && isFirstTime){
loadDataFromDataStore(Long.valueOf(params[0]));
}else{
//load from database
}
return null;
}
#Override
protected void onPostExecute(Void v){
adapter.notifyDataSetChanged();
}
}
My adapter class that extends BaseAdapter (I have removed unnecessary code for this question):
public class DataAdapter extends BaseAdapter {
private ArrayList<Data>data_list;
public DataAdapter(){
data_list = new ArrayList<>();
}
public void addFromOtherThread(Data object) {
data_list.add(object);
}
What am I missing that is making listview not to automatically refresh itself even after calling notifyDatasetChanged()?
change :
#Override
protected void onPostExecute(Void v){
adapter.notifyDataSetChanged();
}
}
to:
#Override
protected void onPostExecute(Void v){
adapter.notifyDataSetChanged();
list_View.setAdapter(adapter);
}
}
Let me know if more clarification is required by commenting below.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xyz);
list_View = (ListView) findViewById(R.id.data_listView);
list_View.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
list_View.setMultiChoiceModeListener(new Selector());
adapter = new MyAdapter(context,Long.valueOf(id),isOnline());
list_View.setAdapter(adapter);
list_View.setSelection(adapter.getCount() - 1);
adapter.registerDataSetObserver(new DataSetObserver() {
#Override
public void onChanged() {
super.onChanged();
list_View.setSelection(adapter.getCount() - 1);
}
loadDataFromDataStore("123456789")
}
private void loadDataFromDataStore(final Long contactId){
final ArrayList<Data> data = new ArrayList<>();;
d("loadingdataFromDatasore");
new GetDataTask(new ApiTask.ResultListener() {
#Override
public void successHook(Object o) {
if (o instanceof GetDataResponse) {
GetDataResponse res = (GetDataResponse) o;
if (res.getData() != null && res.getData().getItems() != null) {
for (ListDataItem i : res.getData().getItems()) {
Data dp = new Data(i.getPosition(), i.getMessage(), i.getDateCreated(),i.getMessageId(),1);
adapter.addFromOtherThread(dp);
}
}
d("Messages loaded from server: " + adapter.getCount());
adapter.notifyDatasetChanges();
}
}
}.execute();
}
GetDataTask should work on background internally you don't need to starts a AsyncTask from here.
If you want to use AsyncTask then your AsyncTask should wait for the result from GetDataTask which it is not doing in your code implementation.
I don't know which kind of framework you are using to making api call but your implementation seems to look wrong.
I have write the code on assumption bases if your GetDataTask is a AsyncTask or some background processor it will work perfectly.

ListView add items two times in onCreate

In my MasterDetail Flow I call this function to create my list:
public class Ansicht extends SherlockFragmentActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
//...
ListItems.setupMainItems(this);
//...
}
}
public class ListItems
{
public static List<DetailListItem> ITEMS = new ArrayList<DetailListItem>();
public static Map<String, DetailListItem> ITEM_MAP = new HashMap<String, DetailListItem>();
public static void addItem(String value, Typ t)
{
DetailListItem item = new DetailListItem(value, t);
ITEMS.add(item);
ITEM_MAP.put(item.id, item);
}
//...
}
public class ListControl extends SherlockListFragment
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
adapter = new ArrayAdapter<ListItems.DetailListItem>(getSherlockActivity(), R.layout.simple_listitem, android.R.id.text1,ListItems.ITEMS);
setListAdapter(adapter);
}
//...
}
But if I close my app and start it again the items are insert a second time. I think I do not have to call this function in onCreate but this image does not really help me because onCreate is the first method with is called.
In my setupMainItems method I only call the addItem method. The onPause and onResume are never overriden. If I close my app with the homebutton all works great but if I close it with the back button the items are insert a second time.
You could do something like
if(ListItems.ITEMS == null || ListItems.ITEMS.size() == 0)
ListItems.setupMainItems(this);
That way it'll only add the items if they haven't been added already

Categories

Resources