How to display Map values in ListView? - java

I am trying to display the Map values in ListView using BaseAdapter. Using the below code I am able to successfully print all the values in Map. But i don't know how to display a single value of Map using its key.
MyContactAdapter2.java
public class MyContactAdapter2 extends BaseAdapter {
List<EffectList> contacts;
Context context;
private LayoutInflater mInflater;
// Constructors
public MyContactAdapter2(Context context, List<Map<String, List<EffectList>>> objects) {
this.context = context;
this.mInflater = LayoutInflater.from(context);
this.contacts = new ArrayList<>();
Map<String, List<EffectList>> map = objects.get(0);
Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
this.contacts.addAll(map.get(iterator.next()));
}
}
#Override
public int getCount() {
int count = contacts.size();
return count;
}
#Override
public EffectList getItem(int position) {
return contacts.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final MyContactAdapter2.ViewHolder vh;
if (convertView == null) {
View view = mInflater.inflate(R.layout.get_layout_row_view, parent, false);
vh = MyContactAdapter2.ViewHolder.create((RelativeLayout) view);
view.setTag(vh);
} else {
vh = (MyContactAdapter2.ViewHolder) convertView.getTag();
}
EffectList item = getItem(position);
vh.textViewName.setText(item.getEffectsId());
vh.textViewEmail.setText(item.getEffectsName());
return vh.rootView;
}
private static class ViewHolder {
public final RelativeLayout rootView;
public final ImageView imageView;
public final TextView textViewName;
public final TextView textViewEmail;
private ViewHolder(RelativeLayout rootView, ImageView imageView, TextView textViewName, TextView textViewEmail) {
this.rootView = rootView;
this.imageView = imageView;
this.textViewName = textViewName;
this.textViewEmail = textViewEmail;
}
public static MyContactAdapter2.ViewHolder create(RelativeLayout rootView) {
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
TextView textViewName = (TextView) rootView.findViewById(R.id.textViewName);
TextView textViewEmail = (TextView) rootView.findViewById(R.id.textViewEmail);
return new MyContactAdapter2.ViewHolder(rootView, imageView, textViewName, textViewEmail);
}
}
}
This is the Json I need to parse,
{
"effect_list": [{
"1":[
{
"effects_id":"1",
"effects_name":"Band 1"
},
{
"effects_id":"2",
"effects_name":"Band 2"
}
],
"2": [
{
"effects_id":"4",
"effects_name":"Background Blur"
},
{
"effects_id":"5",
"effects_name":"Blemish Removal"
}
]
}]
}
I want to display only the values of "1" ("effects_id":"1","effects_name":"Band 1" and "effects_id":"2","effects_name":"Band 2"). How can I achieve it?
I am confused with the code inside constructor too. If you can,Please explain the following code,
this.contacts = new ArrayList<>();
Map<String, List<EffectList>> map = objects.get(0);
Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
this.contacts.addAll(map.get(iterator.next()));
EDIT
EffectList.java
public class EffectList {
#SerializedName("effects_id")
#Expose
private String effectsId;
#SerializedName("effects_name")
#Expose
private String effectsName;
public String getEffectsId() {
return effectsId;
}
/* public void setEffectsId(String effectsId) {
this.effectsId = effectsId;
}
*/
public String getEffectsName() {
return effectsName;
}
/*
public void setEffectsName(String effectsName) {
this.effectsName = effectsName;
}
*/
}

