Implement Custom Gridview in Android [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi i am new to android development. I have made an app where picture are now shown in a gridview. But i want to make it more user friendly. I have found an example but i need some help to implement my code as the example. I want to write my UserList.java as ImageAdapter.java. I want to use imageview instead of holder. How can i do that??
Example i am trying to follow:
ImageAdapter.java
package com.step2rock.www.photographynowroadtopro;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
/**
* Created by Sushimz on 5/7/2016.
*/
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4,
R.drawable.pic_5, R.drawable.pic_6,
// R.drawable.pic_7, R.drawable.pic_8,
// R.drawable.pic_9, R.drawable.pic_10,
// R.drawable.pic_11, R.drawable.pic_12,
// R.drawable.pic_13, R.drawable.pic_14,
// R.drawable.pic_15
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
Now Here is my code.
UserList.java
package com.step2rock.www.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.step2rock.www.crudproject.R;
import com.step2rock.www.crudproject.UserlistActivity;
import com.step2rock.www.model.User;
public class UserList extends BaseAdapter {
private Context mContext;
LayoutInflater inflater;
UserlistActivity activity;
ArrayList<User> users;
public UserList(Context context, ArrayList<User> users) {
mContext = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.users = users;
}
#Override
public int getCount() {
return users.size();
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
static class ViewHolder {
public ImageView ivUserImage;
public TextView tvUserName;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
activity = new UserlistActivity();
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.userrow_activity, null);
holder.ivUserImage = (ImageView) convertView.findViewById(R.id.ivUserImage);
holder.tvUserName = (TextView) convertView.findViewById(R.id.tvHeader);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
User user = new User();
user = users.get(position);
holder.ivUserImage.setImageBitmap(convertToBitmap(user.get_user_pic()));
holder.tvUserName.setText(user.get_first_name());
return convertView;
}
public Bitmap convertToBitmap(String base64String) {
byte[] decodedString = Base64.decode(base64String, Base64.DEFAULT);
Bitmap bitmapResult = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
return bitmapResult;
}
}

You are new to android and want to create a grid of showing pictures.
You can develop the grid like below Follow this tutorial.

Related

Loading Glide image

I am working on developing an app to play songs, I use Glide to get the image of the song and display it in the ListView,
the first problem
when I scrolling in my ListView the application becomes slow, and I have another problem which is when I scrolling Up the Glide load the images that have already been loaded Is there a way to fix these problems?
here is my code to get the image of songs
`
import android.content.Context;
import android.media.MediaMetadataRetriever;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class ListAdapter
extends BaseAdapter {
private Context context;
private static ArrayList<ListItemFromPhone> newList = new ArrayList<>();
ListAdapter(){}
ListAdapter(Context context, ArrayList<ListItemFromPhone> list) {
notifyDataSetChanged();
this.context = context;
newList = list;
}
#Override
public int getCount() {
return newList.size();
}
#Override
public Object getItem(int position) {
return newList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.view,null);
TextView textView = view.findViewById(R.id.textView);
final ImageView imageView = view.findViewById(R.id.imageView);
byte [] imgArtist = getImg(newList.get(position).data);
if (imgArtist != null){
Glide.with(context).asBitmap()
.load(imgArtist)
.placeholder(R.drawable.music_img)
.into(imageView);
}
else imageView.setImageResource(R.drawable.music_img);
textView.setText(newList.get(position).name);
return view;
}
private byte[] getImg(String path){
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(path);
byte[] img = retriever.getEmbeddedPicture();
retriever.release();
return img;
}
}
To avoid this problem you must use RecyclerView. There are a lot of tutorials on youtube on how to implement it.

How can I pass a created array from Custom adapter class to other class?

I'm trying to pass a String list from my custom array class to another class, but for some reason when I try to access the list, it always seems empty.
Here is my Custom Adapter class:
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class CustomAdapter extends ArrayAdapter {
private final List<Model> orders;
private final Context context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public List<String> selectedItems = new ArrayList<>();
public CustomAdapter(#NonNull Context context, List<Model> orders) {
super(context, R.layout.company_list_order, orders);
this.context = context;
this.orders = orders;
}
static class ViewHolder {
protected TextView orderText;
protected CheckBox checkOrder;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
ViewHolder viewHolder;
selectedItems = new ArrayList<>();
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.company_list_order, parent, false);
viewHolder = new ViewHolder();
viewHolder.orderText = (TextView) convertView.findViewById(R.id.textViewList);
viewHolder.checkOrder = (CheckBox) convertView.findViewById(R.id.listCheckBox);
viewHolder.checkOrder.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
orders.get(getPosition).setSelected(buttonView.isChecked());
if (orders.get(getPosition).isSelected()) {
selectedItems.add(orders.get(getPosition).getName());
Log.d("Add", "Item added: " + orders.get(getPosition).getName());
Log.d("Size", "Selected items " + selectedItems.size());
} else {
selectedItems.remove(orders.get(getPosition).getName());
Log.d("Remove", "Item removed: " + orders.get(getPosition).getName());
Log.d("Size", "Selected items " + selectedItems.size());
}
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.textViewList, viewHolder.orderText);
convertView.setTag(R.id.listCheckBox, viewHolder.checkOrder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkOrder.setTag(position);
viewHolder.orderText.setText(orders.get(position).getName());
viewHolder.checkOrder.setChecked(orders.get(position).isSelected());
return convertView;
}
public List<String> getListItems() {
return selectedItems;
}
}
At the bottom I have the method returning the list. How should I access this in another class?
Probably because you create a new ArrayList everytime getView() is called, which is called a lot. And when you call the method getListItems() the method getView() is called before due to scrolling the listview or something. Remove the line
selectedItems = new ArrayList<>();
from getView() method.
Another option is to make a class with static variables to hold the value during the applications lifetime
public class GlobalVars {
public static List<String> selectedItems;
}

Attempt to get length of null array - retrieve bitmap image from sqlite DB

I am trying to retrieve an image from an SQLite database. The image is stored as a BLOB and trying to retrieve it using an array. I'm not sure why its doing this. The permissions are all set correctly.
package com.example.joao_.quizathonegroupteamproject.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.joao_.quizathonegroupteamproject.DatabaseClasses.User;
import com.example.joao_.quizathonegroupteamproject.R;
import java.util.ArrayList;
/**
* Created by Quoc Nguyen on 13-Dec-16.
*/
public class UserListAdapter extends BaseAdapter {
private Context context;
private int layout;
private ArrayList<User> foodsList;
public UserListAdapter(Context context, int layout, ArrayList<User> foodsList) {
this.context = context;
this.layout = layout;
this.foodsList = foodsList;
}
#Override
public int getCount() {
return foodsList.size();
}
#Override
public Object getItem(int position) {
return foodsList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder{
ImageView imageView;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
View row = view;
ViewHolder holder = new ViewHolder();
if(row == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout, null);
holder.imageView = (ImageView) row.findViewById(R.id.imgFood);
row.setTag(holder);
}
else {
holder = (ViewHolder) row.getTag();
}
User food = foodsList.get(position);
byte[] tblUsersImage = food.getImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(tblUsersImage, 0, tblUsersImage.length);
holder.imageView.setImageBitmap(bitmap);
return row;
}
}
You can retrieve BLOB image and stored it in Byte array like this:
byte[] array = cursor.getBlob(columnIndex);
Bitmap bitmap = BitmapFactory.decodeByteArray(array, 0 ,array.length);
and then set it into image view
holder.imageView.setImageBitmap(bitmap);
you didn't provide your SQLite database class but i think , your food.getImage(); return null value .
check dataInsert method OR getData in your SQLite db !

proper way of creating method in your custom listview adapter

I created a simple android application with a ListView in it. I have my custom list adapter below:
package com.example.android.efaas;
import java.util.List;
import com.example.android.R;
import com.example.android.efaas.bean.ListItem;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListAdapter extends BaseAdapter {
LayoutInflater inflater = null;
Context ctx;
List<ListItem> data;
public CustomListAdapter(Activity activity, List<ListItem> data){
ctx = activity;
this.data = data;
inflater = ( LayoutInflater )ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int pos) {
return pos;
}
#Override
public long getItemId(int pos) {
return pos;
}
#Override
public View getView(int pos, View view, ViewGroup vgroup) {
View rowView = inflater.inflate(R.layout.list_menu, null);
ImageView img = (ImageView) rowView.findViewById(R.id.list_menu_image);
TextView title = (TextView) rowView.findViewById(R.id.list_menu_title);
ListItem item = data.get(pos);
img.setImageResource(item.getId());
title.setText(item.getTitle());
return rowView;
}
public ListItem getListItem(int pos){
return data.get(pos);
}
}
I have a public method getListItem, I tried to call it during ListView onClick but somehow I cant seem to make it work. Here's the image of the problem:
How do I resolve this?
You should cast adapter.getAdapter() to CustomListAdapter class then
the compile time error is due of the fact that adapter is not your CustomListAdapter. What you can do is cast the return value of adapter.getAdapter() to CustomListAdapter. Conversely you should use what's already available to retrieve the object at position. Like getItemAtPosition(int)

Android and picasso loading listview

I m actually trying to load different images with Picasso(http://square.github.io/picasso/) in a listview using a simple code and it doesnt work.
In fact, I need to load remote pictures, and display them in a simple listview, without using fragments (because I dont need it at all)
I dont know what's going wrong... Can you help ?
This is my adapter code :
package id2tel.surikatsante.view.adapter;
import android.app.Activity;
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.LinearLayout;
import android.widget.TextView;
import com.koushikdutta.ion.Ion;
import com.squareup.picasso.Picasso;
import java.util.List;
import id2tel.surikatsante.R;
import id2tel.surikatsante.view.rowItem.AnomalieRowItem;
/**
* Cette classe herite de BaseAdapter
*
* #author Vincent Danti
* #version 4.0
* #since 1.0
*/
public class AnomalieAdapter extends BaseAdapter {
Context context;
List<AnomalieRowItem> rowItems;
public AnomalieAdapter(Context context, List<AnomalieRowItem> items) {
this.context = context;
this.rowItems = items;
}
private class ViewHolder {
TextView txtTitre;
ImageView img;
ImageView imgCheck;
LinearLayout layout;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater
.inflate(R.layout.item_anomalies, null);
holder = new ViewHolder();
holder.txtTitre = (TextView) convertView
.findViewById(R.id.titre);
holder.layout = (LinearLayout) convertView.findViewById(R.id.item);
holder.img = (ImageView) convertView
.findViewById(R.id.img);
holder.imgCheck = (ImageView) convertView.findViewById(R.id.check);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final AnomalieRowItem rowItem = (AnomalieRowItem) getItem(position);
holder.txtTitre.setText(rowItem.getLibelle());
if (rowItem.getM_Anomalie().getM_icon() != null) {
Picasso.with(context).load(rowItem.getM_Anomalie().getM_icon()).placeholder(R.drawable.waiting).into(holder.img);
} else {
holder.img.setBackgroundColor(0xffED695A);
}
if (rowItem.isChecked()) {
holder.layout.setBackgroundColor(0xffdddddd);
holder.imgCheck.setVisibility(View.VISIBLE);
} else {
holder.layout.setBackgroundColor(0xffefefef);
holder.imgCheck.setVisibility(View.GONE);
}
return convertView;
}
#Override
public int getCount() {
return rowItems.size();
}
/**
* #param position est une position gps (hérite de toto)
* #return object
*/
#Override
public Object getItem(int position) {
return rowItems.get(position);
}
#Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
}
I always pass in the .error() method of Picasso....
thanks for advance

Categories