I'm trying to figure out how to implement an infinitely scrolling list. It will display a calendar and events and it should start from now or selected date. It should be scrollable in both directions, past and future. The solutions with OnScrollListener here seem to work pretty well if I only need to go to future (index just grows bigger). But I don't see how I would go to the past.
This solution seems to be very wasteful for my case. getView is called thousands of times. Maybe ListView isn't the solution, and I'll have to go with lower-level code. Any ideas?
EDIT: getView being called thousands of times wasn't the fault of the latter solution. However, it still gets called too many times and with wrong values. If I set selection like this:
myList.setSelection(Integer.MAX_VALUE/2)
I get getView calls with indexes starting from zero. For example, I get getView calls like this:
getView pos 0
...
getView pos 26
and then
getView pos 1073741823
...
getView pos 1073741847
Which are the correct ones. Then:
getView pos 0
...
getView pos 26
again
This all happens before I scroll or touch the screen at all. Doesn't seem to make much sense.
Here is an implementation of this task.
EndlessScrollBaseAdapter.java
package com.example.endlessscrollinbothdirections;
import java.util.Map;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.TextView;
/** A child class shall subclass this Adapter and implement method getDataRow(int position,
* View convertView, ViewGroup parent), which supplies a View present data in a ListRow.
* This parent Adapter takes care of displaying ProgressBar in a row or indicating that it
* has reached the last row. */
public abstract class EndlessScrollBaseAdapter<T> extends BaseAdapter implements
OnScrollListener {
private int mVisibleThreshold = 5;
// the main data structure to save loaded data
protected Map<Integer, T> mItems;
protected Context mContext;
// the serverListSize is the total number of items on the server side,
// which should be returned from the web request results
protected int mServerListSize = -1;
// Two view types which will be used to determine whether a row should be displaying
// data or a Progressbar
public static final int VIEW_TYPE_LOADING = 0;
public static final int VIEW_TYPE_ACTIVITY = 1;
public static final int VIRTUAL_MIDDLE_OFFSET = Integer.MAX_VALUE / 2;
public EndlessScrollBaseAdapter(Context context, Map<Integer, T> items) {
mContext = context;
mItems = items;
}
public void setServerListSize(int serverListSize) {
this.mServerListSize = serverListSize;
}
/** disable click events on indicating rows */
#Override
public boolean isEnabled(int position) {
return getItemViewType(position) == EndlessScrollBaseAdapter.VIEW_TYPE_ACTIVITY;
}
/** One type is normal data row, the other type is Progressbar */
#Override
public int getViewTypeCount() {
return 2;
}
/** the size of the List plus one, the one is the last row, which displays a
* Progressbar */
#Override
public int getCount() {
return Integer.MAX_VALUE;
}
/** return the type of the row, the last row indicates the user that the ListView is
* loading more data */
#Override
public int getItemViewType(int position) {
return mItems.containsKey(position
- EndlessScrollBaseAdapter.VIRTUAL_MIDDLE_OFFSET) ? EndlessScrollBaseAdapter.VIEW_TYPE_ACTIVITY
: EndlessScrollBaseAdapter.VIEW_TYPE_LOADING;
}
#Override
public T getItem(int position) {
return mItems.get(position - EndlessScrollBaseAdapter.VIRTUAL_MIDDLE_OFFSET);
}
#Override
public long getItemId(int position) {
return position;
}
/** returns the correct view */
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (getItemViewType(position) == EndlessScrollBaseAdapter.VIEW_TYPE_LOADING) {
return getFooterView(position, convertView, parent);
}
return getDataRow(position, convertView, parent);
};
/** A subclass should override this method to supply the data row.
*
* #param position
* #param convertView
* #param parent
* #return */
public abstract View getDataRow(int position, View convertView, ViewGroup parent);
/** returns a View to be displayed in the last row.
*
* #param position
* #param convertView
* #param parent
* #return */
public View getFooterView(int position, View convertView, ViewGroup parent) {
if (position >= mServerListSize && mServerListSize > 0) {
// the ListView has reached the last row
TextView tvLastRow = new TextView(mContext);
tvLastRow.setHint("Reached the last row.");
tvLastRow.setGravity(Gravity.CENTER);
return tvLastRow;
} else {
TextView tvLastRow = new TextView(mContext);
tvLastRow.setHint("Loading...\n position: " + position);
tvLastRow.setGravity(Gravity.CENTER);
return tvLastRow;
}
}
// Defines the process for actually loading more data based on page
public abstract void onLoadMore(int virtualPosition);
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
for (int i = -mVisibleThreshold; i < visibleItemCount + mVisibleThreshold; i++) {
int virtualPosition = firstVisibleItem
- EndlessScrollBaseAdapter.VIRTUAL_MIDDLE_OFFSET + i;
onLoadMore(virtualPosition);
}
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
}
EndlessScrollAdapter.java
package com.example.endlessscrollinbothdirections;
import java.util.Map;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class EndlessScrollAdapter extends EndlessScrollBaseAdapter<Integer> {
public EndlessScrollAdapter(Activity activity, Map<Integer, Integer> list) {
super(activity, list);
}
#Override
public View getDataRow(int position, View convertView, ViewGroup parent) {
TextView TextView;
if (convertView == null) {
TextView = new TextView(mContext);
} else {
TextView = (TextView) convertView;
}
TextView.setText("virtualPosition: "
+ (position - EndlessScrollBaseAdapter.VIRTUAL_MIDDLE_OFFSET) + "\n"
+ "row data: "
+ mItems.get(position - EndlessScrollBaseAdapter.VIRTUAL_MIDDLE_OFFSET));
return TextView;
}
#Override
public void onLoadMore(int virtualPosition) {
// here you might launch an AsyncTask instead
if (!mItems.containsKey(virtualPosition)) {
mItems.put(virtualPosition, virtualPosition);
notifyDataSetChanged();
}
}
}
MainActivity.java
package com.example.endlessscrollinbothdirections;
import java.util.HashMap;
import java.util.Map;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.lvItems);
Map<Integer, Integer> items = new HashMap<Integer, Integer>();
EndlessScrollAdapter endlessScrollAdapter = new EndlessScrollAdapter(this, items);
listView.setAdapter(endlessScrollAdapter);
listView.setSelection(EndlessScrollBaseAdapter.VIRTUAL_MIDDLE_OFFSET);
listView.setOnScrollListener(endlessScrollAdapter);
}
}
activity_main.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lvItems"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
Related
I have the following layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select an option" />
I use the above layout to set the default text of a spinner button, using this class:
/**
* Decorator Adapter to allow a Spinner to show a 'Nothing Selected...' initially
* displayed instead of the first choice in the Adapter.
*/
public class NothingSelectedSpinnerAdapter implements SpinnerAdapter, ListAdapter {
protected static final int EXTRA = 1;
protected SpinnerAdapter adapter;
protected Context context;
protected int nothingSelectedLayout;
protected int nothingSelectedDropdownLayout;
protected LayoutInflater layoutInflater;
/**
* Use this constructor to have NO 'Select One...' item, instead use
* the standard prompt or nothing at all.
* #param spinnerAdapter wrapped Adapter.
* #param nothingSelectedLayout layout for nothing selected, perhaps
* you want text grayed out like a prompt...
* #param context
*/
public NothingSelectedSpinnerAdapter(
SpinnerAdapter spinnerAdapter,
int nothingSelectedLayout, Context context) {
this(spinnerAdapter, nothingSelectedLayout, -1, context);
}
/**
* Use this constructor to Define your 'Select One...' layout as the first
* row in the returned choices.
* If you do this, you probably don't want a prompt on your spinner or it'll
* have two 'Select' rows.
* #param spinnerAdapter wrapped Adapter. Should probably return false for isEnabled(0)
* #param nothingSelectedLayout layout for nothing selected, perhaps you want
* text grayed out like a prompt...
* #param nothingSelectedDropdownLayout layout for your 'Select an Item...' in
* the dropdown.
* #param context
*/
public NothingSelectedSpinnerAdapter(SpinnerAdapter spinnerAdapter,
int nothingSelectedLayout, int nothingSelectedDropdownLayout, Context context) {
this.adapter = spinnerAdapter;
this.context = context;
this.nothingSelectedLayout = nothingSelectedLayout;
this.nothingSelectedDropdownLayout = nothingSelectedDropdownLayout;
layoutInflater = LayoutInflater.from(context);
}
#Override
public final View getView(int position, View convertView, ViewGroup parent) {
// This provides the View for the Selected Item in the Spinner, not
// the dropdown (unless dropdownView is not set).
if (position == 0) {
return getNothingSelectedView(parent);
}
return adapter.getView(position - EXTRA, null, parent); // Could re-use
// the convertView if possible.
}
/**
* View to show in Spinner with Nothing Selected
* Override this to do something dynamic... e.g. "37 Options Found"
* #param parent
* #return
*/
protected View getNothingSelectedView(ViewGroup parent) {
return layoutInflater.inflate(nothingSelectedLayout, parent, false);
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// Android BUG! http://code.google.com/p/android/issues/detail?id=17128 -
// Spinner does not support multiple view types
if (position == 0) {
return nothingSelectedDropdownLayout == -1 ?
new View(context) :
getNothingSelectedDropdownView(parent);
}
// Could re-use the convertView if possible, use setTag...
return adapter.getDropDownView(position - EXTRA, null, parent);
}
/**
* Override this to do something dynamic... For example, "Pick your favorite
* of these 37".
* #param parent
* #return
*/
protected View getNothingSelectedDropdownView(ViewGroup parent) {
return layoutInflater.inflate(nothingSelectedDropdownLayout, parent, false);
}
#Override
public int getCount() {
int count = adapter.getCount();
return count == 0 ? 0 : count + EXTRA;
}
#Override
public Object getItem(int position) {
return position == 0 ? null : adapter.getItem(position - EXTRA);
}
#Override
public int getItemViewType(int position) {
return 0;
}
#Override
public int getViewTypeCount() {
return 1;
}
#Override
public long getItemId(int position) {
return position >= EXTRA ? adapter.getItemId(position - EXTRA) : position - EXTRA;
}
#Override
public boolean hasStableIds() {
return adapter.hasStableIds();
}
#Override
public boolean isEmpty() {
return adapter.isEmpty();
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
adapter.registerDataSetObserver(observer);
}
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {
adapter.unregisterDataSetObserver(observer);
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return position != 0; // Don't allow the 'nothing selected'
// item to be picked.
}
}
I initialize the above class like this:
NothingSelectedSpinnerAdapter myAdapter = new NothingSelectedSpinnerAdapter(spinnerAdapter, R.layout.layout_pasted_above, getContext());
myAdapter.setAdapter(spinnerAdapter);
However, I want to be able to change the text of the above layout programmatically.
How would I achieve this?
-
Apparently StackOverflow needs more words for me to submit this post, but I have no other important details to add, so I'm just adding this text in so I can actually submit this.
Get the id of your widget and set it to a textview variable in your java class
TextView variable= (TextView)findViewById(R.id.label);
then set the text to whatever string you want
variable.setText("insert your text");
You have to get the reference of TextView here after inflating the method.
protected View getNothingSelectedView(ViewGroup parent) {
View nothingSelectedView = layoutInflater.inflate(nothingSelectedLayout,
parent, false);
TextView labelText =(TextView)nothingSelectedView.findViewById(R.id.label);
labelText.setText("Set You Text Here");
return nothingSelectedView;
}
Just create a field, and expose a public method.
protected View getNothingSelectedView(ViewGroup parent) {
View nothingSelectedView = layoutInflater.inflate(nothingSelectedLayout,
parent,
false);
textViewField = (TextView) nothingSelectedView.findViewById(R.id.label);
return nothingSelectedView;
}
// Expose public method
public void changeText(String text) {
textViewField.setText(text);
}
Call it from wherever
adapter.changeText("new text");
Im attempting to add two different views to the GridviewLayoutManager using a custom adapter.
However, I cant seem to reference the headerview correctly. When the onbindViewHolder is called it is expecting a "ViewHolder" response, however i really want to reference the HeaderView i crated
Because I cant access the correct view, I also cant reference the TextView within the XML layout I am calling.
here is my customer adaptor class:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class ElementsAdapter extends RecyclerView.Adapter<ElementsAdapter.ViewHolder> {
private ArrayList<String> mDataset;
private ArrayList<Integer> mDatamap;
public Context context;
private static final int VIEW_HEADER = 0;
private static final int VIEW_NORMAL = 1;
private View headerView;
private int datasetSize;
public class HeaderHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView headertext;
public HeaderHolder(View v) {
super(v);
headertext = (TextView) v.findViewById(R.id.headertext);
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView txtHeader;
public TextView txtFooter;
public ImageView imgImage;
public ViewHolder(View v) {
super(v);
txtHeader = (TextView) v.findViewById(R.id.firstLine);
txtFooter = (TextView) v.findViewById(R.id.secondLine);
imgImage = (ImageView) v.findViewById(R.id.icon);
}
}
public ElementsAdapter(ArrayList<String> myDataset, ArrayList<Integer> myDatamap) {
mDataset = myDataset;
myDatamap = mDatamap;
}
#Override
public int getItemViewType(int position) {
return isHeader(position) == 1 ? VIEW_HEADER : VIEW_NORMAL;
}
#Override
public int getItemCount() {
return mDataset.size();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_HEADER) {
// create a new view
View sub_view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
Context context = sub_view.getContext();
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(sub_view);
return vh;
// return new HeaderViewHolder(headerView);
} else {
// create a new view
View sub_view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sub_layout, parent, false);
context = sub_view.getContext();
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(sub_view);
return vh;
}
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
if (isHeader(position) == 1) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final String name = mDataset.get(position);
// holder.txtHeader.setText(mDataset.get(position));
viewHolder.headertext.setText(name);
} else {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final String name = mDataset.get(position);
Picasso.with(context).load("http://www.500kgiveaway.co.uk/"+name).resize(200,200).into(viewHolder.imgImage);
// holder.txtHeader.setText(mDataset.get(position));
viewHolder.txtHeader.setText(name);
viewHolder.txtHeader.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick (View v){
//remove(name);
}
}
);
viewHolder.txtFooter.setText("Footer: "+mDataset.get(position));
}
//ViewHolder holder = (ViewHolder) viewHolder;
//holder.textView.setText("Position " + (position - 1));
}
public int isHeader(int position) {
return mDatamap.get(position) ==1 ? 1:0;}
}
It seems to me that the isHeader() method will always return 0, since you compare a String with a integer. I assume you would like want to check the position of the current item to be 1.
Try this code instead:
public boolean isHeader(int position) {
return position == 1;
}
Then replace
if (isHeader(position) == 1)...
with
if (isHeader(position))...
I hope this helps.
Edit
The above was intended. Sorry.
In the class definition ElementsAdapter.ViewHolder is inserted as the ViewHolder type. This works for the normal
ElementsAdapter.ViewHolder extends RecyclerView.ViewHolder
but not for
ElementsAdapter.HeaderHolder extends RecyclerView.ViewHolder
since it doesn't extend ElementsAdapter.ViewHolder.
You should therfore specify RecyclerView.ViewHolder instead as a generic type to support both of your types.
I am trying to accomplish this: http://blog.uncommons.org/2011/05/09/embedding-admob-adverts-in-android-listviews/.
I am stuck at setting the custom BaseAdapter I simply don't know what to put in the BASEADAPTER variable below.
Here is my adapter
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
/**
* List adapter decorator that inserts adverts into the list.
* #author Daniel Dyer
*/
public class AdvertisingAdapter extends BaseAdapter
{
private static final String ADMOB_PUBLISHER_ID = "---------------";
private final Activity activity;
private final BaseAdapter delegate;
private int resource;
private ArrayList<String> objects;
public AdvertisingAdapter(Activity activity, int resource, BaseAdapter delegate,
ArrayList<String> stories) {
// TODO Auto-generated constructor stub
this.resource = resource;
this.activity = activity;
this.delegate = delegate;
this.objects = stories;
delegate.registerDataSetObserver(new DataSetObserver()
{
#Override
public void onChanged()
{
notifyDataSetChanged();
}
#Override
public void onInvalidated()
{
notifyDataSetInvalidated();
}
});
}
public int getCount()
{
return delegate.getCount() + 1;
}
public Object getItem(int i)
{
return delegate.getItem(i - 1);
}
public long getItemId(int i)
{
return delegate.getItemId(i - 1);
}
public View getView(int position, View convertView, ViewGroup parent)
{
if ((position % 10) == 0)
{
if (convertView instanceof AdView)
{
return convertView;
}
else
{
AdView adView = new AdView(activity, AdSize.BANNER, ADMOB_PUBLISHER_ID);
// Disable focus for sub-views of the AdView to avoid problems with
// trackpad navigation of the list.
for (int i = 0; i < adView.getChildCount(); i++)
{
adView.getChildAt(i).setFocusable(false);
}
adView.setFocusable(false);
// Default layout params have to be converted to ListView compatible
// params otherwise there will be a ClassCastException.
float density = activity.getResources().getDisplayMetrics().density;
int height = Math.round(AdSize.BANNER.getHeight() * density);
AbsListView.LayoutParams params
= new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
height);
adView.setLayoutParams(params);
adView.loadAd(new AdRequest());
return adView;
}
}
else
{
return delegate.getView(position - 1, convertView, parent);
}
}
#Override
public int getViewTypeCount()
{
return delegate.getViewTypeCount() + 1;
}
#Override
public int getItemViewType(int position)
{
return position == 0 ? delegate.getViewTypeCount()
: delegate.getItemViewType(position - 1);
}
#Override
public boolean areAllItemsEnabled()
{
return false;
}
#Override
public boolean isEnabled(int position)
{
return position != 0 && delegate.isEnabled(position - 1);
}
}
And in my main activity here is where I'm trying to set my custom base adapter. What should the BASEADAPTER parameter be?
mListView.setAdapter(new AdvertisingAdapter(this,
android.R.layout.simple_list_item_1, BASEADAPTER, stories));
Its depend what you trying to show or display in listview, normally application context and desire data is to be passed to BaseAdapter constructor. For details info read BaseAdapter and even check Android ListView detailed tutorial.
As my post states, I can't seem to get my ArrayAdapter to update the whole list despite the call "adapter.notifyDataSetChanged();" Only the last element in the list is updated.
Can someone maby see what I'm doing wrong below??
Here is my custom adapter
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class WeatherAdapter extends ArrayAdapter<Weather>{
Context context;
int layoutResourceId;
Weather data[] = null;
private WeatherHolder holder;
private Weather weather;
public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.imgIcon = (TextView)row.findViewById(R.id.imgen);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}
weather = data[position];
holder.txtTitle.setText(weather.getName());
//holder.imgIcon.setText(Double.toString(weather.getBuyingRate()));
return row;
}
public void update(String buttonPressed){
if(buttonPressed == "Köp Kurs"){
holder.imgIcon.setText(Double.toString(weather.getBuyingRate()));//This updates only the last element in list but I want to update every element in the list
}
else if(buttonPressed == "Antal"){ holder.imgIcon.setText(Double.toString(weather.getNrOfSharesInPortfolio()));//This updates only the last element in list but I want to update every element in the list
}
}
static class WeatherHolder
{
TextView imgIcon;
TextView txtTitle;
}
}
And here is my main class, when I call "Update()" method only the last element in the list is updated but not the others. How can I update the whole list instead of just the last element in the list?
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listView1;
private Button goButton;
private String[] listheader = {"Köp Kurs","Antal"};
private WeatherAdapter adapter;
private int totalElemInlist = listheader.length;
private int currentelemInList=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Weather weather_data[] = new Weather[]
{
new Weather("ABB", 56.0, 300),
new Weather("Volvo", 89.0,500),
new Weather("Astra Zeneca", 98.55, 50)
};
adapter = new WeatherAdapter(this,
R.layout.listview_item_row, weather_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
goButton = (Button) findViewById(R.id.testButton);
goButton.setText(listheader[currentelemInList]);
listView1.setAdapter(adapter);
goButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
String buttonPressed = (String) ((Button) view).getText();
goThroughList(buttonPressed);
System.out.println("Button Clicked" + buttonPressed);
}
});
}
private void goThroughList(String buttonPressed){
currentelemInList++;
if(currentelemInList>=totalElemInlist){
currentelemInList=0;
}
goButton.setText(listheader[currentelemInList]);
if(buttonPressed == "Köp Kurs"){
System.out.println("Köp kurs");
adapter.update(buttonPressed);
adapter.notifyDataSetChanged();//This only updates the last element in list
}
else if(buttonPressed == "Antal"){
System.out.println("Antal");
adapter.update(buttonPressed);
adapter.notifyDataSetChanged();//This only updates the last element in list
}
System.out.println(currentelemInList);
}
}
How can I update the whole list instead of just the last element in the list?
You must make your changes happen in getView(). getView() redraws every row as they appear whenever the user scrolls the ListView or notifyDataSetChanged() is called. So the changes must happen in there otherwise they will be erased.
First create a new field variable in your adapter:
private String currentImage;
Second change update() to control the contents on imgIcon:
public void update(String buttonPressed){
currentImage = buttonPressed;
notifyDataSetChanged();
}
Last change getView() to display the current images for each row:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
...
weather = data[position];
holder.txtTitle.setText(weather.getName());
if(currentImage.equals("Köp Kurs")){
holder.imgIcon.setText(Double.toString(weather.getBuyingRate()));
}
else if(currentImage.equals("Antal")){
holder.imgIcon.setText(Double.toString(weather.getNrOfSharesInPortfolio()));
}
return row;
}
(You can also simplify goThroughList(), since the if-else contains the exact same code.)
You should compare string like this.
buttonPressed.equals("Köp Kurs")
I might be wrong , but I think problem is because you are holding only last element in your holder. The member variable "holder" in adapter should be array or list, and you should hold all element, and in update method you should iterate through holder array and change the text
public void update(String buttonPressed){
for(int index =0 ; index < holder.size();index++){
if(buttonPressed == "Köp Kurs"){
holder[index].imgIcon.setText(Double.toString(weather.getBuyingRate()));//This updates only the last element in list but I want to update every element in the list
}
else if(buttonPressed == "Antal"){ holder[index].imgIcon.setText(Double.toString(weather.getNrOfSharesInPortfolio()));//This updates only the last element in list but I want to update every element in the list
}
This should solve your problem
I have a custom list view adapter populated through an asynctask, I'm calling notifydatasetchanged in the onprogress function, and getCount() returns 10, yet my list never shows, Ive set a breakpoint and determined that getView() simply never is called. any ideas? Ive tried for hours and Im just stumped. Ive done the exactly same thing in another activity except that one used viewholders, this one only holds text based data so I didn't bother.
Adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row == null) {
row = inflater.inflate(R.layout.podcastepisode, null);
}
PodcastItem item = items.get(position);
TextView episodeTitle = (TextView)row.findViewById(R.id.episodeTitle);
TextView episodeDate = (TextView)row.findViewById(R.id.episodeDate);
episodeTitle.setText(item.title);
episodeDate.setText(API.FormatPodcastDate(item.date));
return row;
}
My task:
protected void onProgressUpdate(PodcastItem... progress) {
AddPodcastActivity.episodes.add(progress[0]);
AddPodcastActivity.adapter.notifyDataSetChanged();
}
I'd recommend moving your list adapter from inside the Activity file to it's own file, and using something like this:
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public class EpisodeArrayAdapter extends BaseAdapter {
public EpisodeArrayAdapter(Context context) {
mContext = context;
items = new ArrayList<PodcastItem>();
}
private Context mContext;
private ArrayList<PodcastItem> items;
public void add(PodcastItem item) {
items.add(item);
notifyDataSetChanged();
}
public void remove(int index) {
items.remove(index);
notifyDataSetChanged();
}
public void clear() {
items.clear();
notifyDataSetChanged();
}
#Override
public int getCount() { return items.size(); }
#Override
public Object getItem(int position) { return items.get(position); }
#Override
public long getItemId(int position) { return position; }
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row == null) row = LayoutInflater.from(mContext).inflate(R.layout.podcastepisode, null);
PodcastItem item = items.get(position);
TextView episodeTitle = (TextView)row.findViewById(R.id.episodeTitle);
TextView episodeDate = (TextView)row.findViewById(R.id.episodeDate);
episodeTitle.setText(item.title);
episodeDate.setText(API.FormatPodcastDate(item.date));
return row;
}
}
This is the type of code we use for all the list adapters in Boid :) Also, notice that the add/remove/clear functions call notifyDataSetChanged(), which makes it so you don't have to call it yourself when adding items.
When you initialize it, you would just use:
EpisodeArrayAdapter adapter = new EpisodeArrayAdapter(this);
listView.setAdapter(adapter);
Adding items with the add() function will cause the list to update immediately. Make sure you call setAdapter for the list view that's using the adapter, otherwise there won't be any connection and nothing will show up in the list (didn't see a call to this in your code).