Minggu, 05 Januari 2014

Create custom ListView of custom object

This example create ListView using custom adapter for custom object.

Create custom ListView of custom object
Create custom ListView of custom object

package com.example.androidcustomlistview;

import java.util.ArrayList;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity {

private class MyObject{
private int number;
private String name;

MyObject(int num, String nam){
number = num;
name = nam;
}

public int getNumber(){
return number;
}

public String getName(){
return name;
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView)findViewById(R.id.listview);

//Init ArrayList of MyObject
ArrayList<MyObject> myArrayList = new ArrayList<MyObject>();
myArrayList.add(new MyObject(0, "Sunday"));
myArrayList.add(new MyObject(1, "Monday"));
myArrayList.add(new MyObject(2, "Tuesday"));
myArrayList.add(new MyObject(3, "Wednesday"));
myArrayList.add(new MyObject(4, "Thursday"));
myArrayList.add(new MyObject(5, "Friday"));
myArrayList.add(new MyObject(6, "Saturday"));

MyAdapter myAdapter = new MyAdapter(this, myArrayList);
listView.setAdapter(myAdapter);

}

private class MyAdapter extends BaseAdapter {

private ArrayList<MyObject> myList;

private Activity parentActivity;
private LayoutInflater inflater;

public MyAdapter(Activity parent, ArrayList<MyObject> l) {
parentActivity = parent;
myList=l;
inflater = (LayoutInflater)parentActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return myList.size();
}

@Override
public Object getItem(int position) {
return myList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView (int position, View convertView,
ViewGroup parent) {
View view = convertView;
if(convertView==null)
view = inflater.inflate(R.layout.row, null);

TextView text1 = (TextView)view.findViewById(R.id.text1);
TextView text2 = (TextView)view.findViewById(R.id.text2);
MyObject myObj = myList.get(position);
text1.setText(String.valueOf(myObj.getNumber()));
text2.setText(myObj.getName());
return view;
}
}
}

row.xml, define the layout of individual row in the list.
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"/>

</LinearLayout>

Main layout
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com" />
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

Next: Implement OnItemClickListener for custom ListView

0 komentar:

Posting Komentar

Copyright © 2012 Codding News All Right Reserved