Android, how to use a Map in a `BaseAdapter`? - java

I am trying to get json with dynamic objects in Android. So I am using Map<> to get the json. I want to use the Map inside BaseAdapter. I am getting the following error
Incompatible types.Required:com.example.EffectList. Found:java.util.Map< java.lang.String,java.util.List< com.example.EffectList >>
This is my BaseAdapter code:
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 Map<String, List<EffectList>> getItem(int position) {
return contactList.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); // COMPILE TIME ERROR HERE
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);
}
}
}
what's wrong in my code?

Well its quite simple, your getItem method returns an object of the type Map<String, List<EffectList>>, you can not cast this type directly to an EffectList.
Something like this is required to obtain an EffectList item:
EffectList item = getItem(position).get("somekey").get(0);
Solutions
Get something out of the Map and then out of the List which is
inside the Map.
Modify your BaseAdapter getItem method to return an object of the EffectList type, and modify your getCount method to return te correct item count;
A few side notes:
I suggest you rethink and redesign your data structure, do you really need a List which contains a Map which contains a List?
I would personally not use a Map to back a Adapter, the item order within a Map is (often) undefined (unless you use a Map implementation where the order is defined);

Related

Is there a way to iterate through all Child Views of ListView and cast them to TextView

Am building an app for exploring files and am using the android native resource layout for data population called android.R.simple_list_item_1, i have tried going through the methods of a listview to see if i can add a drawable to the left of each item in my list but didn't manage. So the only way i get to access a view from a listview is on event OnItemClick where the view tapped is passed as parameter to the method and then i can format it this way for the drawable
public class MainActivity extends AppCompatActivity {
//Define a listview for holding the data mined from storage
public ListView mydata;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Define the listview
mydata=findViewById(R.id.data);
mydata.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
//Cast view to TextView at position
TextView r=(TextView)view;
//get the drawable and set to textview
Drawable mydraw= ResourcesCompat.getDrawable(getResources(),R.drawable.ic_baseline_file_copy_24,null);
r.setCompoundDrawables(mydraw,null,null,null);
}
}
}
Is there in which i can get all the views in the listview, create a loop, iterate through all of them and cast to TextView array and then set my drawable to the TextView(s)?
final static class YourItemClass {
...
}
final static class ViewHolder {
TextView mTextView;
private ViewHolder(#NonNull final View simpleListItem1) {
if (!(simpleListItem1 instanceof TextView)) throw new IllegalArgumentException("TextView was expected as root of Layout");
this.mTextView = (TextView)simpleListItem1;
}
}
final class CustomArrayAdapter extends ArrayAdapter<YourItemClass> {
private final Drawable mDrawable;
private final LayoutInflater mLayoutInflater;
public CustomArrayAdapter(#NonNull final Context context, #NonNull final List<YourItemClass> objects) {
super(context, resource, objects);
this.mDrawable = ResourcesCompat.getDrawable(getResources(),R.drawable.ic_baseline_file_copy_24,null);
this.mLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#NonNull
#Override
public View getView(final int position, #Nullable View convertView, #NonNull final ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = this.mLayoutInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
final YourItemClass cYourItemClassAtSpecificPosition = this.getItem(position);
holder.mTextView.setCompoundDrawables(this.mDrawable, null, null, null);
return convertView;
}
}
Then from your Activity/Fragment:
private void setItemsToListView(#NonNull final List<YourItemClass> items) {
mListView.setAdapter(new CustomArrayAdapter(getActivity(), items));
}
This code will run "setCompoundDrawables()" for each row displayed in the ListView.

Sorting a ListView Adapter for an item that isn't in the Adapter

I'm doing an app that uses BaseAdapter to see installed app in an Android device.
In this Listview there are more items, but in the adapter there are only two, and from the second (the package name) I get the other ones
I'm searching a way to sort the items of this adapter for an item that is in the listview, but not in the adapter. Here there is my code:
class AppsAdapter extends BaseAdapter{
private Context mContext;
private List<Pair<String, List<String>>> mAppsWithPermission;
AppsAdapter(Context context, List<Pair<String, List<String>>> appsWithPermission) {
mContext = context;
mAppsWithPermission = appsWithPermission;
}
static class ViewHolder {
TextView appName;
TextView appPermissions;
ImageView appIcon;
TextView Lines;
}
#Override
public int getCount() {
return mAppsWithPermission.size();
}
#Override
public Object getItem(int position) {
return mAppsWithPermission.get(position);
}
#Override
public long getItemId(int position) {
return mAppsWithPermission.get(position).hashCode();
}
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.appName = convertView.findViewById(R.id.list_item_appname);
holder.appPermissions = convertView.findViewById(R.id.list_item_apppermissions);
holder.appIcon = convertView.findViewById(R.id.list_item_appicon);
holder.Lines = convertView.findViewById(R.id.list_item_lines);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Pair<String, List<String>> item = mAppsWithPermission.get(position);
final PackageManager packageManager = mContext.getPackageManager();
String mAppPer = item.second.toString();
int lineCount = holder.appPermissions.getLineCount();
Log.v("LINE_NUMBERS", lineCount+"");
String strI = Integer.toString(lineCount);
holder.Lines.setText(strI);
if (mAppPer.matches("")) {
holder.Lines.setText("0");
}
});
return convertView;
}
}
When you see "item.second" it's the package name.
As you can think, I'm trying to sort the entire adapter for the descendant value of holder.lines (so StrI).
This is a different question from the others that talks about sorting an adapter, because I want to sort the adapter for an external value.
If you need further informations, you've just to ask me.

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

