adapter.notifyDataSetChanged() not working when I update ArrayList - android

I have seen a lot of post about this case but I don't find a solution for me. I use SwipeRefreshLayout.OnRefreshListener and this is my code in onRefresh() method. This doesn't work.
new Handler().postDelayed(new Runnable() {
#Override public void run() {
Aviso aviso = new Aviso();
aviso.setTitle("MMMMMMMMMMMMMMMMMMMMMM");
aviso.setDescription("Deskribapena");
aviso.setPubDate("Wed, 19 Mar 2016 12:40:00 GMT");
aviso.setDcDate("2016-03-19T12:40:00Z");
avisosList.add(aviso);
swipeRefresh.setRefreshing(false);
adapter.notifyDataSetChanged();
}
}, 2000);
But if I create new adapter and set in the ListView the result is good
new Handler().postDelayed(new Runnable() {
#Override public void run() {
Aviso aviso = new Aviso();
aviso.setTitle("MMMMMMMMMMMMMMMMMMMMMM");
aviso.setDescription("Deskribapena");
aviso.setPubDate("Wed, 19 Mar 2016 12:40:00 GMT");
aviso.setDcDate("2016-03-19T12:40:00Z");
avisosList.add(aviso);
adapter = new AvisosEnListaAdapter(getActivity(), avisosList);
lv.setAdapter(adapter);
swipeRefresh.setRefreshing(false);
}
}, 2000);
Adapter code:
public class AvisosEnListaAdapter extends BaseAdapter{
protected Activity activity;
protected ArrayList<Aviso> items;
//Constructor
public AvisosEnListaAdapter (Activity activity, ArrayList<Aviso> items){
this.activity = activity;
this.items = items;
}
public AvisosEnListaAdapter(ArrayList<Aviso> items) {
this.items = items; // TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return items.get(position).get_Id();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if(convertView == null){
LayoutInflater inf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inf.inflate(R.layout.custom_avisosenlista, null);
}
//Create Aviso object
Aviso aviso = items.get(position);
//write title
TextView title = (TextView) v.findViewById(R.id.custom_avisosenlista_tv_title_aviso);
//Fuente
Typeface fontTitulo = Typeface.createFromAsset(activity.getAssets(), "fonts/OpenSans-Regular.ttf");
//Setter fuente
title.setTypeface(fontTitulo);
title.setText(aviso.getTitle());
//write data
TextView tvData = (TextView) v.findViewById(R.id.custom_avisosenlista_tv_data_aviso);
Typeface fontData = Typeface.createFromAsset(activity.getAssets(), "fonts/OpenSans-LightItalic.ttf");
//Setter fuente
tvData.setTypeface(fontData);
tvData.setText(aviso.getFechaParaPublicar());
return v;
}
}
Where is the problem?
Well, exist a little problem that the element appear in the last position and I want to appear in the first position.

I have one way
Create refresh method in your Adapter like
public void refresh(ArrayList<Aviso> itemsw) {
this.items = itemsw;
notifyDataSetChanged();
}
Now you just called this method from your Activity
Aviso aviso = new Aviso();
aviso.setTitle("MMMMMMMMMMMMMMMMMMMMMM");
aviso.setDescription("Deskribapena");
aviso.setPubDate("Wed, 19 Mar 2016 12:40:00 GMT");
aviso.setDcDate("2016-03-19T12:40:00Z");
avisosList.add(aviso);
adapter.refresh(avisosList);
EDIT:
To add the element at a particular location, use a different overload method for arraylist
instead of
avisosList.add(aviso);
use
avisosList.add(index, aviso); //Indes is the position where you want your item to appear

Any time in your Activity/Fragment do you do any avisosList = new ArrayList(); or something like that to re-add the new items? If so, you must know that a new object is being created, so it doesn't link to the adapter list object, so that's why it is failing to update.
You can only do the "new ArrayList()" only once when managing the adapter, and then to change the content of the List everytime you want to change the adapter, and then, of course, to call to notifyDataSetChanged();

Reload the list elements into you adapter.
Eg:
adapter.setListItems(avisosList);
adapter.notifyDataSetChanged();

You need to implement the addition of the element in the adapter instead of adding an element to the list from which the adapter is created
public class AvisosEnListaAdapter extends BaseAdapter{
protected Activity activity;
protected ArrayList<Aviso> items;
........
public void addItem(Aviso item){
items.add(item);
}
}
then
adapter.addItem(aviso);
adapter.notifyDataSetChanged();

