In this tutorial, I will show you how to use hashmap and simple adapter. See output below!
Here's the activity_main.xml codes below.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/lv_sample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
Here's the content.xml codes below.
<?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="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="Content" />
</LinearLayout>
Actually I have 3 ways here to output that, using ArrayAdapter, SimpleAdapter, & ListAdapter. See codes below.
Using Simple Adapter
package com.example.arrayadapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView lv_sample;
Adapter adapters;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeVars();
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
for(int x=0; x<=7; x++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("key", values[x]);
list.add(map);
}
adapters = new Adapter(getApplicationContext(), list, R.layout.content,
new String[] { "key" },
new int[] { R.id.tv_content});
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
// R.layout.content, R.id.tv_content, values);s
lv_sample.setAdapter(adapters);
lv_sample.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
ListView lv = (ListView) arg0;
TextView key=(TextView)arg0.getChildAt(position-lv.getFirstVisiblePosition()).findViewById(R.id.tv_content);
// Show Alert
Toast.makeText(getApplicationContext(), key.getText().toString() , Toast.LENGTH_LONG)
.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void initializeVars(){
lv_sample = (ListView)findViewById(R.id.lv_sample);
}
}
class Adapter extends SimpleAdapter{
private Context mcontext;
public Adapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
mcontext = context;
}
@SuppressWarnings("unchecked")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater object = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
convertView = object.inflate(R.layout.content,
null);
}
HashMap<String, String> data = (HashMap<String, String>) getItem(position);
((TextView) convertView.findViewById(R.id.tv_content)).setText((String) data.get("key"));
return convertView;
}
}
Using ListAdapter
package com.example.arrayadapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView lv_sample;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeVars();
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
for(int x=0; x<=7; x++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("key", values[x]);
list.add(map);
}
ListAdapter adapters = new SimpleAdapter(getApplicationContext(), list, R.layout.content,
new String[] { "key" },
new int[] { R.id.tv_content});
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
// R.layout.content, R.id.tv_content, values);s
lv_sample.setAdapter(adapters);
lv_sample.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
ListView lv = (ListView) arg0;
TextView key=(TextView)arg0.getChildAt(position-lv.getFirstVisiblePosition()).findViewById(R.id.tv_content);
// Show Alert
Toast.makeText(getApplicationContext(), key.getText().toString() , Toast.LENGTH_LONG)
.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void initializeVars(){
lv_sample = (ListView)findViewById(R.id.lv_sample);
}
}
Using ArrayAdapter
package com.example.arrayadapter;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView lv_sample;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeVars();
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.content, R.id.tv_content, values);
lv_sample.setAdapter(adapter);
lv_sample.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
int itemPosition = position;
String itemValue = (String)lv_sample.getItemAtPosition(itemPosition);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void initializeVars(){
lv_sample = (ListView)findViewById(R.id.lv_sample);
}
}
That's it, I hope this simple tutorial may help you a lot. Happy Coding!