.
ListView Custom Adapter OnItemClickListener
Using Android Studio 1.4
1) Create New Project
Application Name: MyListViewAdapterClickListener1
Select API 14:Android 4.0 (IceCreamSandwich)
Follow the wizard to create New Blank Activity.
2) Edit res/layout/content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"
android:layout_height="match_parent"android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>
|
OUTCOME.
3) Add Row Layout
3.1) res/layout/row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="2dp" >
<ImageView
android:id="@+id/profile_pic"
android:layout_width="70dp"
android:layout_height="70dp"
android:contentDescription="desc"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="@+id/member_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/profile_pic"
android:paddingBottom="10dp"
android:text="txt"
android:textSize="20sp" />
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/member_name"
android:layout_below="@+id/member_name"
android:text="txt"
android:textSize="16sp" />
<TextView
android:id="@+id/contact_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/member_name"
android:layout_alignBottom="@+id/member_name"
android:layout_alignParentRight="true"
android:text="txt"
android:textSize="16sp" />
</RelativeLayout>
|
4) Create Data Model
4.1) Contact.java
package com.notarazi.mylistviewadapterclicklistener1;
public class Contact {
private String member_name;
private int profile_pic_id;
private String status;
private String contactType;
public Contact(String member_name, int profile_pic_id, String status,
String contactType) {
this.member_name = member_name;
this.profile_pic_id = profile_pic_id;
this.status = status;
this.contactType = contactType;
}
public String getMember_name() {
return member_name;
}
public void setMember_name(String member_name) {
this.member_name = member_name;
}
public int getProfile_pic_id() {
return profile_pic_id;
}
public void setProfile_pic_id(int profile_pic_id) {
this.profile_pic_id = profile_pic_id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getContactType() {
return contactType;
}
public void setContactType(String contactType) {
this.contactType = contactType;
}
}
|
5) Create Custom ArrayAdapter
5.1) ContactAdapter.java
package com.notarazi.mylistviewadapterclicklistener1;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class ContactAdapter extends BaseAdapter {
Context context;
List<Contact> rowItems;
ContactAdapter(Context context, List<Contact> rowItems) {
this.context = context;
this.rowItems = rowItems;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
/* private view holder class */
private class ViewHolder {
ImageView profile_pic;
TextView member_name;
TextView status;
TextView contactType;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_layout, null);
holder = new ViewHolder();
holder.member_name = (TextView) convertView
.findViewById(R.id.member_name);
holder.profile_pic = (ImageView) convertView
.findViewById(R.id.profile_pic);
holder.status = (TextView) convertView.findViewById(R.id.status);
holder.contactType = (TextView) convertView
.findViewById(R.id.contact_type);
Contact row_pos = rowItems.get(position);
holder.profile_pic.setImageResource(row_pos.getProfile_pic_id());
holder.member_name.setText(row_pos.getMember_name());
holder.status.setText(row_pos.getStatus());
holder.contactType.setText(row_pos.getContactType());
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
|
6) Add Data Resources
6.1) res/values/strings.xml
<resources>
<string name="app_name">MyListViewAdapterClickListener1</string>
<string name="action_settings">Settings</string>
<!-- Names -->
<string-array name="Member_names">
<item>Honda</item>
<item>Hyundai</item>
<item>Kia</item>
<item>Lada</item>
<item>Mercedes</item>
<item>Proton</item>
</string-array>
<!-- Pictures -->
<array name="profile_pics">
<item>@drawable/car_honda</item>
<item>@drawable/car_hyundai</item>
<item>@drawable/car_kia</item>
<item>@drawable/car_lada</item>
<item>@drawable/car_mercedes</item>
<item>@drawable/car_proton</item>
</array>
<!-- Status -->
<string-array name="statues">
<item>Blogging</item>
<item>Available</item>
<item>Busy</item>
<item>In a meeting</item>
<item>At work</item>
<item>At gym</item>
</string-array>
<!-- contact type -->
<string-array name="contactType">
<item>Mobile</item>
<item>Work</item>
<item>Home</item>
<item>Work</item>
<item>Mobile</item>
<item>Mobile</item>
</string-array>
</resources>
|
6.2) res/drawable/...
Download MyListViewAdapterClickListener1-drawable.zip and place the images into res/drawable folder.
7) Edit MainActivity.java
package com.notarazi.mylistviewadapterclicklistener1;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implementsAdapterView.OnItemClickListener {
String[] member_names;
TypedArray profile_pics;
String[] statues;
String[] contactType;
List<Contact> rowItems;
ListView mylistview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setListViewData();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
private void setListViewData() {
rowItems = new ArrayList<Contact>();
member_names = getResources().getStringArray(R.array.Member_names);
profile_pics = getResources().obtainTypedArray(R.array.profile_pics);
statues = getResources().getStringArray(R.array.statues);
contactType = getResources().getStringArray(R.array.contactType);
for (int i = 0; i < member_names.length; i++) {
Contact item = new Contact(member_names[i],
profile_pics.getResourceId(i, -1), statues[i],
contactType[i]);
rowItems.add(item);
}
mylistview = (ListView) findViewById(R.id.list);
ContactAdapter adapter = new ContactAdapter(this, rowItems);
mylistview.setAdapter(adapter);
profile_pics.recycle();
mylistview.setOnItemClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String member_name = rowItems.get(position).getMember_name();
Toast.makeText(getApplicationContext(), "" + member_name,
Toast.LENGTH_SHORT).show();
}
}
|
OUTCOME.
DOWNLOAD
REFERENCES
READING MATERIALS
TUTORIALS
FURTHER READING
.
No comments:
Post a Comment