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
Related
I have a ListView and a CustomAdapter. The elements are all successfully loaded into the list. Now I want to change the background color of a certain element of the list by clicking on an external button. But I do not know how to access a specific item in the list.
Here is the CustomAdapter class:
public class CustomAdapter extends BaseAdapter {
private Context ctx;
private int resource;
private List<ItemModel> items;
public PreorderListAdapter(Context context, int resource, List<ItemModel> items){
this.ctx = context;
this.resource = resource;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public ItemModel getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#NonNull
#Override
public View getView(int i, View convertView, #NonNull ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(resource, null);
}
TextView text1 = (TextView) view.findViewById(R.id.text1);
TextView text2 = (TextView) view.findViewById(R.id.text2);
TextView text3 = (TextView) view.findViewById(R.id.text3);
ItemModel item = items.get(i);
text1.setText(item.getName());
text2.setText(item.getOption2());
text3.setText(item.getOption3());
return view;
}
}
You can do it like this inside your getView() method
view.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
view.setBackgroundColor(ContextCompat.getColor(this, R.color.yourcolor));
}
});
If you have a button on your view then performs the listener on that button
If you want to get your selected item view from your parent activity then :
yourlistview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent,View view, int position, long id)
{
selectedposition = position ;
}
});
View view = listView.getAdapter().getView(selectedposition,null,listview);
Then change its background:
view.setBackgroundColor(ContextCompat.getColor(this, R.color.yourcolor));
please define your color in your color.xml file
If you have more than one view then , create an ArrayList<View> and do some loop
create a custom listener interface in your activity and your
adapter will implement this.
public interface OnClickListenerFromActivity {
void onActivityButtonClick(int position);
}
on click in your button call your listener's method
mOnClickListenerFromActivity.onActivityButtonClick(mList.getItem(yourPostion));
implement this listener into your adapter
public class CustomAdapter extends BaseAdapter implements Activity.OnClickListenerFromActivity {
private Context ctx;
private int resource;
private List<ItemModel> items;
public PreorderListAdapter(Context context, int resource, List<ItemModel> items){
this.ctx = context;
this.resource = resource;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public ItemModel getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#NonNull
#Override
public View getView(int i, View convertView, #NonNull ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(resource, null);
}
TextView text1 = (TextView) view.findViewById(R.id.text1);
TextView text2 = (TextView) view.findViewById(R.id.text2);
TextView text3 = (TextView) view.findViewById(R.id.text3);
ItemModel item = items.get(i);
text1.setText(item.getName());
text2.setText(item.getOption2());
text3.setText(item.getOption3());
return view;
}
public void onActivityButtonClick(int position) {
// get your item through position and
// set your color here
}
}
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);
I am trying to create a custom adapter for a shop element in my Android app but every time I scroll down the list the images seem to change their position, I have had a look around on here already but suggestions that I have found and tried have been to no avail.
Each item has a title, an image (being loading via an AsyncTask or retrieved from the LRUCache) and a price of the item below is the code I use to generate the adapter.
public class ShopAdapter extends BaseAdapter {
public Context context;
public ArrayList<ShopItem> shopItemList;
public ImageCache imageCache;
String imageToLoad;
Bitmap shopImage;
public ShopAdapter(Context context, ArrayList<ShopItem> shopItemList) {
this.context = context;
this.shopItemList = shopItemList;
imageCache = new ImageCache(this.context);
}
#Override
public int getCount() {
return shopItemList.size();
}
#Override
public Object getItem(int position) {
return shopItemList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.shop_list, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.textView3);
holder.image =(ImageView) convertView.findViewById(R.id.imageView);
holder.price = (TextView) convertView.findViewById(R.id.textView4);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(shopItemList.get(position).getTitle());
shopImage = imageCache.getBitmapFromMemoryCache(shopItemList.get(position).getImage());
if(shopImage != null) {
holder.image.setImageBitmap(shopImage);
} else {
new LoadImage(context, holder.image).execute(imageToLoad);
}
holder.price.setText(shopItemList.get(position).getPrice());
return convertView;
}
static class ViewHolder {
TextView title;
ImageView image;
TextView price;
}
}
Any help would be very much appreciated!
in my android application I am displaying the contact list of phone in a list view and a check box in each row for selecting contacts. But when I am selecting a particular row about tenth row is also getting selected automatically. I am giving my code below, if any one knows please help..
public class ContactsAdapter extends BaseAdapter
{
private Context context;
private ArrayList<Contact> contacts;
public ContactsAdapter(Context context, ArrayList<Contact> contacts)
{
this.context = context;
this.contacts = contacts;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
final ViewHolder mHolder;
if (convertView == null)
{
// gridView = new View(context);
// get layout from mobile.xml
//gridView = inflater.inflate(R.layout.contact, null);
convertView = inflater.inflate(R.layout.contact, null);
mHolder = new ViewHolder();
mHolder.textName =(TextView) convertView.findViewById(R.id.name);
mHolder.textMobile =(TextView) convertView.findViewById(R.id.mobile);
mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);
convertView.setTag(mHolder);
));
}
else
{
mHolder = (ViewHolder) convertView.getTag();
mHolder.textSelector.setOnCheckedChangeListener(null);
}
mHolder.textMobile.setText(contacts.get(position).getMobile());
mHolder.textName.setText(contacts.get(position).getName());
mHolder.textSelector.setFocusable(false);
return convertView;
}
private class ViewHolder {
private TextView textMobile,textName;
private CheckBox textSelector;
}
#Override
public int getCount() {
return contacts.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
Well Thats because ViewHolder will recycle the Views everytime you Scroll
i suggest you to Use onCLick on ListItem instead checkbox
To overcome this Declare a SparseBooleanArray
SparseBooleanArray sba=new SparseBooleanArray();//this should be global
Then set the items checked state as soon as you render it
mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);
mHolder.textSelector.setChecked(sba.get(position));
Then write a onClickListener to you convertView and check it manually
convertView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
mHolder.textSelector.setChecked(isChecked);
sba.put(position,isChecked); //storing the state
}
}
);
**Well Now the sba has list items checked and you can use that for further Actions**
# Jocheved... edit your code like this...
public class ContactsAdapter extends BaseAdapter
{
private Context context;
private ArrayList<Contact> contacts;
SparseBooleanArray sba=new SparseBooleanArray();
public ContactsAdapter(Context context, ArrayList<Contact> contacts)
{
this.context = context;
this.contacts = contacts;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder mHolder;
if (convertView == null)
{
convertView = inflater.inflate(R.layout.contact, null);
mHolder = new ViewHolder();
mHolder.textName =(TextView) convertView.findViewById(R.id.name);
mHolder.textMobile =(TextView) convertView.findViewById(R.id.mobile);
mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);
convertView.setTag(mHolder);
}
else
{
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.textMobile.setText(contacts.get(position).getMobile());
mHolder.textName.setText(contacts.get(position).getName());
mHolder.textName.setSelected(true);
mHolder.textSelector.setChecked(sba.get(position));
mHolder.textSelector.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
if(mHolder.textSelector.isChecked())
{
sba.put(position, true);
}
else
{
sba.put(position, false);
}
}
});
return convertView;
}
private class ViewHolder
{
private TextView textMobile,textName;
private CheckBox textSelector;
}
#Override
public int getCount() {
return contacts.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
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().