In this tutorial, I will show you an example in android using BaseAdapter. See codes below!
MainActivity.java
package com.example.activity;
import java.util.ArrayList;
import java.util.List;
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.ListView;
import android.widget.Toast;
import com.example.adapters.BaseAdapters;
import com.example.models.RowItem;
public class MainActivity extends Activity {
private List<RowItem> row_items;
private ListView list;
private String title[] = {"Dota", "Counter Strike", "Dota 2"};
private String description[] = {"Defence of the Ancient", "Fire in the hole", "Defence of the Ancient Upgrade"};
private int image[] = {R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
row_items = new ArrayList<RowItem>();
for(int x = 0; x <= title.length; x++){
RowItem item = new RowItem(image[x], title[x], description[x]);
row_items.add(item);
}
}catch(Exception e){
e.printStackTrace();
}
list = (ListView)findViewById(R.id.lv_display);
BaseAdapters adapter = new BaseAdapters(getApplicationContext(), row_items);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Item "+ arg2+1 + ":" + row_items.get(arg2), Toast.LENGTH_SHORT).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;
}
}
BaseAdapters.java
package com.example.adapters;
import java.util.List;
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 com.example.activity.R;
import com.example.models.RowItem;
public class BaseAdapters extends BaseAdapter{
private Context m_context;
private List<RowItem> list;
public BaseAdapters(Context context, List<RowItem> row_items){
this.m_context = context;
this.list = row_items;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return list.indexOf(getItem(position));
}
private class ViewHolder{
ImageView images;
TextView title, description;
}
@SuppressWarnings("null")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
try{
ViewHolder holder = null;
LayoutInflater inflates = (LayoutInflater) m_context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
convertView = inflates.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.images = (ImageView)convertView.findViewById(R.id.iv_image);
holder.title = (TextView)convertView.findViewById(R.id.tv_title);
holder.description = (TextView)convertView.findViewById(R.id.tv_description);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
RowItem row = (RowItem) getItem(position);
holder.images.setImageResource(row.getImage());
holder.title.setText(row.getTitle());
holder.description.setText(row.getDescription());
}catch(Exception e){
e.printStackTrace();
}
return convertView;
}
}
RowItem.java
package com.example.models;
public class RowItem {
private int m_image;
private String m_title;
private String m_description;
public RowItem(int image, String title, String description){
this.m_image = image;
this.m_title = title;
this.m_description = description;
}
public void setImage(int image){
this.m_image = image;
}
public int getImage(){
return this.m_image;
}
public void setTitle(String title){
this.m_title = title;
}
public String getTitle(){
return this.m_title;
}
public void setDescription(String description){
this.m_description = description;
}
public String getDescription(){
return this.m_description;
}
public String toString(){
return this.m_title +"\n"+this.m_description;
}
}
activity_main.xml
<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_display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/iv_image"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/iv_image"
android:textSize="20sp"
android:text="Title" />
<TextView
android:id="@+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_title"
android:layout_below="@+id/tv_title"
android:textSize="16sp"
android:text="Description" />
</RelativeLayout>
Output:
That's it. Hope this simple tutorial may help you. Happy Coding!
No comments:
Post a Comment