Related

How to update values in database using an update image button in custom list adapter?

This is my custom list adapter. I want to update the values in table using the update ImageButton in the list. On clicking it, the old values should be shown in a new activity and then the edited value must be stored in the database. However, I am unable to pass an intent inside the onClick() method.
Please suggest me a solution
public class CustomListAdapter extends BaseAdapter implements ListAdapter
{
private ArrayList<String> list = new ArrayList<String>();
private Context context;
OnItemSelectedListener onItemSelectedListener;
public int pos;
String pass,pass2,edit,epass;
public CustomListAdapter(List list, Context context) {
this.list = (ArrayList<String>) list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
//pass2 = list.toString();
return list.get(pos);
}
//#Override
//public Long getItemId(int pos) {
//
// //just return 0 if your list items do not have an Id variable.
//}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.layout_custom_list, null);
}
//Handle TextView and display string from your list
final TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
ImageButton deleteBtn = (ImageButton)view.findViewById(R.id.delete_btn);
ImageButton editBtn = (ImageButton)view.findViewById(R.id.edit_btn);
//Button addBtn = (Button)view.findViewById(R.id.add_btn);
deleteBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
list.remove(position);
pass = listItemText.getText().toString();
notifyDataSetChanged();
pass2 = pass.substring(0,pass.indexOf(' '));
System.out.println(pass2);
Moneydb.delete(pass2);
}
});
editBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v2) {
// TODO Auto-generated method stub
edit=listItemText.getText().toString();
epass = listItemText.getText().toString();
edit = epass.substring(0,epass.indexOf(' '));
Moneydb.edit(edit);
}
});
return view;
}
protected Context getContext() {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
//return list.get(position).getId();
return 0;
}
public void clear() {
//CustomListAdapter collection = null;
// TODO Auto-generated method stub
list.clear();
notifyDataSetChanged();
}
I suggest you to assign and ContextMenu to your list view with two MenuItem, Edit and Delete and write associated code outside of adapter
or you can start Activity by :
Intent new_intent = new Intent(v.getRootView().getContext(),edit_activity.class);
new_intent.putExtra("Key","Value");
v.getRootView().getContext().startActivity(new_intent);
i think the first method is best ;)

How to refresh Listview within BaseAdapter? [duplicate]

