Listview custom adapter with imageview and buttons - java

I tried to make a custom array adapter for my listview but the emulator crashes always and I can't see where my code is good and where it isn't.
The logcat says "java.lang.OutOfMemoryError" but I don't know how to solve. I tried to modify the studio.exe.vmoptions file or removing images (even if they are only 50x50) from the whole app, without any effort.
So I'll post my code, asking your help for making order in my app.
Thank you in advance!
public class MyClassAdapter extends ArrayAdapter<Plate> {
private static class ViewHolder {
TextView Id;
ImageView Image;
TextView Name;
TextView Description;
TextView Type;
TextView Cost;
TextView Count;
TextView Comment;
Button Buttonup;
Button Buttondown;
}
public MyClassAdapter(Context context, int textViewResourceId, ArrayList<Plate> items) {
super(context, textViewResourceId, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Plate item = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(this.getContext())
.inflate(R.layout.item_main, parent, false);
viewHolder = new ViewHolder();
viewHolder.Id = (TextView)convertView.findViewById(R.id.code);
viewHolder.Image = (ImageView) convertView.findViewById(R.id.image);
viewHolder.Name = (TextView) convertView.findViewById(R.id.name);
viewHolder.Description = (TextView) convertView.findViewById(R.id.description);
viewHolder.Cost = (TextView) convertView.findViewById(R.id.price);
viewHolder.Count = (TextView) convertView.findViewById(R.id.count);
viewHolder.Buttonup = (Button) convertView.findViewById(R.id.button_up);
viewHolder.Buttondown = (Button) convertView.findViewById(R.id.button_down);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (item!= null) {
viewHolder.Id.setText(String.format("%d",item.getId()));
viewHolder.Image.setImageURI(item.getImage());
viewHolder.Name.setText(String.format("%s", item.getName()));
viewHolder.Description.setText(String.format("%s", item.getDescription()));
viewHolder.Name.setText(String.format("%s", item.getName()));
viewHolder.Cost.setText(String.format("%s", item.getCost()));
viewHolder.Count.setText(String.format("%d", item.getCount()));
viewHolder.Buttonup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DBHelper mydb= new DBHelper(getContext());
mydb.AddPlate(item.getId());
item.CountUp();
//update viewholder.Count
}
});
viewHolder.Buttondown.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
DBHelper mydb= new DBHelper(getContext());
mydb.RemovePlate(item.getId());
item.CountDown();
//update viewholder.Count
}
});
}
return convertView;
}
And here's a snippet of my code that calls the custom ArrayAdapter
ArrayList<Plate> FullMenu;
FullMenu = mydb.getPlates("Entrees");
Plate p;
int i;
MyClassAdapter adapter = new MyClassAdapter(this,0,FullMenu);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
for (i=0; i < FullMenu.size(); i++) {
p = FullMenu.get(i);
adapter.add(p);
}

move the setadapter
ArrayList<Plate> FullMenu;
FullMenu = mydb.getPlates("Entrees");
Plate p;
int i;
MyClassAdapter adapter = new MyClassAdapter(this,0,FullMenu);
ListView listView = (ListView) findViewById(R.id.list);
for (i=0; i < FullMenu.size(); i++) {
p = FullMenu.get(i);
adapter.add(p);
}
listView.setAdapter(adapter);
also i dont think you need this, since you should read the data from the adapter itself
for (i=0; i < FullMenu.size(); i++) {
p = FullMenu.get(i);
adapter.add(p);
}
you can check this sample i have here for an adapter
https://github.com/juangdiaz/CoffeeApp/blob/master/app/src/main/java/com/juangdiaz/coffeeapp/adapter/ListAdapter.java
and calling the adapter here
https://github.com/juangdiaz/CoffeeApp/blob/master/app/src/main/java/com/juangdiaz/coffeeapp/fragments/CoffeeListFragment.java
let me know if this helps

