Using hashMaps in custom adapters for listView
you can create a custome adapter for it. In this adapter you have to set CheckChangeListener
on checkbox
in getView
method.
public class MyBaseAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
public MyBaseAdapter(Context context) {
this.mContext = context;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.row_layout, null);
holder.txtName = (TextView) convertView.findViewById(R.id.textView1);
holder.chkTick = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final int pos = position;
holder.txtName.setText("ABC");
holder.chkTick.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
Toast.makeText(mContext, "Checked", Toast.LENGTH_SHORT).show();
}
}
});
return convertView;
}
}
You may also check this link1,link2 for more details
You can use hashMap for custome adapter and set value in UI
as follow
public class HashMapAdapter extends BaseAdapter {
private HashMap<String, String> mData = new HashMap<String, String>();
private String[] mKeys;
public HashMapAdapter(HashMap<String, String> data){
mData = data;
mKeys = mData.keySet().toArray(new String[data.size()]);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(mKeys[position]);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
String key = mKeys[pos];
String Value = getItem(pos).toString();
//do your view stuff here
return convertView;
}
}
HashMap to ListView
Make simple adapter class:
MyAdapter.java
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Map;
public class MyAdapter extends BaseAdapter {
private final ArrayList mData;
public MyAdapter(Map<String, String> map) {
mData = new ArrayList();
mData.addAll(map.entrySet());
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Map.Entry<String, String> getItem(int position) {
return (Map.Entry) mData.get(position);
}
@Override
public long getItemId(int position) {
// TODO implement you own logic with ID
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View result;
if (convertView == null) {
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_adapter_item, parent, false);
} else {
result = convertView;
}
Map.Entry<String, String> item = getItem(position);
// TODO replace findViewById by ViewHolder
((TextView) result.findViewById(android.R.id.text1)).setText(item.getKey());
((TextView) result.findViewById(android.R.id.text2)).setText(item.getValue());
return result;
}
}
layout/my_adapter_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@android:id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Your code:
public void showCinemas(HashMap<String, String> cinemas) {
MyAdapter adapter = new MyAdapter(cinemas);
list.setAdapter(adapter);
}
How to get HashMap value into CustomlistAdapter for Listview in android
1. As you are passing HashMap
, so extend Customlist
from ArrayAdapter<HashMap<String, String>>
instead of ArrayAdapter<String>
.
2. Update constructor
as below:
public Customlist(Context context, ArrayList<HashMap<String, String>> contactlist) {
super(context, R.layout.model, contactlist);
this.context = context;
this.contactlist= contactlist;
}
3. In your getView()
method, get HashMap
from List
and get id
, name
and imageurl
as below:
// Contact
HashMap<String, String> contact = contactlist.get(position);
String id = contact.get("id");
String name = contact.get("name");
String imageurl = contact.get("imageurl");
Here is the updated Customlist
adapter:
public class Customlist extends ArrayAdapter<HashMap<String, String>> {
private Context context;
private ArrayList<HashMap<String, String> contactlist;
public Customlist(Context context, ArrayList<HashMap<String, String>> contactlist) {
super(context, R.layout.model, contactlist);
this.context = context;
this.contactlist= contactlist;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.model, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.namelist);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imagelist);
// Contact
HashMap<String, String> contact = contactlist.get(position);
String id = contact.get("id");
String name = contact.get("name");
String imageurl = contact.get("imageurl");
// Do something with value id, name and imageurl
return rowView;
}
}
Hope this will help~
How can i set Hashmap data into listview in android?
At the time of creating ArrayList<HashMap<String, String>>
in HashMap you need to specify key & based on that key you can display data in getView
Here in this example i have given first_name
& last_name
as HashMap key for sample.
private class ListViewCustomAdapter extends BaseAdapter {
Context context;
int totalDisplayDatasize = 0;
ArrayList<HashMap<String, String>> codeLeanChapterList;
public ListViewCustomAdapter(Context context,
ArrayList<HashMap<String, String>> codeLeanChapterList) {
this.context = context;
this.codeLeanChapterList = codeLeanChapterList;
if (this.codeLeanChapterList != null)
totalDisplayDatasize = this.codeLeanChapterList.size();
}
@Override
public int getCount() {
// this could be one of the reason for not showing listview.set
// total data length for count
return totalDisplayDatasize;
}
@Override
public HashMap<String, String> getItem(int i) {
return this.codeLeanChapterList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
private class Holder {
TextView textView1, textView2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = null;
View view = convertView;
/*
* First time for row if view is not created than inflate the view
* and create instance of the row view Cast the control by using
* findview by id and store it in view tag using holder class
*/
if (view == null) {
holder = new Holder();
// / No need to create LayoutInflater instance in
// constructor
convertView = LayoutInflater.from(this.context).inflate(
R.layout.listitem, parent,false);
holder.textView1 = (TextView) convertView
.findViewById(R.id.textView1);
holder.textView2 = (TextView) convertView
.findViewById(R.id.textView2);
convertView.setTag(holder);
} else {
/*
* Here view next time it wont b null as its created and
* inflated once and in above if statement its created. And
* stored it in view tag. Get the holder class from view tag
*/
holder = (Holder) convertView.getTag();
}
holder.textView2.setText("Key Name (First Name): "
+ codeLeanChapterList.get(position).get("first_name"));
String flagChar = codeLeanChapterList.get(position).get("last_name");
return convertView;
}
}
How to use insert data from ArrayListHashMapString, String into a list view in Android Studio
Well without seeing your other class I can take a shot at this. First your Listview adapter is not good, try this one:
public class UsersmyListAdapter extends ArrayAdapter<HashMap<String, String>> {
private static final String TAG_NAME = "Name";
private static final String AGE ="UserAge";
private static final String CLASS = "Class";
private static final String LEVEL = "Level";
private static final String SKILL = "Skill";
private static final String ACTIVITY = "Activity";
private static final String TIME_STAMP = "TimeStamp";
private static final String TAG_IN_GROUP = "InGroup";
public ArrayList<HashMap<String, String>> myList;
Activity activity;
public UsersmyListAdapter(Activity activity, ArrayList<HashMap<String, String>> myList){
super(activity, R.layout.users_lfg_list_item, myList);
this.activity = activity;
this.myList = myList;
}
@Override
public int getCount(){
return myList.size();
}
@Override
public Object getItem(int position){
return myList.get(position);
}
@Override
public long getItemId(int position){
return 0;
}
public View getView(int position ,View convertView, ViewGroup parent){
HashMap<String, String> map = myList.get(position);
ItemViewHolder viewHolder;
if(convertView == null){
viewHolder = new ItemViewHolder();
convertView = activity..getLayoutInflater().inflate(R.layout.users_lfg_list_item, null, true);
viewHolder.name = (TextView) convertView.findViewById(R.id.name);
viewHolder.classType = (TextView) convertView.findViewById(R.id.classType);
viewHolder.skill = (TextView) convertView.findViewById(R.id.skill);
viewHolder.activityType = (TextView) convertView.findViewById(R.id.activityType);
convertView.setTag(viewHolder);
} else {
viewHolder = (ItemViewHolder) convertView.getTag();
}
viewHolder.name.setText(map.get(TAG_NAME));
viewHolder.classType.setText(map.get(CLASS));
viewHolder.skill.setText(map.get(SKILL));
viewHolder.activityType.setText(map.get(ACTIVITY));
return convertView;
}
public class ItemViewHolder {
TextView name;
TextView classType;
TextView skill;
TextView activityType;
}
}
Second you should never set the Listview adapter on your complete method of AsyncTask. You should set the adapter on create after you declare the listview and when the AsyncTask finishes then add the new items to the list and call:
adapter.notifyDataSetChanged();
Hope it helps with your issue!
Android. Using HashMap for listView
It's because you add two times map
which is a reference to the same object. After your first NewsArrayList.add((HashMap<String, String>) map);
, you overwrite the values of title
and date
, that's why you end up with the same values twice.
To fix, just create another map like this:
// map
NewsArrayList = new ArrayList<HashMap<String, String>>();
map1 = new HashMap<String, String>();
map1 .put("title", "New branch opened!");
map1 .put("date", "28.03.2014");
NewsArrayList.add((HashMap<String, String>) map1);
map2 = new HashMap<String, String>();
map2.put("title", "Second one!");
map2.put("date", "28.03.2014");
NewsArrayList.add((HashMap<String, String>) map2);
How to create custom adapter for custom listview with Hashmap
You can design your BaseAdapter
like below code, also you can optimize listview performance using ViewHolder
pattern
private class MyAdapter extends BaseAdapter{
private LayoutInflater mlayoutInflater;
public MyAdapter(Context context) {
mlayoutInflater = LayoutInflater.from(context); //Dynamic layout mapping
}
@Override
public int getCount() {
return orderlist.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mlayoutInflater.inflate(R.layout.listitem, null); // According to the layout of the document to instantiate view
TextView tv = (TextView)convertView.findViewById(R.id.textView);
tv.setText(orderlist.get(position).get(GET_BRAND).toString());
return convertView;
}
}
Related Topics
Getting the Dimensions of the Soft Keyboard
Using Listview:How to Add a Header View
Android Keyboard Hides Edittext
Return Value from Asynctask Class Onpostexecute Method
Firebase Admin Sdk for Android, Methods Not Found
Get the Distance Between Two Geo Points
How to Format Datetime in Flutter
Android Studio: Drawable Folder: How to Put Images for Multiple Dpi
Accessing UI Thread Handler from a Service
Collapsingtoolbarlayout Subtitle
Convert Bitmap Array to Yuv (Ycbcr Nv21)
How to Add an Image in Email Body
Theme/Style Is Not Applied When Inflater Used with Applicationcontext
How to Send File Using Bluetooth on Android Programatically
Android:How to Capture a Image of Gps Location
Android: Test Push Notification Online (Google Cloud Messaging)