This question already has answers here:
how to refresh custom listview using baseadapter in android
(5 answers)
Closed 8 years ago.
I have application this application contain listview , Listview retrieve data from local database sqlite , by using adapter this is my Adapter class code :
public class CommandsListAdapter extends BaseAdapter {
public static String ID = null;
private List<String> data;
ArrayList<HashMap<String, String>> commandList = new ArrayList<HashMap<String, String>>();
private ArrayAdapter<String> listAdapter;
Context context;
public CommandsListAdapter(Context con,
ArrayList<HashMap<String, String>> list) {
commandList = list;
context = con;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return commandList.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return commandList.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(final int index, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.people_list, null);
}
TextView tvTime = (TextView) convertView.findViewById(R.id.timeOfBlock);
TextView tvName = (TextView) convertView.findViewById(R.id.name);
tvName.setText(commandList.get(index).get(MyDatabase.Number_Block));
tvTime.setText(commandList.get(index).get(MyDatabase.Time_Of_Block));
Typeface tf = Typeface.createFromAsset(context.getAssets(),
"segoeuil.ttf");
tvTime.setTypeface(tf);
tvName.setTypeface(tf);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Message.message(context,
// commandList.get(index).get(MyDatabase.Block_Table_Id));
Intent i = new Intent(context, MessageDetail.class);
i.putExtra(ID,
commandList.get(index).get(MyDatabase.Block_Table_Id)
.toString());
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(i);
}
});
convertView.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
ID=commandList.get(index).get(MyDatabase.Block_Table_Id);
removeListItem(v, index);
return false;
}
});
return convertView;
}
protected void removeListItem(View rowView, final int positon) {
final Animation animation = AnimationUtils.loadAnimation(
context, android.R.anim.slide_out_right);
rowView.startAnimation(animation);
Handler handle = new Handler();
handle.postDelayed(new Runnable() {
#Override
public void run() {
commandList.remove(positon);
MyDatabase db=new MyDatabase(context);
db.open();
// db.deleteBlock(ID);
db.close();
Message.message(context, ID);
}
}, 1000);
}
}
and this is my Activity Class code :
public class ListOfBlock extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_of_block);
ActionBar actionBar = getActionBar();
// for color
actionBar.setBackgroundDrawable(new ColorDrawable(Color
.parseColor("#a20000")));
if (actionBar != null) {
actionBar.setTitle(getResources().getString(
R.string.title_activity_list_of_block));
actionBar.setDisplayHomeAsUpEnabled(true);
// actionBar.setIcon(R.drawable.ic_launcher);
}
ListView lvCommands = (ListView) findViewById(R.id.ListOfBlockerListView);
lvCommands.setAdapter(new CommandsListAdapter(getApplicationContext(),
new MyDatabase(getApplicationContext()).getBlockers()));
}
}
now I want to refresh the listview within getview method , I don't know how to do this , and I found some answer but really this is not helped me , thanks for any help .
YourActivity.this.recreate();
This will recreate your list. Keep a variable say n=false in the actvity where you perfom those operations. Then when you call back the activity with list, just pass this as true.
In the activiy where you perfom those operations,
Intent i = new Intent(Operations.this,ListActivity.class);
ListActivity.n=true;
startActivity(i);
In onCreate of ListActivity create a class variable boolean n=false;
if(n){
ListActivity.this.recreate();
}
Hope it helps. Cheers!
I hope this is help you , within the class adapter do this :-
ArrayList<HashMap<String, String>> commandList = new ArrayList<HashMap<String, String>>();
private ArrayList<HashMap<String, String>> searchArrayList;
public CommandsListAdapter(Context con,
ArrayList<HashMap<String, String>> list) {
commandList = list;
context = con;
searchArrayList=list;
}
and create a function for update your listview like this , also within Adapter Class :-
public void updateResults(ArrayList<HashMap<String, String>> results) {
searchArrayList = results;
//Triggers the list update
notifyDataSetChanged();
}
and any where you want to update your listview call this function , and I did this for you Sister , like this :-
updateResults(commandList);
one thing for sure these codes above this is all your code but I made some modify , and the parameter of updateResults function commandList this is your ArrayAdapter you defined already .
I hope this is work
Simply calling notifyDataSetChanged() of BaseAdapter class refresh the list view items. Call it appropriately based upon your requirement.
Cheers.

How do I get my CustomListAdapter to update on notifyDatasetChange?