You are getting objects as List<Map<String, List<EffectList>>> therefore you are doing a get on the first index to get the first map in the list.
Afterwards you are retrieving the key set of your map, this is a Set which contains all keys that are available in your map.
The iterator is used to retrieve the corresponding List from your map to each key that your map contains.
You could optimize this a bit by doing following:
Set<List<EffectList>> values = map.values;
Iterator<List<EffectList>> it = values.iterator();
while(it.hasNext() {
this.contacts.addAll(it.next());
}
To archive the desired output you could filter the EffectList before adding this to the contacts list.
EDIT
As example(using google commons collections):
List<EffectList> itValue = it.next();
List<EffectList> filteredValue = Collections.filter(itValue, new Predicate<EffectList>(){
public boolean apply(EffectList input){
if(input.getEffectsId().equals("1") || input.getEffectsId().equals("2"){
return true;
}
return false;
}
);
this.contacts.addAll(filteredValue);

Related

how to create method in activity to put it in custom adapter listview?

at first I got an error " The code of method getView(int, View, ViewGroup) is exceeding the 65535 bytes limit " because I typed too much code, finally I created a method, but the method is error because the variable inside is not detected
I created Method _Differ();
public void _Differ() {
if (_position == 5) {
full.setText("iiii");
}
}
i put method Differ(); in custom adapter listview
this is my code in listview custom adapter
public class Listview1Adapter extends BaseAdapter {
ArrayList<HashMap<String, Object>> _data;
public Listview1Adapter(ArrayList<HashMap<String, Object>> _arr) {
_data = _arr;
}
#Override
public int getCount() {
return _data.size();
}
#Override
public HashMap<String, Object> getItem(int _index) {
return _data.get(_index);
}
#Override
public long getItemId(int _index) {
return _index;
}
#Override
public View getView(final int _position, View _v, ViewGroup _container) {
LayoutInflater _inflater = getLayoutInflater();
View _view = _v;
if (_view == null) {
_view = _inflater.inflate(R.layout.ccc, null);
}
final LinearLayout linear1 = _view.findViewById(R.id.linear1);
final LinearLayout linear2 = _view.findViewById(R.id.linear2);
final LinearLayout linear3 = _view.findViewById(R.id.linear3);
final LinearLayout linear4 = _view.findViewById(R.id.linear4);
final TextView textview1 = _view.findViewById(R.id.textview1);
final TextView full = _view.findViewById(R.id.full);
final TextView textview2 = _view.findViewById(R.id.textview2);
final TextView shortt = _view.findViewById(R.id.shortt);
final TextView name = _view.findViewById(R.id.name);
final TextView email = _view.findViewById(R.id.email);
full.setText(mapList.get((int)_position).get("end").toString());
shortt.setText(mapList.get((int)_position).get("rule").toString());
email.setText(mapList.get((int)_position).get("start").toString());
_Differ();
return _view;
}
}
//This is your method which will go in your activity >>
public void _Differ(TextView txt, int _position) {
if (_position == 5) {
txt.setText("iiii");
}
}
// This Code Goes in your ListAdpater >>
full.setText(mapList.get((int)_position).get("end").toString());
shortt.setText(mapList.get((int)_position).get("rule").toString());
email.setText(mapList.get((int)_position).get("start").toString());
_Differ(full, _position);
I think this can help you, By the way, Are you using Sketchware Pro 🤔

How do I convert my BaseAdapter to a RecyclerAdapter

So I wanted to change my GridView to a RecyclerView and with that I had to change my BaseAdapter to a RecyclerAdapter.
I already tried making changes, but I have no clue how to switch the code into the RecyclerAdapter.
Here is how my BaseAdapter looks like :
class AlbumAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap< String, String >> data;
public AlbumAdapter(Activity a, ArrayList < HashMap < String, String >> d) {
activity = a;
data = d;
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
AlbumViewHolder holder = null;
if (convertView == null) {
holder = new AlbumViewHolder();
convertView = LayoutInflater.from(activity).inflate(
R.layout.album_row, parent, false);
holder.galleryImage = (ImageView) convertView.findViewById(R.id.galleryImage);
holder.gallery_count = (TextView) convertView.findViewById(R.id.gallery_count);
holder.gallery_title = (TextView) convertView.findViewById(R.id.gallery_title);
convertView.setTag(holder);
} else {
holder = (AlbumViewHolder) convertView.getTag();
}
holder.galleryImage.setId(position);
holder.gallery_count.setId(position);
holder.gallery_title.setId(position);
HashMap < String, String > song = new HashMap < String, String > ();
song = data.get(position);
try {
holder.gallery_title.setText(song.get(Function.KEY_ALBUM));
holder.gallery_count.setText(song.get(Function.KEY_COUNT));
Glide.with(activity)
.load(new File(song.get(Function.KEY_PATH))) // Uri of the picture
.into(holder.galleryImage);
} catch (Exception e) {}
return convertView;
}
}
class AlbumViewHolder {
ImageView galleryImage;
TextView gallery_count, gallery_title;
}
And thanks in advance !
Simply use this, feel free to modify to suit your need, make sure to read the comments as they really will help you understand few things.
public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.ViewHolder> {
private static final String KEY_ALBUM = "KEY_ALBUM";
private static final String KEY_COUNT = "KEY_COUNT";
private static final String KEY_PATH = "KEY_PATH";
private itemClickInterface clickInterface;
//private List<String> data;
private ArrayList<HashMap<String, String >> data;
// public AlbumAdapter(itemClickInterface clickInterface, List<String> data) { // Forget about this if your data is not an array of Strings.
// public AlbumAdapter(itemClickInterface clickInterface, ArrayList<HashMap< String, String >> data) { // Forget about this if you're not ready to pass an onclick interface
public AlbumAdapter(ArrayList<HashMap< String, String >> data) {
this.data = data;
// this.clickInterface = clickInterface; //Simply ignore since you're not passing an interface of clicklister
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.album_row, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
try {
final String data_for_albums = this.data.get(position).get(KEY_ALBUM);
final String data_for_counts = this.data.get(position).get(KEY_COUNT);
final String data_for_paths = this.data.get(position).get(KEY_PATH);
holder.gallery_title.setText(data_for_albums);
holder.gallery_count.setText(data_for_counts);
Glide.with(activity)
.load(new File(data_for_paths)) // Uri of the picture
.into(holder.galleryImage);
/*
* You can modify this as you want.
* */
// holder.galleryImage.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// clickInterface.click(data_for_paths); // This depends on you.
// }
// });
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public int getItemCount() {
return data.size();
}
/*--------------------------------------------------------------------------------------
| GET REFERENCES TO VIEWS HERE
*--------------------------------------------------------------------------------------*/
class ViewHolder extends RecyclerView.ViewHolder {
ImageView galleryImage;
TextView gallery_count, gallery_title;
ViewHolder(View itemView) {
super(itemView);
galleryImage = itemView.findViewById(R.id.galleryImage);
gallery_count = itemView.findViewById(R.id.gallery_count);
gallery_title = itemView.findViewById(R.id.gallery_title);
}
}
}
How to call from our activity
ArrayList<HashMap<String, String >> data = new ArrayList<>();
//I'm assuming you already feed your data, so you're not passing null like me here.
cardlistview = findViewById(R.id.cardlistview);
albumAdapter = new AlbumAdapter(your_arraysOfHashMap);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
cardlistview.setLayoutManager(mLayoutManager);
cardlistview.setAdapter(albumAdapter);

List<Map<String, List<>>> in BaseAdapter, Android

This question has been asked previously. Sorry, I am new to Android, those answers didn't helped me.
I am trying to parse the following json using retrofit,Android. In the json below, objects 4 and 1 are Dynamic. So I am using Map to get the data.
{
"effect_list":[
{
"4":[
{
"effects_id":"18",
"effects_name":"Band 1"
},
{
"effects_id":"19",
"effects_name":"Band 2"
}
],
"1":[
{
"effects_id":"1",
"effects_name":"Background Blur"
},
{
"effects_id":"4",
"effects_name":"Blemish Removal"
}
]
}
]
}
Now I want to display the json data inside listview using BaseAdapter.
I tried
MyContactAdapter2.java
public class MyContactAdapter2 extends BaseAdapter {
List<Map<String, List<EffectList>>> contactList;
Context context;
private LayoutInflater mInflater;
// Constructors
public MyContactAdapter2(Context context, List<Map<String, List<EffectList>>> objects) {
this.context = context;
this.mInflater = LayoutInflater.from(context);
contactList = objects;
}
#Override
public int getCount() {
return contactList.size();
}
#Override
public EffectList getItem(int position) {
return (EffectList) contactList.get(position); <----- ERROR HERE DURING RUNTIME
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final MyContactAdapter2.ViewHolder vh;
if (convertView == null) {
View view = mInflater.inflate(R.layout.get_layout_row_view, parent, false);
vh = MyContactAdapter2.ViewHolder.create((RelativeLayout) view);
view.setTag(vh);
} else {
vh = (MyContactAdapter2.ViewHolder) convertView.getTag();
}
EffectList item = getItem(position); <----- ERROR HERE DURING RUNTIME
vh.textViewName.setText(item.getEffectsId());
vh.textViewEmail.setText(item.getEffectsName());
return vh.rootView;
}
private static class ViewHolder {
public final RelativeLayout rootView;
public final ImageView imageView;
public final TextView textViewName;
public final TextView textViewEmail;
private ViewHolder(RelativeLayout rootView, ImageView imageView, TextView textViewName, TextView textViewEmail) {
this.rootView = rootView;
this.imageView = imageView;
this.textViewName = textViewName;
this.textViewEmail = textViewEmail;
}
public static MyContactAdapter2.ViewHolder create(RelativeLayout rootView) {
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
TextView textViewName = (TextView) rootView.findViewById(R.id.textViewName);
TextView textViewEmail = (TextView) rootView.findViewById(R.id.textViewEmail);
return new MyContactAdapter2.ViewHolder(rootView, imageView, textViewName, textViewEmail);
}
}
}
I am getting the following error during Runtime.
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to my class
What am I doing wrong here?
Return Map<String,List<EffectList>> from getItem method because contactList contains Map as items:
#Override
public Map<String,List<EffectList>> getItem(int position) {
return contactList.get(position);
}
And in getView use keys to access values from Map:
Map<String,List<EffectList>> map=getItem(position);
List<EffectList> effectList = map.get("KEY_NAME");
// get item from effectList using for loop

Parse Dynamic nested array in Android Retrofit

In the following json I am able to read the categories field. How can I read the effect_list attributes 4 and 1 which are Dynamic?
{
"categories":[
{
"mcategory_id":"4",
"mcategory_name":"Band"
},
{
"mcategory_id":"1",
"mcategory_name":"Basic Effects"
},
{
"mcategory_id":"3",
"mcategory_name":"Bg Image Card"
}
],
"effect_list":[{
"4":[
{
"effects_id":"18",
"effects_name":"Band 1"
},
{
"effects_id":"19",
"effects_name":"Band 2"
}
],
"1":[
{
"effects_id":"1",
"effects_name":"Background Blur"
},
{
"effects_id":"4",
"effects_name":"Blemish Removal"
}
]
} ]
}
What I did so far
ContactList.java
#SerializedName("categories")
#Expose
private List<Category> categories = null;
#SerializedName("effect_list")
#Expose
private Map<String, List<List<EffectList>>> effectlist;
public Map<String, List<List<EffectList>>> getEffectlist() {
return effectlist;
}
public void setEffectlist(Map<String, List<List<EffectList>>> effectlist) {
this.effectlist = effectlist;
}
public List<Category> getCategories() {
return categories;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
}
EffectList.java
#SerializedName("effects_id")
#Expose
private String effectsId;
#SerializedName("effects_name")
#Expose
private String effectsName;
public String getEffectsId() {
return effectsId;
}
public void setEffectsId(String effectsId) {
this.effectsId = effectsId;
}
public String getEffectsName() {
return effectsName;
}
public void setEffectsName(String effectsName) {
this.effectsName = effectsName;
}
ApiService.java
#GET("json_new")
Call<ContactList> getMyJSON();
Now I am able to read the attributes of categories but effect_list is null.
How to read the effect list attributes where 4 and 1 are Dynamic using retrofit?
Edit:
I am using ArrayAdapter to store the list. What are the changes I need to do in MyContactAdapter1.java?
MyContactAdapter1.java
List<EffectList> contactList;
Context context;
private LayoutInflater mInflater;
// Constructors
public MyContactAdapter1(Context context, List<EffectList> objects) {
super(context, 0, objects);
this.context = context;
this.mInflater = LayoutInflater.from(context);
contactList = objects;
}
#Override
public EffectList getItem(int position) {
return contactList.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final MyContactAdapter1.ViewHolder1 vh;
if (convertView == null) {
View view = mInflater.inflate(R.layout.get_layout_row_view, parent, false);
vh = MyContactAdapter1.ViewHolder1.create((RelativeLayout) view);
view.setTag(vh);
} else {
vh = (MyContactAdapter1.ViewHolder1) convertView.getTag();
}
EffectList item = getItem(position);
// vh.textViewName.setText(item.getEffectsId());
vh.textViewName.setText(item.getEffectsName());
vh.textViewEmail.setText(item.getEffectsId());
// Picasso.with(context).load(item.getProfilePic()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(vh.imageView);
return vh.rootView;
}
private static class ViewHolder1 {
public final RelativeLayout rootView;
public final ImageView imageView;
public final TextView textViewName;
public final TextView textViewEmail;
private ViewHolder1(RelativeLayout rootView, ImageView imageView, TextView textViewName, TextView textViewEmail) {
this.rootView = rootView;
this.imageView = imageView;
this.textViewName = textViewName;
this.textViewEmail = textViewEmail;
}
public static MyContactAdapter1.ViewHolder1 create(RelativeLayout rootView) {
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
TextView textViewName = (TextView) rootView.findViewById(R.id.textViewName);
TextView textViewEmail = (TextView) rootView.findViewById(R.id.textViewEmail);
return new MyContactAdapter1.ViewHolder1(rootView, imageView, textViewName, textViewEmail);
}
}
Main.java
call.enqueue(new Callback<ContactList>() {
#Override
public void onResponse(Call<ContactList> call, Response<ContactList> response) {
if (response.isSuccessful()) {
Toast.makeText(getApplicationContext(),"Response Success",Toast.LENGTH_LONG).show();
Log.d("RESPONSE: ", contactList1.toString());
contactList = (List<Map<String,List<EffectList>>>) response.body().getEffectlist();
adapter = new MyContactAdapter1(uploadpage.this, contactList);
listView.setAdapter(adapter);
} else {
}
}

Custom Adapter not working for the listview from Parse.com

I am trying to create an android application using a database from Parse.com. I am using a custom adapter to create a listview. I don't find any errors with the code and yet the listeview is not showing up. Nothing there in the logcat as well. Just the listview does not show up.
lv = (ListView)findViewById(R.id.listView);
mProgress = (ProgressBar)findViewById(R.id.check_progress);
mProgress.setVisibility(View.VISIBLE);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Sellers");
query.orderByAscending("Name");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if (e == null) {
studentsList = new ArrayList<Sellers>();
for (ParseObject ob : parseObjects) {
s = new Sellers();
s.setName(ob.getString("Name").toString());
s.setAddress1(ob.getString("Address1").toString());
s.setAddress2(ob.getString("Address2").toString());
s.setShopName(ob.getString("ShopName").toString());
s.setEmail(ob.getString("Email").toString());
s.setPhone(ob.getString("Phone").toString());
s.setZipcode(ob.getString("Zipcode").toString());
studentsList.add(s);
}
adapter = new ListviewAdapter(CheckStatus.this, studentsList);
lv.setAdapter(adapter);
mProgress.setVisibility(View.GONE);
} else {
mProgress.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
This is the activity where I am invoking the listview.
public class ListviewAdapter extends BaseAdapter{
private final static String TAG = ListviewAdapter.class.getSimpleName();
private Context activity;
private LayoutInflater inflater = null;
private List<Sellers> sellers;
int layout;
public ListviewAdapter(Context activity, List<Sellers> sellers) {
this.activity = activity;
this.sellers = sellers;
inflater = LayoutInflater.from(activity);
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
public class ViewHolder {
TextView name ;
TextView shop ;
TextView address1 ;
TextView address2;
TextView phone;
TextView email;
RelativeLayout rl;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
View v =view;
ViewHolder holder = new ViewHolder();
if (view == null) {
LayoutInflater li = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.list_item_layout,null);
holder.name = (TextView)v.findViewById(R.id.seller_name);
holder.shop = (TextView)v.findViewById(R.id.shop_name);
holder.address1 = (TextView)v.findViewById(R.id.address1);
holder.address2 = (TextView)v.findViewById(R.id.address2);
holder.phone = (TextView)v.findViewById(R.id.phone);
holder.email = (TextView)v.findViewById(R.id.emailID);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
Sellers s = sellers.get(position);
// String a = s.Name;
// Log.d(TAG, a);
holder.name.setText(s.getName());
holder.shop.setText(s.getShopName());
holder.address1.setText(s.getAddress1());
holder.address2.setText(s.getAddress2());
holder.phone.setText(s.getPhone());
holder.email.setText(s.getEmail());
Log.d("CustomAdapter.class", "CustomAdapter");
// imageView.setImageDrawable(s.getPic());
return v;
}
}
And this is the custom adapter. There are no null pointer exceptions showing up in the logcat. I can't determine why the listview is not getting populated.
Try this;
#Override
public int getCount() {
return sellers.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position:
}
You have to implement your code on getCount() by return number of item listview will be created.

Categories

Resources