
This tutorial shows how to display a Map List containing text and images in a Custom ListView.
Step 1: Create a new project with name ListViewExample and package name com.myexample.listview. Select File/New/New Project. Fill the forms and click “Finish” button.
Step 2: Open res/layout/xml (or) main.xml and add following code. Here we add a ListView mainListview1.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:ads="http://schemas.android.com/apk/res-auto" tools:context=".MainActivity" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/mainListview1"/> </LinearLayout>
Step 3: In res/drawable/ directory add images to be used in ListView. E.g. ic_info_white_24dp.png, ic_lock_white_24dp.png, ic_security_white.png, ic_volume_up_white.png, home.png.
Step 4: In res/layout/ directory add a new file list_item.xml and add following code. Here we add a TextView for displaying text and an ImageView for displaying images.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#990048"> <ImageView android:layout_height="match_parent" android:layout_width="wrap_content" android:src="@drawable/ic_launcher" android:id="@+id/listitemImageView1" android:padding="10dp" android:scaleType="fitCenter"/> <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Large Text" android:id="@+id/listitemTextView1" android:padding="12dp" android:textSize="16sp" android:textColor="#FCFCFC"/> </LinearLayout>
Step 5: Open app/src/main/java/package and open MainActivity.java. Add following code in it. Here we create a Map list with a title and image at each position, and set an adapter to display this in ListView.
package com.myexample.listview; import android.app.*; import android.os.*; import android.view.*; import android.view.View.*; import android.widget.*; import java.util.*; import android.content.*; public class MainActivity extends Activity { private ListView listview1; // Create a new Array of type HashMap private ArrayList<HashMap<String, Object>> maplist = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listview1 = (ListView) findViewById(R.id.mainListview1); // Toast item title when ListView item is clicked listview1.setOnItemClickListener(new AdapterView.OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View child, int pos, long _pos){ Toast.makeText(getApplicationContext(), maplist.get(pos).get("title"), Toast.LENGTH_LONG).show(); } }); // Add items to the Map list { HashMap<String, Object> _item = new HashMap<>(); _item.put("title", "Info"); _item.put("icon", R.drawable.ic_info_white_24dp); maplist.add(_item); } { HashMap<String, Object> _item = new HashMap<>(); _item.put("title", "Privacy Policy"); _item.put("icon", R.drawable.ic_lock_white_24dp); maplist.add(_item); } { HashMap<String, Object> _item = new HashMap<>(); _item.put("title", "Security"); _item.put("icon", R.drawable.ic_security_white); maplist.add(_item); } { HashMap<String, Object> _item = new HashMap<>(); _item.put("title", "Volume"); _item.put("icon", R.drawable.ic_volume_up_white); maplist.add(_item); } { HashMap<String, Object> _item = new HashMap<>(); _item.put("title", "Home"); _item.put("icon", R.drawable.home); maplist.add(_item); } // Define Custom Adapter for the Map list and display it in ListView CustomListAdapter adapter = new CustomListAdapter(this, maplist); listview1.setAdapter(adapter); ((BaseAdapter)listview1.getAdapter()).notifyDataSetChanged(); } public class CustomListAdapter extends BaseAdapter { private Context context; ArrayList<HashMap<String, Object>> items; // Public constructor public CustomListAdapter(Context context, ArrayList<HashMap<String, Object>> items) { this.context = context; this.items = items; } @Override public int getCount() { // Return total of items in the list return items.size(); } @Override public Object getItem(int position) { // Return list item at the specified position return items.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; // Inflate the layout for each list row LayoutInflater _inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (v == null) { v = _inflater.inflate(R.layout.list_item, null); } // Set Width of ListView to MATCH_PARENT parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); // Get the TextView and ImageView from CustomView for displaying item TextView txtview = (TextView) v.findViewById(R.id.listitemTextView1); ImageView imgview = (ImageView) v.findViewById(R.id.listitemImageView1); // Set the text and image for current item using data from map list txtview.setText(maplist.get(position).get("title").toString()); imgview.setImageResource(maplist.get(position).get("icon")); // Return the view for the current row return v; } } }
Output:
Now run the app. The app will display a ListView with icons and text.
We mortgage funds out to people in need of monetary assistance, which have a poor credit or in want of cash to pay payments,to speculate
on enterprise.
My brother recommended I might like this website.
He was entirely right. This post truly made my day.
You can not imagine just how much time I had spent for this information! Thanks!
Thanks for finally talking about >Custom ListView with
BaseAdapter for displaying Map List – ApkTutor <Loved it!
Inspiring qᥙest there. What happened after?
Thɑnks! https://hanun87.blogspot.com/
Hello, i think tһat i sɑw you visited myy weblog
thuѕ i came to “retսrn thee favor”.I аm trying to
find things too enhance my site!I supposе its ok to use a few of your ideas!! https://sablonkaos13.blogspot.com/
Good day! This is kind of off topic but I need some help from an established blog.
Is it very hard to set up your own blog? I’m not very techincal
but I can figure things out pretty fast. I’m thinking about making my own but I’m not sure where to begin. Do
you have any tips or suggestions? With thanks
That is a great tip especially to those fresh to
the blogosphere. Brief but very precise information… Thanks
for sharing this one. A must read post!
Since Airsoft guns are designed to look realistic, carrying engineered to be visible to
others is a bad idea (and illegal). The easiest way you
can find this design, would be to first produce a research and have
it printed on paper. It was an activity that was primarily
produced by Inuit women nevertheless, there are already some men doing the work as well.
I immer verbrachte meine halbe Stunde, um diese zu lesen Blog s Artiokel oder Bewertungen Täglich
zusammen mitt einem Tasse Kaffee.
You are so awesome! I do not think I have read a single
thing like that before. So great to find someone with unique thoughts on this topic.
Really.. many thanks for starting this up. This website is one thing that’s
needed on the web, someone with a little originality!
Thanks for one’s marvelous posting! I certainly enjoyed reading
it, you might be a great author.I will make sure to bookmark your blog and will
eventually come back in the foreseeable future.
I want to encourage you to ultimately continue your great
posts, have a nice holiday weekend!
Custom ListView with BaseAdapter for displaying Map List – ApkTutor
agiscxfngz
[url=http://www.gg7ti8735g3f83xqz8r0og1si5h173w4s.org/]ugiscxfngz[/url]
giscxfngz http://www.gg7ti8735g3f83xqz8r0og1si5h173w4s.org/
Raiders coach Jack Del Rio said the lack of rushing attempts against Cincinnati was a result of the overall ineffectiveness of the offense.
Hydro Flask Australia
“I don’t think eastern conference finals will be a long war, knight’s strength is too strong. The celtics nobody with lebron, they even Owen this level of star all have no, they are really no one can stop the knight.
nike mercurial
The NBA playoffs now for today, have determined the warriors in the western conference and will compete for a spot in the finals quota, knight still don’t know who his opponent was.However, even in the face of the celtics or the wizards, presumably knight will be
nike pg 2
Carey – Owen about injury of varese, said it happened out how knight players want to play games.”I was about to enter a 5 on 5 game, but after three seconds, varese is injured
ecco Shoes