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);
}
}
}
Related
I'm having trouble displaying the right String from an ArrayList in my ListView.
My Array (m_aDataList) looks like this:
m_aDataList {ArrayList}
{...}0
m_cText = "R;21;9;River Street 2;12154;;1;.......
m_cTimeStamp = 1556553367492
m_nID = 7
m_nStatus = 0
m_nType = 10002
{...}1
....
This is how I'm currently trying to Display the ArrayList in my ListView:
ArrayAdapter<Message> tuAdapter;
MessageManager tu = new MessageManager();
ArrayList<Message> list = tu.getMessageData();
tuAdapter = new ArrayAdapter<Message>(this, android.R.layout.simple_list_item_1, list);
lvOrders.setAdapter(tuAdapter);
It technically works and adds something like this to my ListView:
de.telematik.testapp.entities.Message#232d9c76
But what I'm trying to do is, only show Messages where m_nType == 10002 and Display them like this in my ListView:
Order: 12154 (order number from m_cText)
Checking weither or not m_nType is = 10002 or 10000 shouldn't be the problem. But how do I get the order number out of the String m_cText and then display it in my ListView?
In any case, thanks for your help.
You are using a basic default list adapter, but you are passing it an array of objects (Message). In order to control what displays in the list, you will need to create your own custom adapter class, that extends (probably base adapter) one of the standard adapter classes. You will call it similarly to the way you pass your list to your simple list adapter. Here is a sample custom list adapter:
ClaimListAdapter.java
package com.mycompany.myapp.adapter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import com.mycompany.myapp.R;
import com.mycompany.myapp.ClaimListFragment;
import com.mycompany.myapp.model.ClaimItem;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class ClaimListAdapter extends BaseAdapter {
private Context context;
private ArrayList<ClaimItem> claimItems;
ClaimListFragment fragment;
public ClaimListAdapter(ClaimListFragment fragment, Context context, ArrayList<ClaimItem> claimItems){
this.context = context;
this.claimItems = claimItems;
this.fragment = fragment;
}
#Override
public int getCount() {
return claimItems.size();
}
#Override
public Object getItem(int position) {
return claimItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("InflateParams")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// This method gets called for every item in the list when it is about to be displayed in the list
// This helps you get a reference to the layout of the list item
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.claim_list_item, null);
}
// If you have a button that you want to perform some function, such as delete, and then call
// a method back in the parent to update the items in the array, this is one way to do it
Button btnDelete = (Button) convertView.findViewById(R.id.claim_delete_in_list);
// Save the position of the item in the list, so, you know which item was clicked
btnDelete.setTag(position);
btnDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Integer position = (Integer)v.getTag();
// Call this method which is back in the parent, which in this case is a fragment, but could be an Activity
fragment.deleteItemList(position);
}
});
btnDelete.setVisibility(View.GONE);
// Here you get a reference to the various TextViews in your layout
TextView txtTitle = (TextView) convertView.findViewById(R.id.claim_title);
TextView txtStatus = (TextView) convertView.findViewById(R.id.claim_status);
TextView txtDate = (TextView) convertView.findViewById(R.id.claim_date);
TextView txtDistance = (TextView) convertView.findViewById(R.id.claim_distance);
TextView txtAmount = (TextView) convertView.findViewById(R.id.claim_amount);
// Here you get the various strings out of your object for display
String claim_title = claimItems.get(position).getDocumentID();
if (claim_title == null) {
claim_title = "";
}
String claim_status = claimItems.get(position).getClaimStatus();
String claim_date = claimItems.get(position).getClaimDate();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = null;
try {
date = (Date) dateFormat.parse(claim_date);
dateFormat = new SimpleDateFormat("M/d/yyyy");
String formattedDate = dateFormat.format(date);
txtDate.setText(formattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
double total_miles = claimItems.get(position).getTotalMiles();
double total_amount = claimItems.get(position).getTotalAmount();
// Here you set the values in the TextView for display
txtTitle.setText(claim_title);
txtStatus.setText(claim_status);
txtDistance.setText("" + total_miles + "mi");
txtAmount.setText("$" + total_amount);
return convertView;
}
}
Alternatively, you could create a string array containing only the order numbers from your array of messages, and pass that array to your list adapter instead of the array of messages.
I have an application that uses sqlite, it stores information about hardware store items and displays them in a ListView, this list view shows the name of the item, the price, the quantity, and the supplier. And each list item also has a Sell button and when I click the button it is supposed to subtract 1 from that specific item's quantity and update the database, but since the button is created in the CursorAdapter Im not sure how to access the database and update it.
This is my CursorAdapter:
package com.example.android.inventoryapp;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.android.inventoryapp.data.InventoryContract.InventoryEntry;
public class InventoryCursorAdapter extends CursorAdapter {
public InventoryCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView itemNameView = view.findViewById(R.id.item_name);
TextView itemPriceView = view.findViewById(R.id.item_price);
TextView itemQuantityView = view.findViewById(R.id.item_quantity);
TextView itemSupplierView = view.findViewById(R.id.item_supplier);
ImageView sellButton = view.findViewById(R.id.sell_button);
int nameColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_ITEM_NAME);
int priceColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_ITEM_PRICE);
int quantityColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_ITEM_QUANTITY);
int supplierColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_ITEM_SUPPLIER);
int quantity = cursor.getInt(quantityColumnIndex);
String name = cursor.getString(nameColumnIndex);
String price = String.valueOf(cursor.getInt(priceColumnIndex)) + context.getString(R.string.currency_symbol);
String quantityStr = String.valueOf(quantity);
String supplier = cursor.getString(supplierColumnIndex);
itemNameView.setText(name);
itemPriceView.setText(price);
itemQuantityView.setText(quantityStr);
itemSupplierView.setText(supplier);
}
}
In your activity that holds the adapter reference, create an inner class something like:
public class MyClickListener {
public void handleClick(Item item) {
// access your DB here, {item} is available if you need the data
}
}
then when you create your adapter
myAdapter = new InventoryCursorAdapter(context, cursor, new MyClickListener());
save the reference to that click listener in your adapter.
then in the adapter's BindView method (if you need the item data to update the database, pass it through the click listener)
sellButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Item item = myItemSet.get(position);
myListener.handleClick(item);
}
});
I am working on an Android application and I am trying to make the item click on a listView with custom adapter to work but am not being able to. I have my OnItemClickListener implemented inside the customer adapter.
Would you know what I can be doing wrong? ListView loads with content correctly, only point is that it does not clicks.
This is my listView definition and adapter setting:
public void updateUserAssetBookingsListView(final ArrayList<AssetBooking> userAssetBookings) {
System.out.println("Bookings updated, new total is: " + userAssetBookings.size());
BookingAdapter bookingAdapter = new BookingAdapter(getContext(), 0, userAssetBookings);
userAssetBookingsListView.setAdapter(bookingAdapter);
userAssetBookingsListView.setOnItemClickListener(bookingAdapter);
}
This is my custom adapter:
package it.bitrack.adapter;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.Toast;
import org.w3c.dom.Text;
import it.bitrack.fabio.bitrack.R;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import it.bitrack.support.Epoch;
import it.bitrack.support.AssetBooking;
/**
* Created by fabio on 25/04/2017.
*/
public class BookingAdapter extends ArrayAdapter<AssetBooking> implements AdapterView.OnItemClickListener {
ArrayList<AssetBooking> userAssetBookings;
Epoch epoch = new Epoch();
public BookingAdapter(#NonNull Context context, #LayoutRes int resource, ArrayList<AssetBooking> userAssetBookings) {
super(context, resource, userAssetBookings);
this.userAssetBookings = userAssetBookings;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Get the data item for this position
AssetBooking ab = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.booking_listview_layout, parent, false);
}
// Lookup view for data population
TextView assetTextView = (TextView) convertView.findViewById(R.id.assetTextView);
TextView fromTextView = (TextView) convertView.findViewById(R.id.fromTextView);
TextView toTextView = (TextView) convertView.findViewById(R.id.toTextView);
TextView durationTextView = (TextView) convertView.findViewById(R.id.durationTextView);
ImageButton cancelImageButton = (ImageButton) convertView.findViewById(R.id.cancelImageButton);
cancelImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "You tried to delete this row " + position, Toast.LENGTH_SHORT).show();
}
});
// Populate the data into the template view using the data object
// AssetBooking ab = userAssetBookings.get(position);
long from = ab.getFromDatetime() / 1000;
long to = ab.getToDatetime() / 1000;
long delta = (to - from);
long deltaDays = delta / 86400;
long deltaMinutes = ((delta % 86400) / 60) % 60;
long deltaHours = (delta % 86400) / 3600;
assetTextView.setText(ab.getNetworkAssetCode() + ": " + ab.getAssetDescription());
fromTextView.setText(epoch.getDatetimeFromTimestampNoSeconds(from));
toTextView.setText(epoch.getDatetimeFromTimestampNoSeconds(to));
durationTextView.setText(deltaDays + " day(s) " + deltaHours + " hour(s) " + deltaMinutes + " min(s)");
// Return the completed view to render on screen
return convertView;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
// set title
alertDialogBuilder.setTitle("Bookings details");
// set dialog message
alertDialogBuilder
.setMessage("Booker name: " + userAssetBookings.get(position).getUserName() + " " + userAssetBookings.get(position).getUserLastName() +
"\nBooker e-mail address: " + userAssetBookings.get(position).getUserEmail() +
"\nBooking date: " + epoch.getDatetimeFromTimestampNoSeconds(userAssetBookings.get(position).getCreatedOn() / 1000))
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
Couple of things in your booking_listview_layout.xml
Add android:descendantFocusability="blocksDescendants" to the root layout.
Also if you add android:clickable="true" in your layout remove it and also add android:focusable="false". ---if the first case not worked.
Second approach
Add a clickListener to the view object inside getView() method..it will call upon entire row.
Code snippet:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.booking_listview_layout, parent, false);
}
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
new AlertDialog.Builder(context).setTitle("touched").show();
}
});
return convertView;
}
Inside the 'getView()' method try putting an OnClickListener on the convertview in the following way:
convertview.setOnClickListener(new OnClickListener(){
...............YOUR CODE HERE.............
})
see if this works and dont implement onitemclicklistener
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 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.