To create custom list view we need to follow following steps.
1) create new adapter we call it as CustomAdapter
public class CustomAdapter extends BaseAdapter {
Context context;
String[] UserName;
String[] UserStatus;
private static LayoutInflater inflater = null;
public CustomAdapter(Context context, String[] data,String[] data1) {
// TODO Auto-generated constructor stub
this.context = context;
this.UserName = data;
this.UserStatus=data1;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return UserName.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return UserName[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
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.custome_listview_item, null);
ImageView Uimg=(ImageView) vi.findViewById(R.id.imageView1);
Uimg.setImageResource(R.drawable.header_bg_brown);
TextView text = (TextView) vi.findViewById(R.id.ItemHeader);
text.setText(UserName[position]);
TextView textt = (TextView) vi.findViewById(R.id.Itemtext);
textt.setText(UserStatus[position]);
return vi;
}
}
2) then create layout file for custom list view row. we are calling it row because this layout will use to design each row of list.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/royalblue_color"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:nextFocusRight="@+id/ItemHeader"
/>
<TextView
android:id="@+id/ItemHeader"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:nextFocusLeft="@id/imageView1"
android:text="Header"
android:textColor="@color/white_color"
android:textSize="20dp" />
<TextView
android:id="@+id/Itemtext"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/ItemHeader"
android:text="Text"
android:textColor="@color/white_color"
android:textStyle="italic" />
</RelativeLayout>
</LinearLayout>
3) adding item to list on activity
ApplicationObjectsClass aoc = new ApplicationObjectsClass();
public class WebViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
Users user = new Users();
user.setUserID("Test1");
user.setUserID("Test2");
//as new user will come we will add them to this list
aoc.addOnlineUser(user);
int usercount = aoc.getOnlineUserList()
.size();
int count = 0;
String[] listItem = new String[usercount];
String[] listItemStatus = new String[usercount];
for (Users object2 : aoc.getOnlineUserList()) {
listItem[count] = object2.getUserID();
listItemStatus[count] = "Status of "+ object2.getUserID();
count++;
}
CustomAdapter custAdapter;
custAdapter = new CustomAdapter(getBaseContext(), listItem,
listItemStatus);
ListView listV = (ListView) findViewById(R.id.listView1);
listV.setAdapter(custAdapter);
listV.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(android.widget.AdapterView<?> adapter, View view, int position, long id) {
Object item = adapter.getAdapter().getItem(position);
Log.d("Get Item",item.toString());
Intent intent = new Intent(getBaseContext(),ChatActivity.class);
intent.putExtra("connectTo", item.toString());
startActivity(intent);
}});
final LinearLayout lm = (LinearLayout) findViewById(R.id.LinearLayout1);
}
}
No comments:
Post a Comment