You should be resize dynamically your image, so you avoid a failure of memory.
You can read this lesson
And here you can see a simple example of its operation
Good luck!

Related

How to add items dynamically to a custom adapter for ListView

I made adapter for ListView. If I fill the elements array before creating the adapter and linking it to the listView, the elements are displayed.
But if I use the updateItems () method to add items when the button is clicked, nothing happens.
Code of adapter:
public class ListAdapter extends ArrayAdapter<Lf> {
private LayoutInflater inflater;
private int layout;
private List<Lf> lfs;
public ListAdapter(Context context, int resource, List<Lf> lfs) {
super(context, resource, lfs);
this.lfs = lfs;
this.layout = resource;
this.inflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView==null){
convertView = inflater.inflate(this.layout, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) convertView.getTag();
}
Lf lf = lfs.get(position);
viewHolder.name.setText(lf.getLf());
viewHolder.freq.setText((int)lf.getFreq() + "");
return convertView;
}
public void updateItems(List<Lf> lfs) {
this.lfs.clear();
this.lfs.addAll(lfs);
this.notifyDataSetChanged();
}
private class ViewHolder {
final TextView name, freq;
ViewHolder(View view){
name = (TextView) view.findViewById(R.id.text_item_1);
freq = (TextView) view.findViewById(R.id.text_item_2);
}
}
}
Code of MainActivity:
public class MainActivity extends AppCompatActivity {
EditText editText1;
ListView listView;
ListAdapter adapter;
List<Lf> elements = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.edit_text);
listView = (ListView) findViewById(R.id.fragment_list);
// Working... Elements print on screen
for(int i = 0; i < 10; i++) {
Lf temp = new Lf();
temp.setLf("mean");
temp.setFreq(100);
elements.add(temp);
}
adapter = new ListAdapter(this, R.layout.list_item, elements);
listView.setAdapter(adapter);
}
public void activity_button(View view) {
adapter.updateItems(elements);
}
}
If I click on the button, the existing items on the screen are cleared instead of a new one added. But in debug I see that elements normally passed to ListAdapter.
The problem is that your adapter's lfs's field and your activity's elements field both refer to the same List instance. This happens because you pass elements to the ListAdapter constructor, and then simply assign this.lfs = lfs.
So let's look at what happens when you pass elements to updateItems()...
public void updateItems(List<Lf> lfs) {
this.lfs.clear(); // this.lfs and the input lfs are the same list, so this clears both
this.lfs.addAll(lfs); // input lfs is now empty, so addAll() does nothing
this.notifyDataSetChanged();
}
Probably the best thing to do is to create a copy of the list in your adapter's constructor.
this.lfs = new ArrayList<>(lfs);
Now your adapter and activity will reference different lists, so this.lfs.clear() won't accidentally clear out the very list you're passing to it.

How to update variable in OnStart() after notificationdatasetchanged() call in customadapter

