main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:stretchColumns="*" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/tbl">
<TableRow>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/icon" />
<TextView android:layout_width="wrap_content" android:id="@+id/name_txt"
android:layout_height="wrap_content" android:text="Profile Name" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="100dp" android:gravity="center"
android:layout_height="wrap_content" android:text="Price"
android:id="@+id/price_txt" />
<TextView android:layout_width="100dp" android:gravity="center"
android:layout_height="wrap_content" android:text="Country"
android:id="@+id/country_txt"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- <RatingBar android:id="@+id/ratingBar1" android:fitsSystemWindows="false"
android:maxWidth="150dip" android:minHeight="23dip" android:maxHeight="25dip"
android:layout_height="40dp" android:layout_width="150dp" /> -->
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Time Slot"
android:layout_gravity="center_vertical|center_horizontal"
android:id="@+id/time" />
<RatingBar android:id="@+id/ratingBar" style="?android:attr/ratingBarStyleSmall"
android:layout_gravity="center_vertical|center_horizontal"
android:focusable="true" android:layout_width="wrap_content"
android:layout_height="wrap_content"></RatingBar>
</LinearLayout>
</TableRow>
</TableLayout>
clistview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/booklist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
</LinearLayout>
Customlistview.java
package com.incept.customlist;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class CustomlistviewActivity extends Activity {
static final ArrayList<HashMap<String,Object>> list =
new ArrayList<HashMap<String,Object>>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clistview);
ListView listview = (ListView)findViewById(R.id.booklist);
populateList();
listview.setAdapter(new myListAdapter(list,this));
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
private void populateList() {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("name", "Pramod");
map.put("price", "25$");
map.put("country", "India");
map.put("timeslot", "2");
map.put("rating", 3);
list.add(map);
map = new HashMap<String, Object>();
map.put("name", "Chakri");
map.put("price", "10$");
map.put("country", "India");
map.put("timeslot", "2");
map.put("rating", 1);
list.add(map);
map = new HashMap<String, Object>();
map.put("name", "Rajesh");
map.put("price", "50$");
map.put("country", "UK");
map.put("timeslot", "2");
map.put("rating", 5);
list.add(map);
map = new HashMap<String, Object>();
map.put("name", "Tom");
map.put("price", "100$");
map.put("country", "US");
map.put("timeslot", "2");
map.put("rating", 4);
list.add(map);
map = new HashMap<String, Object>();
map.put("name", "Pramod");
map.put("price", "50$");
map.put("country", "India");
map.put("timeslot", "2");
map.put("rating", 1);
list.add(map);
}
}
myListAdapter.java
package com.incept.customlist;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;
class myListAdapter extends BaseAdapter {
ArrayList<HashMap<String,Object>> Profiles;
LayoutInflater layoutinflator;
myListAdapter(ArrayList<HashMap<String,Object>> books,Context con)
{
Profiles=books;
layoutinflator=LayoutInflater.from(con);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Profiles.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return Profiles.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder mviewholder;
if(convertView==null){
convertView=layoutinflator.inflate(R.layout.main, null);
mviewholder = new ViewHolder();
mviewholder.v1=(TextView)convertView.findViewById(R.id.name_txt);
mviewholder.v2=(TextView)convertView.findViewById(R.id.price_txt);
mviewholder.v3=(TextView)convertView.findViewById(R.id.country_txt);
mviewholder.v4=(TextView)convertView.findViewById(R.id.time);
mviewholder.b1=(RatingBar)convertView.findViewById(R.id.ratingBar);
convertView.setTag(mviewholder);
}
else{
mviewholder=(ViewHolder) convertView.getTag();
}
mviewholder.v1.setText((String) Profiles.get(position).get("name"));
mviewholder.v2.setText((String) Profiles.get(position).get("price"));
mviewholder.v3.setText((String) Profiles.get(position).get("country"));
mviewholder.v4.setText((String) Profiles.get(position).get("timeslot"));
mviewholder.b1.setRating((Integer) Profiles.get(position).get("rating"));
return convertView;
}
}
ViewHolder.java
package com.incept.customlist;
import android.widget.RatingBar;
import android.widget.TextView;
public class ViewHolder {
TextView v1;
TextView v2;
TextView v3;
TextView v4;
RatingBar b1;
}
No comments:
Post a Comment