This is the first time I am building a Listview with a custom layout, so in case I have missed something obvious please just point it out.
The problem I am having that I cannot get the listview to update itself with new information after the Oncreate(); has been used. So the list is very static.
I am trying to create a custom listview adapter that looks as such:
public class MainListCustomBaseAdapter extends BaseAdapter {
static ArrayList<ListItems> DataSomething;
static Context Cont;
public MainListCustomBaseAdapter (ArrayList<ListItems> data, Context c){
DataSomething = data;
Cont = c;
}
public int getCount() {
// TODO Auto-generated method stub
return DataSomething.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return DataSomething.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)Cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mainlistlayout, null);
}
ImageView image = (ImageView) v.findViewById(R.id.ListImage);
TextView titleView = (TextView)v.findViewById(R.id.title);
TextView DetailItemView = (TextView)v.findViewById(R.id.DetailItem);
ListItems msg = DataSomething.get(position);
image.setImageResource(msg.icon);
titleView.setText(msg.title);
DetailItemView.setText("ItemDetails: "+msg.ItemDetails);
return v;
}
public void updateResults(ArrayList<MainListCustomBaseAdapter> results){
notifyDataSetChanged();
}
}
My Oncreate looks like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecipeList = (ListView) findViewById(R.id.mainListView);
ShoppingItems = new ArrayList<ListItems>();
RecipeList.setAdapter(new MainListCustomBaseAdapter(ShoppingItems, this));
ListItems Detail;
Detail = new ListItems();
Detail.setIcon(R.drawable.food);
Detail.setName("Food Stuff");
Detail.setItemDetails("ItemDetailsComp");
ShoppingItems.add(Detail);
}
and listitem looks like this:
public class ListItems {
public int icon ;
public String title;
public String ItemDetails;
public String getName() {
return title;
}
public void setName(String from) {
this.title = from;
}
public String getItemDetails() {
return ItemDetails;
}
public void setItemDetails(String ItemDetailsComp) {
this.ItemDetails = ItemDetailsComp;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
}
How do I get the listview to update dynamically? with maybe a SetInvalidatedViews() or notifyDatasetchanged()?
Any help is deeply appreciated.
Use the below
MainListCustomBaseAdapter adapter = new MainListCustomBaseAdapter(ShoppingItems, this)
RecipeList.setAdapter(adapter);
To refresh or update listview
adapter.notifyDataSetChanged();
public void notifyDataSetChanged ()
Added in API level 1
Notifies the attached observers that the underlying data has been
changed and any View reflecting the data set should refresh itself.
put this line after adding the element in the arraylist
RecipeList.setAdapter(new MainListCustomBaseAdapter(ShoppingItems, this));

Getting AsyncTask result data from Fragment

I have AsyncTask class called LoadXMLData, and as you can see I parse XML data in doInBackground() method.
public class LoadXMLData extends AsyncTask<String, RSSFeed, RSSFeed>{
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;
private Context context;
RSSFeed feed;
public LoadXMLData(Context context) {
this.context = context;
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setMessage("Molimo Vas, sačekajte. Podaci se učitavaju.");
}
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
Log.d("OVDE SAM:", "onPreExecute()");
}
#Override
protected RSSFeed doInBackground(String... urls) {
// Obtain feed
DOMParser myParser = new DOMParser();
feed = myParser.parseXml(urls[0]);
Log.d("OVDE SAM:", "PARSIRAM XML");
return feed;
}
#Override
protected void onPostExecute(RSSFeed result) {
mProgressDialog.dismiss();
super.onPostExecute(result);
}
}
And I have few fragments, where I need to get data from that AsyncTask. How I could do that?
Here is the code of an fragment called NajnovijeFragment.
public class NajnovijeFragment extends Fragment{
GridView lv;
RSSFeed feed;
CustomListAdapter adapter;
private String RSSFEEDURL = "http://balkanandroid.com/feed/";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_najnovije, container,
false);
lv = (GridView) view.findViewById(R.id.GridView1);
// Set an Adapter to the ListView
adapter = new CustomListAdapter();
lv.setAdapter(adapter);
// Set on item click listener to the ListView
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// actions to be performed when a list item clicked
int pos = arg2;
Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);
Intent intent = new Intent(getActivity(), DetailsActivity.class);
intent.putExtras(bundle);
intent.putExtra("pos", pos);
startActivity(intent);
}
});
return view;
}
#Override
public void onDestroy() {
super.onDestroy();
adapter.imageLoader.clearCache();
adapter.notifyDataSetChanged();
}
class CustomListAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public ImageLoader imageLoader;
public CustomListAdapter() {
layoutInflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(getActivity().getApplicationContext());
}
public int getCount() {
// TODO Auto-generated method stub
// Set the total list item count
return feed.getItemCount();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate the item layout and set the views
View listItem = convertView;
int pos = position;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.list_item, null);
}
// Initialize the views in the layout
ImageView iv = (ImageView) listItem.findViewById(R.id.thumb);
TextView tvTitle = (TextView) listItem.findViewById(R.id.title);
TextView tvDate = (TextView) listItem.findViewById(R.id.tvDate);
// Set the views in the layout
imageLoader.DisplayImage(feed.getItem(pos).getImage(), iv);
tvTitle.setText(feed.getItem(pos).getTitle());
tvDate.setText(feed.getItem(pos).getDate());
return listItem;
}
}
}
The easiest way to get data from an ASyncTask is by implementing a callback.
Create an Interface:
public interface OnXMLLoadFinishedListener {
public void onXMLDataReady(RSSFeed results);
}
In you LoadXMLData:
private OnXMLLoadFinishedListener listener;
public void setOnXMLLoadFinishedListener(OnXMLLoadFinishedListener listener){
this.listener = listener;
}
#Override
protected void onPostExecute(RSSFeed result) {
super.onPostExecute(result);
listener.onXMLDataReady(RSSFeed results);
}
In your Fragment:
public class NajnovijeFragment extends Fragment implements OnXMLLoadFinishedListener{
and override onXMLDataReady:
#override
public void onXMLDataReady(RSSFeed results){
//display your data.
}
Make sure that when you create your AsyncTask instance you set the listener otherwise this will not work:
LoadXMLData xmlLoader = new LoadXMLData();
xmlLoader.setOnXMLLoadFinishedListener(this);
Your AsyncTask already knows context, so you could call back into your activity (called ActivityMain for illustrative purposes) in onPostExecute. e.g.
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
((ActivityMain) context).loadCompleteHandler(param1,param2,...)
}
It's then up to you how you want to implement loadCompleteHandler in your activity. Now your activity might not exist, so you must be careful to cancel the AsyncTask when the activity is removed. Fragments belonging to an activity can also access the activity.
AsyncTask is a Class that is very related to the UI, if you need to update the UI with this XML parsing you should take this consideration:
Make the asynctask an inner class in your fragment or
Pass the fragment to your asynctask
Update the fragment's view in onPostExecute()
In any case you should check if your activity is null, if so... avoid updating views, something like that:
onPostExecute(Object xml) {
if(getActivity != null) {
// update Views like...
textViewLabel.setText(parsedXml.getTitle);
}
}
I would suggest you to use SafeAsyncTask, which is a java class from the Roboguice Project, only one file, and it is related to java.util.concurrent.Callable, just copy and paste the source:
SafeAsyncTask.java
How to use it!

