Skip to content

Instantly share code, notes, and snippets.

@ceosilvajr
Created September 25, 2015 16:15
Show Gist options
  • Save ceosilvajr/735fe68e26b75ffa3698 to your computer and use it in GitHub Desktop.
Save ceosilvajr/735fe68e26b75ffa3698 to your computer and use it in GitHub Desktop.
Sample ListView

Hi Aubrey! Please follow this steps for your ListView

  1. Create Item Object.
  2. Create ItemAdapter, this is the bridge between a ListView and the data that backs the list (Item).
  3. Create Layout container that holds the view of each item.

And now your good.

I created a simple activity to show you how to use this 3 components in your listview. Please check ItemListActivity.

<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"
tools:context="com.softwen.ashonlineshop.ItemListActivity">
<ListView
android:id="@+id/item_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.softwen.ashonlineshop.objects;
/**
* Created by ceosilvajr on 9/25/15.
*/
public class Item {
private String name;
private double price;
public Item(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
package com.softwen.ashonlineshop.adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.softwen.ashonlineshop.R;
import com.softwen.ashonlineshop.objects.Item;
import java.util.List;
/**
* Created by ceosilvajr on 9/25/15.
*/
public class ItemAdapter extends ArrayAdapter<Item> {
private Context mContext;
private List<Item> mItems;
public ItemAdapter(Context context, List<Item> objects) {
super(context, 0, objects);
this.mContext = context;
this.mItems = objects;
}
static class ViewHolder {
TextView tvName;
TextView tvPrice;
public ViewHolder(View view) {
tvName = (TextView) view.findViewById(R.id.item_name);
tvPrice = (TextView) view.findViewById(R.id.item_price);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder viewHolder;
if (convertView != null) {
viewHolder = (ViewHolder) view.getTag();
} else {
LayoutInflater layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.container_item,
parent, false);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
}
Item item = mItems.get(position);
viewHolder.tvName.setText(item.getName());
viewHolder.tvPrice.setText("" + item.getPrice());
return convertView;
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Item getItem(int position) {
return mItems.get(position);
}
}
package com.softwen.ashonlineshop;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import com.softwen.ashonlineshop.adapters.ItemAdapter;
import com.softwen.ashonlineshop.objects.Item;
import java.util.ArrayList;
import java.util.List;
public class ItemListActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
// Initialize itemadapter with this (or the context which is the activity itself)
// and the list of Item object in this example the dummyItem
ItemAdapter itemAdapter = new ItemAdapter(this, getDummyItem());
//Initialize listview object
ListView listView = (ListView) findViewById(R.id.item_list);
//use the itemadapter for listview adapter
listView.setAdapter(itemAdapter);
}
/*
* This is just a sample Lists of Item objects
**/
private List<Item> getDummyItem() {
List<Item> items = new ArrayList<Item>();
items.add(new Item("Item 1", 1000.00));
items.add(new Item("Item 2", 2000.00));
items.add(new Item("Item 3", 3000.00));
items.add(new Item("Item 4", 4000.00));
return items;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment