I have a ListView that has one image and two lines of texts for each element (organized by a RelativeLayout). It works ok, but it's too slow and I know where the problem comes from!
This is the getView() method for the custom adapter that I'm using:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.list_view_item, parent, false);
mViewHolder = new ViewHolder();
mViewHolder.cover = (ImageView) convertView.findViewById(R.id.app_icon);
mViewHolder.title = (TextView) convertView.findViewById(R.id.selection);
mViewHolder.description = (TextView) convertView.findViewById(R.id.app_short_description);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
// Here is the origin of the issue !
final Feed currentFeed = getItem(position);
mViewHolder.title.setText(currentFeed.getTitle());
mViewHolder.description.setText(currentFeed.getDescription());
try {
if(currentFeed.getThumbnailUrl() != null) {
downloadThumbnail(mViewHolder.cover, currentFeed.getThumbnailUrl());
}
} catch(Exception e) {
e.printStackTrace();
}
return convertView;
}
private static class ViewHolder {
TextView title;
TextView description;
ImageView cover;
}
So I have done some manual benchmarking and it appears that allocating an instance of Feed is the source of this slowness:
final Feed currentFeed = getItem(position);
I know this because I have written another version of this to compare the two:
// Here is the origin of the issue !
//final Feed currentFeed = getItem(position);
mViewHolder.title.setText("Title");
mViewHolder.description.setText("Description");
try {
if(currentFeed.getThumbnailUrl() != null) {
downloadThumbnail(mViewHolder.cover, "some url");
}
} catch(Exception e) {
e.printStackTrace();
}
This one was way smoother (even with the downloadThumbnail() method working).
I also precise that there are only 15 items on my ListView.
I know that allocating objects is very expensive because of garbage collection but I can't any other way to do it!
Any idea?
Thanks!
EDIT
Don't mind too much about the downloadThumbnail() method, it already does some caching. And actually even without any picture, it's still slow.
When user scrolls the list, getView gets called on the adapter. Make sure that you dont do same things repeatedly, for example generating thumbnail. If number of items is limited (for example video content), then you can create all views and keep it ready for get view. Otherwise you may have to implement cacheing.
Below code shows an adapter and listView implementation, where in all listviews are created and stored in memory. Since this is meant for video browsing, memory does not pose any issue. (limited number of content, max 100)
Video List Adapter
import java.util.ArrayList;
import java.util.Formatter;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
public class VideoListAdapter extends BaseAdapter {
private Context mContext = null;
private HashMap<String, VideoListItem> mHashedItems = new HashMap<String, VideoListItem>();
private static final String TAG = "VideoListAdapter";
public static final int VIDEO_CONTENT_ID = 0;
public static final int VIDEO_CONTENT_TITLE = 1;
public static final int VIDEO_CONTENT_DURATION = 2;
public static final int VIDEO_CONTENT_RESOLUTION = 3;
public static final int VIDEO_CONTENT_MIME = 4;
private Cursor mCursorForVideoList = null;
private ContentResolver mContentResolver = null;
private int mListCount = 0;
VideoListAdapter(Context context, ContentResolver cr) {
mContext = context;
mContentResolver = cr;
Log.i(TAG, "In the Constructor");
mCursorForVideoList =
mContentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.MediaColumns._ID,
MediaStore.MediaColumns.TITLE,
MediaStore.Video.VideoColumns.DURATION,
MediaStore.Video.VideoColumns.RESOLUTION
},
null,
null,
null);
mListCount = mCursorForVideoList.getCount();
}
#Override
public int getCount() {
return mListCount;
}
#Override
public Object getItem(int arg0) {
return getVideoListItem(arg0);
}
#Override
public long getItemId(int position) {
//Log.i(TAG, "position : " + position);
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//Log.i(TAG, "GetView :: Position : " + position);
return getVideoListItem(position);
}
private VideoListItem getVideoListItem(int position)
{
//Log.i(TAG, "getVideoListItem :: Position : " + position);
String key = Integer.toString(position);
VideoListItem item = mHashedItems.get(key);
if(item == null)
{
//Log.i(TAG, "New getVideoListItem :: Position : " + position);
mCursorForVideoList.moveToPosition(position);
mHashedItems.put(key, new VideoListItem(mContext, mContentResolver, mCursorForVideoList));
}
return mHashedItems.get(key);
}
};
Video List View
import java.util.Formatter;
import java.util.Locale;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
class VideoListItem extends LinearLayout
{
private static final String TAG = "VideoListAdapter";
private ImageView mThumbnail = null;
private TextView mDuration = null;
private TextView mTitle = null;
private TextView mResolution = null;
private LayoutInflater mLayoutFactory = null;
private long mContentId = 0;
public VideoListItem(Context context, ContentResolver cr, Cursor cursor) {
super(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
params.setMargins(10, 10, 10, 10);
mLayoutFactory = LayoutInflater.from(context);
View thisView = mLayoutFactory.inflate(R.layout.videolistitem, null);
addView(thisView);
mThumbnail = (ImageView) findViewById(R.id.thumbnail);
mDuration = (TextView) findViewById(R.id.DDuration);
mTitle = (TextView) findViewById(R.id.DTitle);
mResolution = (TextView) findViewById(R.id.DResolution);
mThumbnail.setLayoutParams(new LinearLayout.LayoutParams(144, 144));
Resources r = this.getResources();
Bitmap bMap = MediaStore.Video.Thumbnails.getThumbnail(cr, cursor.getLong(VideoListAdapter.VIDEO_CONTENT_ID), MediaStore.Video.Thumbnails.MINI_KIND, null);
if(bMap != null)
{
mThumbnail.setImageBitmap(Bitmap.createScaledBitmap(bMap, 128, 128, true));
}
else
{
mThumbnail.setImageDrawable(r.getDrawable(R.drawable.error));
}
mThumbnail.setPadding(16, 16, 16, 16);
mTitle.setText(cursor.getString(VideoListAdapter.VIDEO_CONTENT_TITLE));
mTitle.setSingleLine();
mTitle.setTextColor(Color.GREEN);
mResolution.setText(cursor.getString(VideoListAdapter.VIDEO_CONTENT_RESOLUTION));
mResolution.setSingleLine();
mResolution.setTextColor(Color.RED);
mDuration.setText(stringForTime(cursor.getInt(VideoListAdapter.VIDEO_CONTENT_DURATION)));
mDuration.setSingleLine();
mDuration.setTextColor(Color.CYAN);
mContentId = cursor.getLong(VideoListAdapter.VIDEO_CONTENT_ID);
}
public long getContentId()
{
return mContentId;
}
private StringBuilder mFormatBuilder = null;
private Formatter mFormatter = null;
private String stringForTime(int timeMs) {
int totalSeconds = timeMs / 1000;
mFormatBuilder = new StringBuilder();
mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
mFormatBuilder.setLength(0);
if (hours > 0) {
return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
} else {
return mFormatter.format("%02d:%02d", minutes, seconds).toString();
}
}
};
Shash
Don't allocate or store the Feed object in your View holder and instead only store the position (position). When you need to reference the object then grab the reference index from the ViewHolder and act accordingly.
Edit
Of course I missed that you're using the object later on... You might also create a number of minimal, static methods for your Feed object that only return specific things, such as the title, etc. Then call these methods in your getView method to set the UI elements without full creation of the Feed itself.
Related
I am working on Calendar App to display list of items selected by user. i have set adapter in fragment with 2 array_lists(one for image resources and other for items name) and size of list but rather than to display particular number of list items only first item is coming and get_View method is calling 10 times just for first list element and only one item comes to display.
code of ItemSelectedclass
package shopping.com.shopping.adapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import java.util.ArrayList;
import shopping.com.shopping.R;
public class ItemSelected extends BaseAdapter {
Context mContext;
int limit;
int count=0;
ScrollView sc_list;
private LayoutInflater inflater = null;
String item_name[]={"Egg","Bread","Milk","Watercan","Fruit","Egg","Bread"};
int imgsrc[]={R.drawable.smallegg,R.drawable.smallbread,R.drawable.smallmilk,R.drawable.smallwatert,R.drawable.smallapple,R.drawable.smallegg,R.drawable.smallbread};
ArrayList<String> listofIndexes,listofquantities;
ArrayList<String> location_or_society_details;
public ItemSelected(Context context ,ArrayList<String> listofIndexes,ArrayList<String> listofquantities,int limit) {
// "imgsrc" is the image-reference of selected item from the list
// "quantity" is the quantity of selected item which customer want to book
// "imgTitle" is the name of selected img like milk,bread,watertank
// "getimgSrc[]" is the array which contains refrences of all the items
// "get_title" is the array which contains all the items names
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mContext = context;
this.limit=limit;
this.listofIndexes=listofIndexes;
this.listofquantities=listofquantities;
}
#Override
public int getCount() {
return limit;
}
#Override
public Object getItem(int position) {
return this.listofIndexes.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
Log.d("testing","afzal");
count++;
TextView i_name=null,i_count=null;
ImageView i_image=null;
if(convertView == null) {
inflater = LayoutInflater.from(mContext);
view = inflater.inflate(R.layout.items_list, null);
}
i_image = (ImageView) view.findViewById(R.id.i_image);
i_name = (TextView) view.findViewById(R.id.i_name);
i_count = (TextView) view.findViewById(R.id.i_count);
i_name.setText("testing");
i_count.setText("count");
i_image.setImageResource(R.drawable.logo);
//setting respective value of Booked list_items with 'Item_name' and their 'Quantity'. suppose first user select item#5 with quantity 5 then item#2 with quatity 9 and so on, then first child of listView should b item#5 with quant 5 second list_item should be 2 with quantity 9 and so on
// String index= listofIndexes.get(position);
// String quantity=listofquantities.get(position);
// i_name.setText(item_name[Integer.parseInt(index)]);
// i_image.setImageResource(imgsrc[Integer.parseInt(index)]);
// i_count.setText(Integer.parseInt(quantity));
return view;
}
}
code for TabFragment1 class
package shopping.com.shopping.fragmensts;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Switch;
import android.widget.Toast;
import com.samsistemas.calendarview.widget.CalendarView;
import com.samsistemas.calendarview.widget.DayView;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import shopping.com.shopping.R;
import shopping.com.shopping.activities.ListItemsActivity;
import shopping.com.shopping.activities.SetOrder;
import shopping.com.shopping.activities.SignUpSignIn;
import shopping.com.shopping.adapter.ItemSelected;
import shopping.com.shopping.adapter.PagerAdapter;
public class TabFragment1 extends Fragment implements View.OnClickListener{
private CalendarView mCalendarView;
private View myFragmentView;
Button btn;
ItemSelected adapter;
ListView listview;
LinearLayout l_lay;
ScrollView scroll;
ArrayList<String> listofIndexes,listofquantities;
int iTem_Index=3;
String item_name[]={"Egg","Bread","Milk","Watercan","Fruit","Egg","Bread"};
int imgsrc[]={R.drawable.smallegg,R.drawable.smallbread,R.drawable.smallmilk,R.drawable.smallwatert,R.drawable.smallapple,R.drawable.smallegg,R.drawable.smallbread};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView= inflater.inflate(R.layout.fragment_tab_fragment1, container, false);
mCalendarView = (CalendarView) myFragmentView.findViewById(R.id.calendar_view);
btn= (Button) myFragmentView.findViewById(R.id.try_it_now);
l_lay = (LinearLayout) myFragmentView.findViewById(R.id.linear);
listview = (ListView) myFragmentView.findViewById(R.id.booked_item);
scroll=(ScrollView)myFragmentView.findViewById(R.id.scroll_list);
SharedPreferences sh_pref=getActivity().getSharedPreferences("backToHome", Context.MODE_PRIVATE);
int flag= sh_pref.getInt("flag", 0);
String item=sh_pref.getString("item","null");
int quantity=sh_pref.getInt("quantity",0);
//if user has booked something then listView in home page will be visible
if(flag==3){
int count=0;
try {
listofIndexes=new ArrayList<String>();
BufferedReader inputReader = new BufferedReader(new InputStreamReader(getActivity().openFileInput("ItemsBooked")));
String readItem;
StringBuffer stringBuffer = new StringBuffer();
while ((readItem = inputReader.readLine()) != null){
listofIndexes.add(readItem);
}
}
catch (IOException e) {
e.printStackTrace();
}
try {
listofquantities=new ArrayList<String>();
BufferedReader inputReader = new BufferedReader(new InputStreamReader(getActivity().openFileInput("Quantity")));
String readQuantity;
StringBuffer stringBuffer = new StringBuffer();
while ((readQuantity = inputReader.readLine()) != null) {
count++;
Toast.makeText(getActivity(), "count is .."+count, Toast.LENGTH_SHORT).show();
listofquantities.add(readQuantity);
}
}
catch (IOException e) {
e.printStackTrace();
}
l_lay.setVisibility(View.GONE);
scroll.setVisibility(View.VISIBLE);
listview.setVisibility(View.VISIBLE);
// after matching and verifying, adding "item" into the array list
adapter= new ItemSelected(getActivity(),listofIndexes,listofquantities,count);
listview.setAdapter(adapter);
}
btn.setOnClickListener(this);
//put intent
mCalendarView.setFirstDayOfWeek(Calendar.SUNDAY);
mCalendarView.setIsOverflowDateVisible(true);
mCalendarView.setCurrentDay(new Date(System.currentTimeMillis()));
mCalendarView.setNextButtonColor(R.color.colorAccent);
mCalendarView.refreshCalendar(Calendar.getInstance(Locale.getDefault()));
mCalendarView.setNextButtonColor(R.color.bg_for_selecte_dday);
mCalendarView.setBackButtonColor(R.color.bg_for_selecte_dday);
//get current date
Date date=new Date(System.currentTimeMillis());
mCalendarView.setCurrentDay(date);
mCalendarView.setOnDateSelectedListener(new CalendarView.OnDateSelectedListener() {
#Override
public void onDateSelected(#NonNull Date selectedDate) {
Toast.makeText(getActivity(), "second date is selected", Toast.LENGTH_SHORT).show();
mCalendarView.setSelectedDayBackground(getResources().getColor(R.color.white));
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
}
});
mCalendarView.setOnMonthChangedListener(new CalendarView.OnMonthChangedListener() {
#Override
public void onMonthChanged(#NonNull Date monthDate) {
SimpleDateFormat df = new SimpleDateFormat("dd MMMM yyyy", Locale.getDefault());
}
});
final DayView dayView = mCalendarView.findViewByDate(new Date(System.currentTimeMillis()));
return myFragmentView;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.try_it_now:
Intent i=new Intent(getActivity(),ListItemsActivity.class);
i.putExtra("flag", 3);
startActivity(i);
}
}
}
I am unable to comment due to low reputation. Kindly post the layout files. Also, instead of using two arraylists and a count variable, create a model class. It will make your code clean and easy to manage.
Refer point no. 12 and 13 from this link http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/
I have been searching about this issue but still I am not very clear on how to use a click listener with an array adapter.
I need a click listener for each item of the list.
Codes:
The Area item:
package fogames.tamagomonsters;
public class Area {
public String name;
public String number;
public Area(String name, String number) {
this.name = name;
this.number = number;
}
}
The array adapter:
package fogames.tamagomonsters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class AreasAdapter extends ArrayAdapter<Area> {
public AreasAdapter(Context context, ArrayList<Area> Areas) {
super(context, 0, Areas);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Area Area = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_area, parent, false);
}
// Lookup view for data population
TextView tvname = (TextView) convertView.findViewById(R.id.tvName_area);
TextView tvnumber = (TextView) convertView.findViewById(R.id.tvNumber_of_beasts);
// Populate the data into the template view using the data object
tvname.setText(Area.name);
tvnumber.setText(Area.number);
// Return the completed view to render on screen
return convertView;
}
}
The activity:
package fogames.tamagomonsters;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;
import java.util.ArrayList;
public class PlayMenuActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
FrameLayout fl = new FrameLayout(this);
fl.setBackgroundColor(Color.argb(255, 0, 0, 0));
// Construct the data source
ArrayList<Area> arrayOfAreas = new ArrayList<Area>();
// Create the adapter to convert the array to views
AreasAdapter adapter = new AreasAdapter(this, arrayOfAreas);
// Attach the adapter to a ListView
ListView lv = new ListView(this);
lv.setAdapter(adapter);
// Restore preferences
SharedPreferences prefs = getSharedPreferences(PreferenceConstants.PREFERENCE_NAME, MODE_PRIVATE);
int mlen = prefs.getInt(PreferenceConstants.MLEN, 0);
long money = prefs.getLong(PreferenceConstants.MONEY, 0);
int mall = 6; //hay que ver que hacer con esto...
int eqall = 3; //igual
boolean[] mgot = new boolean[mall];
int[] exp = new int[eqall];
int[] lvl = new int[eqall];
int[] at = new int[eqall];
int[] en = new int[eqall];
for (int i = 0; i < mall; i++) {
mgot[i] = prefs.getBoolean(PreferenceConstants.MGOT[i], false);
}
for (int i = 0; i < eqall; i++) {
exp[i] = prefs.getInt(PreferenceConstants.EXP[i], 0);
lvl[i] = prefs.getInt(PreferenceConstants.LVL[i], 0);
at[i] = prefs.getInt(PreferenceConstants.AT[i], 0);
en[i] = prefs.getInt(PreferenceConstants.EN[i], 0);
}
String name[] = {getString(R.string.a001)};
int prado_got = 0;
if (mgot[0]) {
prado_got += 1;
}
if (mgot[3]) {
prado_got += 1;
}
String prado = String.valueOf(prado_got) + " / 2";
String number[] = {prado};
// Add item to adapter
Area a001 = new Area(name[0], number[0]);
adapter.add(a001);
this.setContentView(fl);
}
}
Thank you in advance.
You can do something like that:
•First insert in your adapter this method
#Override
public Area getItem(int position) {
return [yourArrayAreas].get(position);
}
•Then, in your Activity...
final AreasAdapter adapter = new AreasAdapter(this, arrayOfAreas);
ListView lv = new ListView(this);
lv.setAdapter(adapter);
mListStopsMuni.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id{
Area yourClickArea = adapter.getItem(position);
//Rest of code...
}
});
Good luck ;)
Something like this:
ArrayList<Area> arrayListAreas;
public AreasAdapter(Context context, ArrayList<Area> Areas) {
super(context, 0, Areas);
this.arrayListAreas = Areas;
}
//And then....
#Override
public Area getItem(int position) {
return arrayListAreas.get(position);
}
I have a listview in which i load data with cursor adapter and content provider, the data is loading and scrolling correctly except of background, the view background is not able to save its state....so i had tried my every approach to save the background of view but i failed...any help would be greatly appreciated.
package papuu.items;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import papu.R;
import papu.db.Order;
public class CustomCursorAdapter extends CursorAdapter {
public CustomCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View vView = LayoutInflater.from(context).inflate(R.layout.listview_item_row, parent, false);
vView.setTag(new ViewHolder(vView));
// no need to bind data here. you do in later
return vView;// **EDITED:**need to return the view
}
// The bindView method is used to bind all data to a given view
// such as setting the text on a TextView.
#Override
public void bindView(View row, Context context, Cursor cursor) {
Order cPerson = new Order(cursor);
//here the row background is not able to save its background state, as i scroll the row background gets shuffled
if (cPerson.status == 3)
row.setBackgroundColor(Color.parseColor("#8FCC85")); //Green Accepted
else if (cPerson.status == 0)
row.setBackgroundColor(Color.parseColor("#B0BEC5")); //gray Picked Up
ViewHolder vh = (ViewHolder) row.getTag();
vh.restaurentName.setText(cPerson.restaurantName);
vh.address.setText(cPerson.customerAddress);
DateFormat df = new SimpleDateFormat("yyyy'-'mm'-'dd'T'hh':'mm':'SS'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date;
int mins = 0;
int hrs = 0;
try {
date = df.parse(cPerson.pickupTime);
hrs = (int) ((date.getTime() / (1000 * 60 * 60)) % 24);
mins = (int) ((date.getTime() / (1000 * 60)) % 60);
} catch (Exception ex) {
ex.printStackTrace();
}
// Log.e(cPerson.status + "",mins+"");
vh.pickupTime.setText(hrs + ":" + mins);
vh.price.setText("Tk. " + cPerson.status);
}
public class ViewHolder {
TextView restaurentName;
TextView address;
TextView pickupTime;
TextView price;
ViewHolder(View row) {
restaurentName = (TextView) row.findViewById(R.id.tv_restaurent_name);
address = (TextView) row.findViewById(R.id.tv_address);
pickupTime = (TextView) row.findViewById(R.id.tv_pickuptime);
price = (TextView) row.findViewById(R.id.tv_price);
}
}
}
After I create or receive a message send the message then save it to realm. Afterwards I need to update my threads ListView on the threads page and bring the newest messages to the top. I already have it so the thread list shows the updated preview and updated date, but it stays in it's inital ListView position. I tried to requery realm to get all info and reorder by the lastUpdated time, but it doesn't seem to work. Do I need to wipe the old thread list then repopulate it to get it to update?
I have the update triggered on the onResume()
#Override
protected void onResume() {
super.onResume();
updateListview = true;
updateList();
}
Here's my update
#UiThread
public void updateList() {
try {
if (updateListview) {
thread_realm = Realm.getInstance(this);
results = thread_realm.where(ZipListModel.class).findAllSorted("zipupdated", RealmResults.SORT_ORDER_DESCENDING);
adapter = new ZipListAdapter(this, results);
threadsListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
if (results.size()==0){
createZipHint.setVisibility(View.VISIBLE);
} else {
createZipHint.setVisibility(View.INVISIBLE);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Adapter
package com.admin.zipline.adapters;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.admin.zipline.R;
import com.admin.zipline.activities.ZipListPage;
import com.admin.zipline.model.ZipListModel;
import org.androidannotations.annotations.ViewById;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ZipListAdapter extends ArrayAdapter<ZipListModel>
{
List<ZipListModel> items;
Context context;
Typeface semiBold;
Typeface light;
Typeface regular;
String[] months={};
public ZipListModel ziplist;
ArrayList<String> ziplistNames,ziplistParticipantsaids;
public ZipListAdapter(Context context, List<ZipListModel> threadslist) {
super(context,R.layout.zip_adapter_view,threadslist);
this.context = context;
this.items=threadslist;
}
#Override
public View getView(final int position, View view, ViewGroup parent) {
ViewHolder holder ;
if (view == null) {
holder =new ViewHolder();
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.zip_adapter_view, parent, false);
light = Typeface.createFromAsset(context.getAssets(), "ProximaNova-Light.otf");
regular = Typeface.createFromAsset(context.getAssets(), "ProximaNova-Regular.otf");
semiBold = Typeface.createFromAsset(context.getAssets(),"ProximaNova-Semibold.otf");
//TODO
/*for showing the author image
* */
//holder.thread_image = (ImageView)view.findViewById(R.id.author_avatar);
holder.thread_text = (TextView) view.findViewById(R.id.threadtext);
holder.thread_name = (TextView) view.findViewById(R.id.threadname);
holder.last_updated = (TextView) view.findViewById(R.id.lastupdated);
holder.zip_members=(TextView)view.findViewById(R.id.ziplist_members);
holder.thread_text.setTypeface(light);
holder.thread_name.setTypeface(semiBold);
holder.zip_members.setTypeface(regular);
view.setTag(holder);
}else{
holder =(ViewHolder)view.getTag();
}
try{
ziplist = items.get(position);
ziplistNames = new ArrayList<String>();
ziplistParticipantsaids=new ArrayList<>();
if (ziplist != null) {
if (ziplist.getMessagesListsmodel().first().getText()!=null){
holder.thread_text.setText(ziplist.getMessagesListsmodel().first().getText());
}
if (ziplist.getMessagesListsmodel().first().getCreatedAt()!=null){
holder.last_updated.setText(getDate(ziplist.getMessagesListsmodel().first().getCreatedAt()));
}
for (int i = 0; i < ziplist.getParticipantsmodel().size(); i++) {
ziplistNames.add(ziplist.getParticipantsmodel().get(i).getName());
ziplistParticipantsaids.add(ziplist.getParticipantsmodel().get(i).getParticipantId());
}
String members="";
for (int i=0;i<ziplistNames.size();i++){
members+=ziplist.getParticipantsmodel().get(i).getFirstName()+", ";
}
if (members.length() > 3){
members=members.substring(0,members.length()-2);
}
holder.zip_members.setText(members);
if(ziplist.getZipname().isEmpty()){
holder.thread_name.setText(members);
} else {
holder.thread_name.setText(ziplist.getZipname());
}
}
}
catch (Exception e){
e.printStackTrace();
}
view.setBackgroundColor(Color.parseColor(ziplist.getZipColor()));
return view;
}
String getDate(Date date) {
try {
Date d = date;
months=context.getResources().getStringArray(R.array.months);
return months[d.getMonth()] + " " + (d.getDate());
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
public class ViewHolder{
ImageView thread_image;
TextView thread_text,thread_name,last_updated,zip_members;
// CardView cardView;
}
}
The question is not quite clear to me but i can suggest some common ways to do this.
Assume your ZipListModel defined like this:
public class ZipListModel extends RealmObject {
private String title;
private Date date;
private String Author;
... getters and setters...
}
The easiest way to show your models in the ListView is using RealmBaseAdapter. You can find document here. And example.
eg.:
public class ZipListAdapter extends RealmBaseAdapter<ZipListModel> implements ListAdapter {
public ZipListAdapter(Context context, int resId,
RealmResults<ZipListModel> realmResults,
boolean automaticUpdate) {
super(context, realmResults, automaticUpdate);
}
...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Update your views with the realmResults
...
ZipListModel zlm = realmResults.get(position);
authorTextView.setText(zlm.getAuthor);
...
}
...
}
The benifit of using RealmBaseAdapter is the realmResults can be updated automatically when the Realm transaction commited which means in your case, whenever you update your Realm by
thread_realm = Realm.getInstance(this);
thread_realm.beginTransaction()
// Change something in your Realm
...
thread_realm.commitTransaction()
The ZipListAdapter's notifyDataSetChanged will be called automatically and the listView will be updated.
BTW, you can still stay with your own adpater and simply update it by using RealmChangeListener. See example .
Try use RealmBaseAdapter.UpdateData(RealmResult<T> result). My project has similar requirement and it works for me
https://realm.io/docs/java/latest/api/io/realm/RealmBaseAdapter.html
Helo. I'm experiencing strange behavior with my app. It crashes on litsview item flip animation with this error:
06-02 17:26:16.748 22310-23383/com.dealy.android A/OpenGLRenderer﹕ Error: Ambient Vertex Buffer overflow!!! used 286, total 284
I don't use any of OpenGL libs or smthing, so it's strange. Also, crash only occurs on Nexus 6 with android 5.1.
The code of list item here:
package com.dealy.android.adapter;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.annotation.TargetApi;
import android.content.Context;
import android.location.Location;
import android.os.Build;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.squareup.picasso.Picasso;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.InjectView;
import database.HelperFactory;
import com.dealy.android.DealyApplication;
import com.dealy.android.R;
import com.dealy.android.util.SimpleAnimationEndListener;
import dto.GeoPoint;
import dto.ImageDTO;
import special.dto.Special;
import venue.dto.Venue;
/**
* Created by Beyka on 13.01.2015.
*/
public class SpecialAdapter extends BaseAdapter {
private static final DecimalFormat DEFAULT_DISTANCE_FORMAT = new DecimalFormat("##0.0");
private final AnimatorSet fbo;
private final AnimatorSet fbi;
private Context context;
private LayoutInflater inflater;
private List<Special> values;
private SparseBooleanArray flipArray;
private GeoPoint lastLocation;
Tracker t;
public SpecialAdapter(Context context, List<Special> values, GeoPoint lastLocation) {
fbo = (AnimatorSet) AnimatorInflater.loadAnimator(context, R.animator.card_flip_right_out);
fbi = (AnimatorSet)AnimatorInflater.loadAnimator(context, R.animator.card_flip_right_in);
this.context = context;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.values = values;
flipArray = new SparseBooleanArray();
this.lastLocation = lastLocation;
}
public void setLastLocation(GeoPoint lastLocation) {
this.lastLocation = lastLocation;
notifyDataSetChanged();
}
#Override
public int getCount() {
return values.size();
}
#Override
public Special getItem(int position) {
return values.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_special, parent, false);
holder = new Holder(convertView);
convertView.setTag(holder);
} else{
holder = (Holder)convertView.getTag();
}
Special special = getItem(position);
fillHolder(holder, special, position);
return convertView;
}
private void fillHolder(final Holder holder, Special special, final int position) {
holder.front.setVisibility(flipArray.get(position, false) ? View.GONE : View.VISIBLE);
holder.back.setVisibility(!flipArray.get(position, false) ? View.GONE : View.VISIBLE);
t = ((DealyApplication) context.getApplicationContext()).getTracker(DealyApplication.TrackerName.APP_TRACKER);
holder.more.setOnClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public void onClick(View v) {
t.send(new HitBuilders.EventBuilder().setCategory("Test Catogory").setAction("Нажатие на подробности").build());
fbi.setTarget(holder.back);
fbo.addListener(new SimpleAnimationEndListener() {
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public void onAnimationEnd(Animator animation) {
flipArray.put(position, true);
holder.front.setVisibility(View.GONE);
holder.back.setVisibility(View.VISIBLE);
fbi.start();
}
});
fbo.setTarget(holder.front);
fbo.start();
}
});
holder.goBack.setOnClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public void onClick(View v) {
t.send(new HitBuilders.EventBuilder().setCategory("Test Catogory").setAction("Нажатие на вернуться").build());
fbi.setTarget(holder.front);
fbo.addListener(new SimpleAnimationEndListener() {
#Override
public void onAnimationEnd(Animator animation) {
flipArray.put(position, false);
holder.back.setVisibility(View.GONE);
holder.front.setVisibility(View.VISIBLE);
fbi.start();
}
});
fbo.setTarget(holder.back);
fbo.start();
}
});
holder.backTitle.setText(special.getName());
holder.description.setText(special.getDescription());
if(special.getImageLink() == "" || special.getImageLink() == null){
for(ImageDTO image : special.getImages()){
if(image.getWidth() >= 640){
Picasso.with(context).load("http://app.dev.getdealy.ru/images/" + image.getUrl()).fit().into(holder.logo);;
}
}
}
else {
Picasso.with(context).load("http://app.dev.getdealy.ru/images/" + special.getImageLink()).fit().into(holder.logo);
}
Venue venue = new Venue();
try {
venue = HelperFactory.getHelper().getVenueDAO().getVenueById(special.getVenues().get(0));
} catch (SQLException e) {
e.printStackTrace();
}
final float[] distanceInMeters = new float[1];
try {
Location.distanceBetween(lastLocation.getLat(), lastLocation.getLng(), special.venue.getLat(), special.venue.getLng(), distanceInMeters);
} catch(NullPointerException e){
}
double distanceInKm = distanceInMeters[0] / 1000.0;
holder.distance.setText(context.getString(R.string.kilometers, DEFAULT_DISTANCE_FORMAT.format(distanceInKm)));
holder.backDistance.setText(context.getString(R.string.kilometers, DEFAULT_DISTANCE_FORMAT.format(distanceInKm)));
if(distanceInKm == 0){
holder.distance.setVisibility(TextView.GONE);
holder.backDistance.setVisibility(TextView.GONE);
}
if(special.venue.getLogoLink() != null) {
Picasso.with(context).load("http://app.dev.getdealy.ru/images/" + special.venue.getLogoLink()).fit().into(holder.place);
Picasso.with(context).load("http://app.dev.getdealy.ru/images/" + special.venue.getLogoLink()).fit().into(holder.backPlace);
}
else{
Picasso.with(context).load("http://app.dev.getdealy.ru/images/" + special.venue.getImages().get(0)).fit().into(holder.place);
Picasso.with(context).load("http://app.dev.getdealy.ru/images/" + special.venue.getImages().get(0)).fit().into(holder.backPlace);
}
}
class Holder {
#InjectView(R.id.front)
View front;
#InjectView(R.id.logoImage)
ImageView logo;
#InjectView(R.id.place)
ImageView place;
#InjectView(R.id.distanceText)
TextView distance;
#InjectView(R.id.more)
Button more;
#InjectView(R.id.back)
View back;
#InjectView(R.id.goBack)
Button goBack;
#InjectView(R.id.backTitle)
TextView backTitle;
#InjectView(R.id.backPlace)
ImageView backPlace;
#InjectView(R.id.backDistance)
TextView backDistance;
#InjectView(R.id.description)
TextView description;
#InjectView(R.id.card)
RelativeLayout card;
private Holder(View view) {
ButterKnife.inject(this, view);
}
}
}
i have resolve this, the reason is : you had open the hardwareAccelerated attribute , you must close it in your manifest.xml like this
android:hardwareAccelerated = "false"
......
>
This problem is caused by more than 6.0 of Android's system automatically open the default hardware acceleration, but sometime we had no need to used it, Because most of the hardware capacity is still insufficient.
hope can help you!
#high sea company `s answer is right.
Thank you!!!
You can try like this:
in AndroidManifest.xml application:
<application android:hardwareAccelerated="false" ...>
in Activity :
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
In layout XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="2dp"
android:layerType="software" //**close hardwareAccelerated**
android:paddingRight="2dp" >
in View:
myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);