What is want is to update total number in textview in one activity. i have a custom adapter which calls Arraylist in this activity then populates in listview, this adapter also has image view which removes the list item and does notifydatasetchanged().
this is my customadapter
private ArrayList<DataModel> dataSet;
Context mContext;
private static class ViewHolder {
TextView txtName;
TextView txtType;
Button remove;
}
public CustomAdapterForData(ArrayList<DataModel> data, Context context) {
super(context, R.layout.fields, data);
this.dataSet = data;
this.mContext=context;
}
#Override
public void onClick(View view) {
int position = (Integer) view.getTag();
Object object = getItem(position);
DataModel dataModel = (DataModel) object;
switch (view.getId())
{
case R.id.btnRemove:
remove(dataModel);
// dataSet.remove(position);
//dataSet.remove(position);
notifyDataSetChanged();
break;
}
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
DataModel dataModel = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.fields, parent, false);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.fieldName);
viewHolder.txtType = (TextView) convertView.findViewById(R.id.tvPrize);
viewHolder.remove = (Button) convertView.findViewById(R.id.btnRemove);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
viewHolder.txtName.setText(dataModel.getFieldName());
viewHolder.txtType.setText(dataModel.getType());
viewHolder.remove.setOnClickListener(this);
viewHolder.remove.setTag(position);
return convertView;
}
after that this is my onstart()
protected void onStart() {
//if ((dataModels.size()!=0)){
ListView listView = (ListView) findViewById(R.id.listView);
adapter = new CustomAdapterForData(dataModels, getApplicationContext());
listView.setAdapter(adapter);
dataModelsnew = dataModels;
TextView tv = (TextView) findViewById(R.id.textViewTotal);
double sum = 0;
for (int i = 0 ; i < dataModelsnew.size(); i++){
sum = sum + dataModelsnew.get(i).getPrize();
}
tv.setText("Total : " + String.valueOf(Math.round(sum)*100d/100d));
//}
super.onStart();
}
what i want is populate latest value in textview which is sum of double (one element of ArrayList).Please help me to resolve this.
Thanks in advance. The variable i want to update after i remove something from list view otherwise it is working fine.
You can use DataSetObservers to detect any changes in your data:
final TextView tv = (TextView) findViewById(R.id.textViewTotal);
adapter.registerDataSetObserver(new DataSetObserver() {
#Override
public void onChanged() {
super.onChanged();
double sum = 0;
for (int i = 0 ; i < dataModels.size(); i++){
sum = sum + dataModels.get(i).getPrize();
}
tv.setText("Total : " + String.valueOf(Math.round(sum)*100d/100d));
}
});

I've made my own ListView Adapter but no data is printed

This is what i have tried, but my ListView doesn't get filled. I use a custom adapter because i wan't to change the background color of the items that't got a boolean value = true. I am using Android Studio.
Hope someone can help.
i'm new to android.
public class ShoppingListActivity extends ActionBarActivity {
private TextView header;
private ListView listView;
private CustomArrayAdapter arrayAdapter;
private ShoppingList shoppingList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_list);
header = (TextView)findViewById(R.id.textView2);
//get reference to ListView
listView = (ListView)findViewById(R.id.shoppingListItemsView);
shoppingList = (ShoppingList) getIntent().getSerializableExtra(Globals.CHOSENLISTKEY);
arrayAdapter = new CustomArrayAdapter(this, R.layout.custom_list_view, shoppingList.getItemList());
listView.setAdapter(arrayAdapter);
header.setText(shoppingList.toString());
Button btnShop = (Button) findViewById(R.id.btnShop);
btnShop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ShoppingListActivity.this, SelectStoreActivity.class);
startActivity(intent);
}
});
}
public void setData() {
for(int i = 0; i < shoppingList.getItemList().size(); i++) {
arrayAdapter.add(shoppingList.getItemList().get(i));
}
}
}
public class CustomArrayAdapter extends ArrayAdapter<Entry> {
private int resource;
private ArrayList<Entry> list;
public CustomArrayAdapter(Context context, int resource, ArrayList<Entry> list) {
super(context, resource);
this.resource = resource;
this.list = list;
}
#Override
public View getView(int position, View convertView, ViewGroup parentGroup) {
View view = convertView;
Entry entry = list.get((position));
if(view == null) {
LayoutInflater layoutInflater = ((Activity) getContext()).getLayoutInflater();
view = layoutInflater.inflate(resource, parentGroup, false);
}
TextView textView = (TextView) view.findViewById(R.id.customName);
if(entry.getItem().isBought()) {
textView.setBackgroundColor(Color.BLUE);
}
return view;
}
}
To show data in ListView rows call setText method of TextView which is return from getView method:
TextView textView = (TextView) view.findViewById(R.id.customName);
// get Text from entry and pass it to setText method
String strTextViewData=entry.getItem()...;
textView.setText(strTextViewData);
if(entry.getItem().isBought()) {
textView.setBackgroundColor(Color.BLUE);
}