How can I use Roboguice dependency injection in adapter android

What i have done:
I am aware of using dependency injection of Roboguice in activity like below
#InjectView(R.id.listView) ListView listView;
Question:
How to use this in a
CODE
From activity I am calling adapter like below::
AdptOrderListHome tickets = new AdptOrderListHome(getActivity(), result.getProducts());
listView.setAdapter(tickets);
AdptOrderListHome.java
public class AdptOrderListHome extends BaseAdapter {
ArrayList<InventoryProductItems> mProducts;
private Context mContext = null;
public AdptOrderListHome(Context context, ArrayList<InventoryProductItems> products) {
super();
mContext = context;
mProducts = products;
Log.d("",mProducts.size()+"");
Log.d("",mProducts.size()+"");
}
public int getCount() {
return mProducts.size();
}
public InventoryProductItems getItem(int position) {
return mProducts.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder vHolder;
if (convertView == null) {
LayoutInflater layout = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layout.inflate(R.layout.row_order_list_home, null);
vHolder = new ViewHolder(view);
view.setTag(vHolder);
} else {
vHolder = (ViewHolder) view.getTag();
}
//Set the tag
vHolder.txtProductNameId.setTag(mProducts.get(position).getProduct().getId());
vHolder.txtProductNameId.setText(mProducts.get(position).getProduct().getName());
vHolder.txtInStockId.setText(mProducts.get(position).getCurrent_Product_item_Count()+"");
vHolder.txtRbProductsId.setText(mProducts.get(position).getRB_Product_item_Count()+"");
return view;
}
class ViewHolder {
private TextView txtProductNameId, txtInStockId, txtRbProductsId;
private LinearLayout root;
public ViewHolder(View base) {
txtProductNameId = (TextView) base.findViewById(R.id.txtProductNameId);
txtInStockId = (TextView) base.findViewById(R.id.txtInStockId);
txtRbProductsId = (TextView) base.findViewById(R.id.txtRbProductsId);
root = (LinearLayout) base.findViewById(R.id.root);
}
}
}
EDIT
class ViewHolder {
#InjectView(R.id.txtProductNameId) private TextView txtProductNameId;
#InjectView(R.id.txtInStockId) private TextView txtInStockId;
#InjectView(R.id.txtRbProductsId) private TextView txtRbProductsId;
public ViewHolder(View base) {
RoboGuice.getInjector(base.getContext()).injectViewMembers(mContext);
}
}
error:
mContext giving me error in line
RoboGuice.getInjector(base.getContext()).injectViewMembers(mContext);
Cannot resolve method inject view members
You can look at the source of RoboActivity to see how it is done.
Basically, you need to call RoboInjector.injectViewMembers() in the constructor of your viewholder. Something like this:
class ViewHolder {
#InjectView(R.id.txtView)
private TextView txtView;
#InjectView(R.id.imgView)
private ImageView imgView;
public ViewHolder(View root) {
RoboGuice.getInjector(root.getContext()).injectViewMembers(this);
}
}
Note that this will only View Injection.
If you want to do Dependency Injection (with #Inject fields), you should use RoboInjector.injectMembersWithoutViews().

Integrating current ListView with a Custom Adapter with ListView to allow multiple views

The current project I have includes a ListView with a Custom Adapter. However, I am now interested in adding multiple types of views to my ListView but after several attempts I have been unable to add the two sources of code together to successfully integrate them.
Article on ListView with multiple views: ListView Article for multiple views
The custom adapter in my current code retrieves the data from another class called getData which is referenced by "data".
Code from article (ListView with multiple views):
public class MultipleItemsList extends ListActivity {
private MyCustomAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new MyCustomAdapter();
for (int i = 1; i < 50; i++) {
mAdapter.addItem("item " + i);
if (i % 4 == 0) {
mAdapter.addSeparatorItem("separator " + i);
}
}
setListAdapter(mAdapter);
}
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList mData = new ArrayList();
private LayoutInflater mInflater;
private TreeSet mSeparatorsSet = new TreeSet();
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final String item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.item1, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item2, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
}
Current code (ListView with custom adapter):
FragmentA.java
package com.example.newsapp;
public class FragmentA extends Fragment{
getData data = getData.getMyData();
public Integer ArticleID;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.fragment_a, container, false);
ListView listView = (ListView)V.findViewById(R.id.list)
CustomList adapter = new
CustomList(getActivity(), data.Headline.toArray(new String[data.Headline.size()]), data.Description.toArray(new String[data.Description.size()]), data.BitmapList.toArray(new Bitmap[data.BitmapList.size()]), data.ArticleID.toArray(new Integer[data.ArticleID.size()]));
listView.setAdapter(adapter);
listView.setOnItemClickListener(this); //Removed on click item event code.
return V;
}
CustomList.java
package com.example.newsapp;
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] titleId;
private final String[] descriptionId;
private final Bitmap[] pictureid;
public CustomList(Activity context,
String[] Headline, String[] Description, Bitmap[] BitmapList, Integer[] ArticleID) {
super(context, R.layout.single_row, Headline);
this.context = context;
this.titleId = Headline;
this.descriptionId = Description;
this.pictureid = BitmapList;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.single_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.tvTitle);
TextView txtDescription = (TextView) rowView.findViewById(R.id.tvDescription);
ImageView imageView = (ImageView) rowView.findViewById(R.id.ivIcon);
txtTitle.setText(titleId[position]);
txtDescription.setText(descriptionId[position]);
imageView.setImageBitmap(pictureid[position]);
return rowView;
}
}
Edit:
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] titleId;
private final String[] descriptionId;
private final Bitmap[] pictureid;
public CustomList(Activity context,
String[] Headline, String[] Description, Bitmap[] BitmapList, Integer[] ArticleID) {
super(context, R.layout.single_row, Headline);
this.context = context;
this.titleId = Headline;
this.descriptionId = Description;
this.pictureid = BitmapList;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
int viewType = getItemViewType(position);
View rowView = null;
switch(viewType) {
case 0:
LayoutInflater inflater = context.getLayoutInflater();
rowView= inflater.inflate(R.layout.single_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.tvTitle);
TextView txtDescription = (TextView) rowView.findViewById(R.id.tvDescription);
ImageView imageView = (ImageView) rowView.findViewById(R.id.ivIcon);
txtTitle.setText(titleId[position]);
txtDescription.setText(descriptionId[position]);
imageView.setImageBitmap(pictureid[position]);
case 1:
LayoutInflater inflater2 = context.getLayoutInflater();
rowView= inflater2.inflate(R.layout.single_row_loadmore, null, true);
}
return rowView;
}
#Override
public int getViewTypeCount() {
return 2; // TODO make this a static final
}
#Override
public int getItemViewType(int position) {
return position % 2; // 0 or 1
}
}
First, a bit of an aside: you should create a class that encapsulates a headline, description, etc. and use an array/collection of those objects to back your adapter. It will be far easier than managing many disparate arrays of things, especially if one day you decide you need another attribute of an Article (its category, for example).
class Article {
int id;
String headline;
String description;
Bitmap picture;
}
With regard to your ListView, the magic happens in the methods getItemViewType() and getViewTypeCount(). In getViewTypeCount() you return the maximum number of row types -- the article you posted uses two row types and so returns 2. In getItemViewType() you return a value between zero and (viewTypeCount - 1) -- in the article, his implementation can return 0 or 1 because his viewTypeCount is 2.
How you decide which row type applies to each item is entirely up to you. If, for example, you wanted to simply alternate view types on every row, you can do this:
#Override
public int getViewTypeCount() {
return 2; // TODO make this a static final
}
#Override
public int getItemViewType(int position) {
return position % 2; // 0 or 1
}
In other applications you would probably inspect the item at the given position to help you determine what should be returned in getItemViewtype().
The reason this functionality exists is that getView() provides a parameter (called convertView) that is a row which has been recycled. In order to give you an appropriate convertView, ListView needs to first know what row type it was. When you want to implement getView() for an adapter with multiple row types, it generally looks something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int viewType = getItemViewType(position);
switch(viewType) {
case 0:
return setUpOneViewType(position, convertView, parent);
case 1:
return setUpAnotherViewType(position, convertView, parent);
}
}
Note the cases for the switch statement correspond to the possible values that can be returned from getItemViewType(). These could be static final members.
I highly suggest watching The World of ListView. That video covers this topic as well as how to properly use convertView in your adapter implementation.

Categories

Resources