Different Images with Different text In Android ListView(Part Two)

Let’s get start by creating a project in Android ADT.

1. Create a new project by going to File ⇒ New Android Application. Fill all the details  As Application Name:DifferentIconListView,Project Name:DifferentIconListView,Package Name:com.Test.differenticonlistview  and click on Next button to finish .

2 .Once the project is created open your activity_main.xml (res/layout) and paste the following code.

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
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>

Create another layout under under res/layout as row.xml.

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
tools:ignore=”SpUsage,HardcodedText” >

<ImageView
android:id=”@+id/icon”
android:layout_width=”80dp”
android:layout_height=”80dp”
android:contentDescription=”image”
android:paddingLeft=”10dp”
android:paddingRight=”10dp” />

<TextView
android:id=”@+id/title”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_toRightOf=”@+id/icon”
android:paddingBottom=”10dp”
android:textColor=”#CC0033″
android:textSize=”16dp” />

<TextView
android:id=”@+id/desc”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/title”
android:layout_toRightOf=”@+id/icon”
android:paddingLeft=”10dp”
android:textColor=”#3399FF”
android:textSize=”14dp” />

</RelativeLayout>

3.Open MainActivity.java class and paste following code.

package com.Test.differenticonlistview ;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements
OnItemClickListener {

public static final String[] titles = new String[] { “Sunday”,
“Monday”, “Tuseday”, “Thursday”,”Friday”,”Satday” };

public static final String[] descriptions = new String[] {
“Today is Sunday”,
“Today is Monday”, “Today is Tuseday”,
“Today is Thursday”,”Today is Friday”,”Today is Satday” };

public static final Integer[] images = { R.drawable.icon_add,
R.drawable.ic_launcher, R.drawable.icon_add, R.drawable.ic_launcher,R.drawable.icon_add,R.drawable.ic_launcher };

ListView listView;
List<RowItem> rowItems;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {

RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
rowItems.add(item);
}

listView = (ListView) findViewById(R.id.list);
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this,
R.layout.row, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast toast = Toast.makeText(getApplicationContext(),
“Item ” + (position + 1) + “: ” + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}

4.Create a MyPerformanceArrayAdapter.java class and paste following code.

Here we are creating our own adapter class by simply extending this class to ArrayAdapter.

package com.Test.differenticonlistview;

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.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MySimpleArrayAdapter extends ArrayAdapter<RowItem> {

Context context;
List<RowItem> items;

public MySimpleArrayAdapter(Context context, int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
this.items = items;
}

public View getView(int position, View convertView, ViewGroup parent) {

RowItem rowItem = getItem(position);

LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.row, null);
TextView   txtDesc = (TextView) convertView.findViewById(R.id.desc);
TextView   txtTitle = (TextView) convertView.findViewById(R.id.title);
ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);

txtDesc.setText(rowItem.getDesc());
txtTitle.setText(rowItem.getTitle());
imageView.setImageResource(rowItem.getImageId());

return convertView;
}
}

5. Creat a RowItem.class and paste following code .

package com.Test.differenticonlistview;

public class RowItem {

private int imageId;
private String title;
private String desc;

public RowItem(int imageId, String title, String desc) {
this.imageId = imageId;
this.title = title;
this.desc = desc;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return title + “\n” + desc;
}

}

6.Finally run your project by right clicking on your project folder ⇒ Run As ⇒ 1 Android Application.

2013-07-24_19-30-38

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s