ListView Added Duplicate item in list when screen orientation changes

I trying to create List View i face some problem when orientation changes.
Problem is: when i changes orientation of screen list-view add duplicate list item in list. how to restrict this data change
Code Is:
public class DayPlannerActivity extends Activity {
private TextView txtHeader;
private Context mContext;
private ListView lvDayplanner;
private DayPlannerAdapter adapter;
private Activity activity;
private static Vector<DayPlanner> list = new Vector<DayPlanner>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dayplanner);
mContext = this;
activity = this;
txtHeader = (TextView) findViewById(R.id.txtHeader);
txtHeader.setText(R.string.haivlate);
String[] Checks = {"select","Check1","Check2"};
DayPlanner dp = new DayPlanner("11:00 PM", Checks);
list.add(dp);
dp = new DayPlanner("12:00 PM", Checks);
list.add(dp);
lvDayplanner = (ListView) findViewById(R.id.lvDayplanner);
adapter= new DayPlannerAdapter(activity,list);
lvDayplanner.setAdapter(adapter);
}
}
List Adapter :
public class DayPlannerAdapter extends BaseAdapter {
private Activity mActivity;
private static Vector<DayPlanner> list;
private static LayoutInflater inflater;
public DayPlannerAdapter ( Activity _activity,Vector<DayPlanner> _list) {
mActivity = _activity;
list = _list;
inflater = (LayoutInflater)mActivity.getSystemService(mActivity.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder{
public TextView txtScheduledTime;
public Spinner spnrChecks;
public Button btnGo;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.dayplanner_listitem, null);
holder=new ViewHolder();
holder.txtScheduledTime=(TextView)vi.findViewById(R.id.txtScheduledTime);
holder.spnrChecks = (Spinner) vi.findViewById(R.id.spnrChecks);
holder.btnGo = (Button) vi.findViewById(R.id.btnGo);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.txtScheduledTime.setText(list.get(position).getScheduledTime());
ArrayAdapter<String> spnrAdapter=new ArrayAdapter<String>(mActivity,
android.R.layout.simple_spinner_item, list.get(position).getChecks());
spnrAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.spnrChecks.setAdapter(spnrAdapter);
holder.btnGo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.setClass(mActivity,DayPlannerFormActivity.class);
mActivity.startActivity(i);
}
});
return vi;
}
}
Since Android automatically saves Views states when the orientation changes, you need a way to know if it's not the first call to onCreate. Luckily, it's easy: Override onSaveInstanceState, and store even 1 value to make the bundle your get in onCreate not-null.
#Override
public void onSaveInstanceState(Bundle outInstanceState) {
outInstanceState.putInt("value", 1);
}
Then, when the activity is recreated, the parameter savedInstanceState in onCreate will not be null. So just do the test:
if(savedInstanceState != null)
Before you add data to your views.
its because your list of dayplanner objects is static, so when you change the orientation of the view it recreates the activity but since in java a static object is not recreated, but saved for that type, it makes the list have two of the same.
Way too late the party here,will still answer as it might be useful to someone else. I was also having the same issue it was resolved by PROPERLY implementing the view holder design pattern.

Categories

Resources