Why conditions from Adapters getView(..) method doesn`t apply correctly to ListView

I have a ListView in which I display some products. I use an object that extends the BaseAdapter class to populate the ListView, more exacly using the getView(..) method. I have a TextView "link" on every itemView on which if the user taps it will go to a web page. In my base adapter I set a listener on the TextView only if my product contains a link.
I have done debugging in my getView(..) method and it all works just fine, but after it exits the getView method, if there is an item that doesn`t have a link it will take the link/listener from another item from the listView.
Adapter Class:
public class MatchListBaseAdapter extends BaseAdapter {
private static ArrayList<Match> matchesArrayList;
private LayoutInflater l_Inflater;
private OnClickListener onClickListener;
public MatchListBaseAdapter(Context context, View.OnClickListener listener, ArrayList<Match> results,Activity a) {
matchesArrayList = results;
onClickListener = listener;
l_Inflater = LayoutInflater.from(context);
}
public int getCount() {
return matchesArrayList.size();
}
public Object getItem(int position) {
return matchesArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.itemlist_match, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.oferNameMLI2);
holder.expireDate = (TextView) convertView.findViewById(R.id.expireDateMLI);
holder.price = (TextView) convertView.findViewById(R.id.priceMLI);
holder.companyName = (TextView) convertView.findViewById(R.id.compNameMLI);
holder.productImage = (ImageView) convertView.findViewById(R.id.productImageMLI);
holder.companyImage = (ImageView) convertView.findViewById(R.id.companyImageMLI);
holder.description = (TextView) convertView.findViewById(R.id.moreDetailsMLI);
holder.digitalySigned = (ImageView) convertView.findViewById(R.id.digitalSignatureImageView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//populating the holder.. doesn`t have any relevance..
if(matchesArrayList.get(position).getCompanyLink() != null){
holder.companyImage.setOnClickListener(onClickListener);
holder.companyImage.setTag(position);
}
return convertView;
}
static class ViewHolder {
TextView name;
TextView expireDate;
TextView price;
TextView companyName;
TextView description;
ImageView productImage;
ImageView companyImage;
ImageView digitalySigned;
}
}
Activity onCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_layout);
matches = DataManager.getInstance().getListSubscription().get(DataManager.getInstance().getSubscriptionPosition()).getMatchesList();
ListView lv1 = (ListView) findViewById(R.id.listView_layout);
DataManager.getInstance().setAdapterMatch(new MatchListBaseAdapter(this, this, matches,this));
lv1.setAdapter(DataManager.getInstance().getAdapterMatch());
}
Just want to mention once again that i ve done debugging in the getView(..) method and it was ok, the flow was the right one, but after the items in the listView that doesn't supposed to have a listener on the TextView it had from the other items.
Also this happens always for the first item in the listView .. and it is populated with the link from the last item in the listView that contains a link.
I've searched a lot for this issue but didn't find anything relevant, but i think that there is a problem on my convertView, but i can`t figure it out..
Thx a lot
The problem is that when you're reusing the view and not setting an explicit onClickListener to it, it still contains the old listener from another product - from the view that was reused. Try making a change as following:
if(matchesArrayList.get(position).getCompanyLink() != null){
holder.companyImage.setOnClickListener(onClickListener);
holder.companyImage.setTag(position);
}
else {
holder.companyImage.setOnClickListener(null);
}

How to get view for an item in listview in android?

Is it possible to get an item view based on its position in the adapter and not in the visible views in the ListView?
I am aware of functions like getChildAt() and getItemIdAtPosition() however they provide information based on the visible views inside ListView. I am also aware that Android recycles views which means that I can only work with the visible views in the ListView.
My objective is to have a universal identifier for each item since I am using CursorAdapter so I don't have to calculate the item's position relative to the visible items.
Here's how I accomplished this. Within my (custom) adapter class:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(textViewResourceId, parent, false);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.name = (TextView) view.findViewById(R.id.name);
viewHolder.button = (ImageButton) view.findViewById(R.id.button);
viewHolder.button.setOnClickListener
(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = (int) viewHolder.button.getTag();
Log.d(TAG, "Position is: " +position);
}
});
view.setTag(viewHolder);
viewHolder.button.setTag(items.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).button.setTag(items.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
return view;
}
Essentially the trick is to set and retrieve the position index via the setTag and getTag methods. The items variable refers to the ArrayList containing my custom (adapter) objects.
Also see this tutorial for in-depth examples. Let me know if you need me to clarify anything.
See below code:
public static class ViewHolder
{
public TextView nm;
public TextView tnm;
public TextView tr;
public TextView re;
public TextView membercount;
public TextView membernm;
public TextView email;
public TextView phone;
public ImageView ii;
}
class ImageAdapter extends ArrayAdapter<CoordinatorData>
{
private ArrayList<CoordinatorData> items;
public FoodDriveImageLoader imageLoader;
public ImageAdapter(Context context, int textViewResourceId,ArrayList<CoordinatorData> items)
{
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
ViewHolder holder = null;
if (v == null)
{
try
{
holder=new ViewHolder();
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new FoodDriveImageLoader(FoodDriveModule.this);
v = vi.inflate(R.layout.virtual_food_drive_row, null);
//System.out.println("layout is null.......");
holder.nm = (TextView) v.findViewById(R.id.name);
holder.tnm = (TextView) v.findViewById(R.id.teamname);
holder.tr = (TextView) v.findViewById(R.id.target);
holder.re = (TextView) v.findViewById(R.id.received);
holder.membercount = new TextView(FoodDriveModule.this);
holder.membernm = new TextView(FoodDriveModule.this);
holder.email = new TextView(FoodDriveModule.this);
holder.phone = new TextView(FoodDriveModule.this);
holder.ii = (ImageView) v.findViewById(R.id.icon);
v.setTag(holder);
}
catch(Exception e)
{
System.out.println("Excption Caught"+e);
}
}
else
{
holder=(ViewHolder)v.getTag();
}
CoordinatorData co = items.get(position);
holder.nm.setText(co.getName());
holder.tnm.setText(co.getTeamName());
holder.tr.setText(co.getTarget());
holder.re.setText(co.getReceived());
holder.ii.setTag(co.getImage());
imageLoader.DisplayImage(co.getImage(), FoodDriveModule.this , holder.ii);
if (co != null)
{
}
return v;
}
}
A better option is to identify using the data returned by the CursorAdapter rather than visible views.
For example if your data is in a Array , each data item has a unique index.
Here, Its very short described code, you can simple re-use viewholder pattern to increase listview performance
Write below code in your getview() method
ViewHolder holder = new ViewHolder();
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.skateparklist, null);
holder = new ViewHolder();
holder.headlineView = (TextView) convertView
.findViewById(R.id.textView1);
holder.DistanceView = (TextView) convertView
.findViewById(R.id.textView2);
holder.imgview = (NetworkImageView) convertView
.findViewById(R.id.imgSkatepark);
convertView.setTag(holder); //PLEASE PASS HOLDER AS OBJECT PARAM , CAUSE YOU CAN NOY PASS POSITION IT WILL BE CONFLICT Holder and Integer can not cast
//BECAUSE WE NEED TO REUSE CREATED HOLDER
} else {
holder = (ViewHolder) convertView.getTag();
}
// your controls/UI setup
holder.DistanceView.setText(strDistance);
......
return convertview
Listview lv = (ListView) findViewById(R.id.previewlist);
final BaseAdapter adapter = new PreviewAdapter(this, name, age);
confirm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
View view = null;
String value;
for (int i = 0; i < adapter.getCount(); i++) {
view = adapter.getView(i, view, lv);
Textview et = (TextView) view.findViewById(R.id.passfare);
value=et.getText().toString();
Toast.makeText(getApplicationContext(), value,
Toast.LENGTH_SHORT).show();
}
}
});

Categories