I am using CustomArrayAdapter with ListView. But, even after setting setAdapter it is not working. Following are my code snippets:
PastOrders.java Fragment displaying listView
public class PastOrders extends Fragment {
private View view;
private ListView pastOrdersList;
private Context context;
private ArrayList<Order> orders;
public PastOrders() {
}
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = inflater.inflate(R.layout.fragment_pastorders, container, false);
context = getActivity();
pastOrdersList = (ListView) view.findViewById(R.id.pastOrdersList);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Order");
query.whereEqualTo("user_id", Vars.currentUser);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if(e != null) {
Log.e("TAG", e.getLocalizedMessage());
} else {
ArrayList<Order> orders = new ArrayList<Order>();
Log.i("TAG", objects.toString());
for (ParseObject object: objects) {
Order temp = new Order();
temp.orderId = object.getObjectId();
temp.date = object.getCreatedAt();
temp.status = object.getString("status");
Log.i("TAG", "orderID: "+ object.getObjectId() + " temp orderID"+ temp.orderId);
orders.add(temp);
Vars.pastOrders.add(object);
}
Log.i("TAG", "after for loop past orders: "+ orders.toString());
PastOrdersAdapter pastOrdersAdapter = new PastOrdersAdapter(getActivity(), R.layout.past_orders_row, orders);
pastOrdersList.setAdapter(pastOrdersAdapter);
}
}
});
return view;
}
#Override
public void onResume() {
super.onResume();
}
}
PastOrdersAdapter.java Custom Adapter
public class PastOrdersAdapter extends ArrayAdapter<Order> {
private Context context;
private ViewHolder holder;
//private ArrayList<Order> orders;
public PastOrdersAdapter(Context context, int resource, List<Order> orders) {
super(context, resource);
// this.orders = orders;
Log.i("TAG", "POA adapter called");
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i("TAG", "getView from POA");
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (convertView == null) {
convertView = inflater
.inflate(R.layout.past_orders_row, parent, false);
holder = new ViewHolder();
holder.orderId = (TextView) convertView.findViewById(R.id.orderId);
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.status = (TextView) convertView.findViewById(R.id.status);
holder.details = (ImageView) convertView.findViewById(R.id.details);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ParseObject order = Vars.pastOrders.get(position);
Log.i("TAG", "order id in adapter: "+ order.getObjectId());
if(order != null) {
holder.orderId.setText(order.getObjectId());
holder.date.setText(getFormattedDate(order.getCreatedAt()));
holder.status.setText(order.getString("status"));
if(order.getString("status").equals("Delivered")) {
holder.status.setTextColor(Color.GREEN);
} else if(order.getString("status").equals("Cancelled")) {
holder.status.setTextColor(Color.RED);
} else {
holder.status.setTextColor(Color.YELLOW);
}
}
return convertView;
}
static class ViewHolder {
TextView orderId;
TextView date;
TextView status;
ImageView details;
}
}
Order.java
public class Order {
public String orderId;
public Date date;
public String status;
}
fragment_pastorders.xml Layout File for Fragment showing ListView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/pastOrdersList"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
past_orders_row.xml Layout file for each row of listview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="150dp"
android:orientation="vertical"
android:id="#+id/LinearLayout01">
<TextView
android:id="#+id/orderId"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/date"
android:textStyle="italic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/status"/>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_next_item"
android:layout_toRightOf="#id/LinearLayout01"
android:layout_centerVertical="true"
android:id="#+id/details"/>
</RelativeLayout>
I have tried printing getCount() as well and it is non-zero. I have tried everything. Kindly help
Thanks,
Arpit
In the constructor of your custom adapter, your forgot to pass your orders parameter to the parent constructor. So your adapter does not know what to display.
You are passing empty list orders to you adapter. And then in adapter you are using different list: Vars.pastOrders.
So your adapter has empty list.
Replace this:
PastOrdersAdapter pastOrdersAdapter =
new PastOrdersAdapter(getActivity(), R.layout.past_orders_row, orders);
with this:
PastOrdersAdapter pastOrdersAdapter =
new PastOrdersAdapter(getActivity(), R.layout.past_orders_row, Vars.pastOrders);
Related
I'm trying to create simple list of Objects which i created
the objects are ShopListItem, this is the code of the class:
public class ShopListItem {
private String name;
private double price;
public ShopListItem(String name , double price)
{
this.name = name;
this.price = price;
}
public double getPrice() {
return price;
}
public String getName() {
return name;
}
}
this is the ArrayAdatpter class:
public class shopItemAdapter extends ArrayAdapter<ShopListItem> {
public shopItemAdapter(Context context, ArrayList<ShopListItem> items){
super(context, 0, items);
}
public View getView(int position, View convertView, ViewGroup parent) {
ShopListItem item = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.shop_item_row, parent, false);
}
TextView name = (TextView) convertView.findViewById(R.id.itemName);
TextView price = (TextView) convertView.findViewById(R.id.itemPrice);
name.setText(item.getName());
price.setText(String.valueOf(item.getPrice()));
return convertView;
}
}
and this is my activity which is supposed to display this list:
public class check extends Activity {
ListView items = (ListView)findViewById(R.id.items);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_q);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
ShopListItem item1 = new ShopListItem("milk" , 5.9);
ShopListItem item2 = new ShopListItem("bamba" , 4);
ArrayList<ShopListItem> list = new ArrayList<ShopListItem>();
list.add(item1);
list.add(item2);
shopItemAdapter adapter = new shopItemAdapter(this , list);
items.setAdapter(adapter);
}
}
I open the activity from another activity when user clicks a button, this is the on click method:
public void openList(View view){
Intent intent = new Intent(HomeActivity.this , check.class);
this.startActivity(intent);
}
shop_item_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF9F9F9"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/itemName"
android:layout_weight="0.61"
android:layout_margin="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/itemPrice"
android:layout_margin="5dp" />
</LinearLayout>
and content_check.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="atoa.roomates.QA"
tools:showIn="#layout/activity_q">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/items"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
thank you!
You are accessing view ListView items = (ListView)findViewById(R.id.items); before onCreate(). So move that line in onCreate method after setContentView().
initialize your list in setContentView instead of top
like following
ListView items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_demo);
items = (ListView) findViewById(R.id.items);
after change this work fine.
public class shopItemAdapter extends ArrayAdapter<ShopListItem> {
private Context mContext;
private List<ShopListItem> mListShop;
public shopItemAdapter(Context context, int resource, ArrayList<ShopListItem> items){
super(context, resource, items);
this.mContext= context;
this.mListShop = items;
}
#Override
public int getCount() {
return mListShop.size();
}
#Override
public ShopListItem getItem(int position) {
return mListShop.get(position);
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.shop_item_row, null);
holder = new ViewHolder();
holder.mTextPrice = (TextView) view.findViewById(R.id.itemPrice);
holder.mTextName = (TextView) view.findViewById(R.id.itemName);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
ShopListItem item = getItem(position);
holder.mTextName.setText(item.getName());
holder.mTextPrice.setText(item.getPrice());
return view;
}
public class ViewHolder {
public TextView mTextPrice;
public TextView mTextName;
}
}
In your Activity
public class check extends Activity {
ListView items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_q);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
items = (ListView)findViewById(R.id.items);
ShopListItem item1 = new ShopListItem("milk" , 5.9);
ShopListItem item2 = new ShopListItem("bamba" , 4);
ArrayList<ShopListItem> list = new ArrayList<ShopListItem>();
list.add(item1);
list.add(item2);
shopItemAdapter adapter = new shopItemAdapter(this , R.layout.shop_item_row, list);
items.setAdapter(adapter);
}
}
Try it!!!!
I have a custom row item xml and a Activity xml which has a button, a textbox and a listview. Can anybody give me a very simple example on how this is done? I know i will need a adapter (what it extends i dont know) and update it from the button
I have tried getting the list view updated from the button with the editview text but cant get the adapter code to be hit. I am looking at base adapters now but cant help but feel this shouldn't be complicated and need so many override methods.
Activity class
public class InvitePlayers_Activity extends Activity {
ListViewAdapter emailAdapter = null;
ImageView imgView_mail;
ImageView imgView_confirm;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); //remove title bar
setContentView(R.layout.activity_inviteplayers);
//Generate list View from ArrayList
displayListView();
}
private void displayListView() {
//assign controls
final ListView listView = (ListView) findViewById(R.id.listView_invitePlayers);
imgView_mail = (ImageView)findViewById(R.id.imgView_mail);
//Test data
ArrayList<String> inviteNew = new ArrayList<String>();
final ArrayList<ArrayList<String>> inviteList = new ArrayList<ArrayList<String>>();
emailAdapter = new ListViewAdapter(this,inviteList);
listView.setAdapter(emailAdapter);
// Assign adapter to ListView
listView.setTextFilterEnabled(true);
//Edit listeners
imgView_mail.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
//variables
final String enteredMail = "testListViewEntry";
final ArrayList<ArrayList<String>> inviteList = new ArrayList<ArrayList<String>>();
ArrayList<String> invite = new ArrayList<String>();
invite.add(0, enteredMail);//add first email
invite.add(1,"icon_invitestatussent.png"); //add first status icon
inviteList.add(invite);
emailAdapter.notifyDataSetChanged();
listView.setAdapter(emailAdapter);
}
});
}
}
Adapter class
public class ListViewAdapter extends BaseAdapter {
private Activity context;
ArrayList<ArrayList<String>> inviteDetails = new ArrayList<ArrayList<String>>();
public ListViewAdapter(Activity context, ArrayList<ArrayList<String>> inviteDetails ) {
this.inviteDetails = inviteDetails;
this.context = context;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
public View getView(int position, View view, ViewGroup parent){
//Inflater
LayoutInflater inflater = context.getLayoutInflater();
//get row view
if (view == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
view = mInflater.inflate(R.layout.list_item_email, null);
}
//assign controls
final TextView textView_playerEmail = (TextView) view.findViewById(R.id.textView_playerEmail);
ImageView imgView_inviteStatus = (ImageView) view.findViewById(R.id.imgView_inviteStatus);
//Assign control values that are dynamic
textView_playerEmail.setText(inviteDetails.get(position).get(0));
imgView_inviteStatus.setImageResource(R.drawable.icon_invitestatussent);
return view;
}
public ArrayList<ArrayList<String>> addInvite(ArrayList<String> inviteDetails, ArrayList<ArrayList<String>> currentInvites)
{
currentInvites.add(inviteDetails);
return currentInvites;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
Custom row xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:id="#+id/textView_playerEmail"
android:textColor="#color/white"
android:text="item1">
</TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgView_inviteStatus" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgView_remove"
android:src="#drawable/btn_cancel" />
Activity layout
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:gravity="left|center">
<ImageView
android:layout_width="45dp"
android:layout_height="34dp"
android:id="#+id/imgView_mail"
android:src="#drawable/btn_mail"
android:layout_weight="0.22"
android:padding="3dp" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView_invitePlayers"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="#+id/btn_confirm"
android:src="#drawable/btn_confirm"
android:clickable="false"
android:adjustViewBounds="true"
android:layout_gravity="center_horizontal"
android:padding="2dp"
android:layout_weight="1" />
</LinearLayout>
From reading the description of the question I think you need to set custom adapter to show the elements in you listview. This is how you set the custom adapter for the listview:
public class Notes_list extends BaseAdapter
{
private Context mContext;
private ArrayList<String> id;
private ArrayList<String> firstName;
private ArrayList<String> lastName;
public Notes_list(Context c, ArrayList<String> id,ArrayList<String> fname, ArrayList<String> lname)
{
this.mContext = c;
this.id = id;
this.firstName = fname;
this.lastName = lname;
}
public int getCount() {
return id.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int pos, View child, ViewGroup arg2) {
Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.custom_row, null);
mHolder = new Holder();
// mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
mHolder.txt_fName = (TextView) child.findViewById(R.id.txt_fName);
mHolder.txt_lName = (TextView) child.findViewById(R.id.txt_lName);
child.setTag(mHolder);
} else
{
mHolder = (Holder) child.getTag();
}
// mHolder.txt_id.setText(id.get(pos));
mHolder.txt_fName.setText(firstName.get(pos));
mHolder.txt_lName.setText(lastName.get(pos));
return child;
}
public class Holder
{
// TextView txt_id;
TextView txt_fName;
TextView txt_lName;
}
}
now in your main activity where you have to call the list enter code like this:
private ListView userList;
private ArrayList<Employee> itemsList = new ArrayList<modelName>();
userList = (ListView) findViewById(R.id.List);
UserList disadpt = new UserList(MainActivity.this, itemsList);
userList.setAdapter(disadpt);
Hope it helps...
I have a list view which displays only one record always, I've traced my program and find that there are 3 records in my arrayList which I'm passing to the listView adapter. After tracing into getView of list view I find that It executes more that 3 times with position of 0!
I have no idea what's the problem, please help me if you can.
activity_news.xml:
<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"
android:background="#color/background"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".News" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/lvNews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" />
</ScrollView>
</RelativeLayout>
this is row template for listView: news_row.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="wrap_content"
android:background="#drawable/message_row_style"
android:layout_gravity="right"
android:gravity="right"
android:orientation="vertical" >
<LinearLayout
android:layout_marginTop="50dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/txtTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#color/dark_purple"
android:layout_gravity="right"
android:gravity="right"
android:background="#color/red"
/>
<TextView
android:id="#+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/dark_purple"
android:layout_gravity="right"
android:background="#color/blue"
android:gravity="right" />
</LinearLayout>
</LinearLayout>
newsAdapter.java:
public class NewsAdapter extends BaseAdapter{
private Context context;
private ArrayList<News> list;
private Activity activity;
public NewsAdapter(Context c,Activity activity,ArrayList<News> l) {
Log.d("Ehsan", "News Adapter Constructor");
context = c;
list = l;
this.activity = activity;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int i) {
return list.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.news_row, parent,false);
TextView title = (TextView) row.findViewById(R.id.txtTitle);
TextView date = (TextView) row.findViewById(R.id.txtDate);
News temp = list.get(position);
title.setText(temp.getTitle());
date.setText(temp.getsDate());
title.setTag(temp.getid());
title.setOnClickListener(onClickListener);
return row;
}
private OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
String date = null,title = null,description = null;
int id=1;
Intent i = null;
News news = new News(activity);
news.load(id);
date = news.getsDate()+" - ";
title = news.getTitle();
description = news.getDescription();
i = new Intent(activity,ShowInfo.class);
i.putExtra("date", date);
i.putExtra("title", title);
i.putExtra("description", description);
activity.startActivity(i);
activity.finish();
}
};
}
Do following changes in your getView
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
ViewHolder holder=new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.news_row, parent,false);
TextView title = (TextView) row.findViewById(R.id.txtTitle);
TextView date = (TextView) row.findViewById(R.id.txtDate);
convertView.setTag(holder);
}
ViewHolder hold=(ViewHolder)convertView.getTag();
News temp = list.get(position);
hold.title.setText(temp.getTitle());
hold.date.setText(temp.getsDate());
title.setOnClickListener(onClickListener);
return row;
}
add this class in your adapter file
static class ViewHolder
{
TextView title;
TextView date;
}
You should call super in the constructor of your adapter change your adapter as following:
public class NewsAdapter extends ArrayAdapter<News>{
private Context context;
private ArrayList<News> list;
private Activity activity;
public NewsAdapter(Context c,Activity activity,ArrayList<News> l) {
super(c,-1,l);//<-- **pass your item list to super**
Log.d("Ehsan", "News Adapter Constructor");
context = c;
list = l;
this.activity = activity;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int i) {
return list.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.news_row, parent,false);
TextView title = (TextView) row.findViewById(R.id.txtTitle);
TextView date = (TextView) row.findViewById(R.id.txtDate);
News temp = list.get(position);
title.setText(temp.getTitle());
date.setText(temp.getsDate());
title.setTag(temp.getid());
title.setOnClickListener(onClickListener);
return row;
}
private OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
String date = null,title = null,description = null;
int id=1;
Intent i = null;
News news = new News(activity);
news.load(id);
date = news.getsDate()+" - ";
title = news.getTitle();
description = news.getDescription();
i = new Intent(activity,ShowInfo.class);
i.putExtra("date", date);
i.putExtra("title", title);
i.putExtra("description", description);
activity.startActivity(i);
activity.finish();
}
};
}
I created this class called GeoArea, which is suppose to store "Geographical Area" that have children Geographical Areas, this is fairly strait foward:
public class GeoArea {
public String id;
public String name;
public List<GeoArea> subGeoAreas;
public GeoArea parentGeoArea;
public GeoArea(String id) {
this.id = id;
name = id;
subGeoAreas = new LinkedList<GeoArea>();
}
#Override
public String toString() {
return name;
}
}
I have created the following Layout to render it on Android, the idea here is to for each GeoArea to recursively render it self and then it's children GeoArea in a listView:
//layout_geo_area.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txtGeoAreaName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:text="Geo Area Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#+id/listViewChildGeoAreas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtGeoAreaName"
android:gravity="left" >
</ListView>
</RelativeLayout>
This is my adapter I created for GeoArea to be displayed in a listView:
public class AdapterGeoArea extends ArrayAdapter<GeoArea>{
private ArrayList<GeoArea> _myGeoArea;
private Context _myContext;
LayoutInflater _inflater;
public AdapterGeoArea(Context context, ArrayList<GeoArea> myGeoArea) {
super(context, 0, myGeoArea);
_myGeoArea = myGeoArea;
_inflater = LayoutInflater.from(context);
_myContext = context;
}
public int getCount() {
return _myGeoArea.size();
}
public View getView(int position, View convertView, ViewGroup parent) {
GeoAreaLayoutHolder holder;
if (convertView == null) {
convertView = _inflater.inflate(R.layout.layout_geo_area,parent,false);
holder = new GeoAreaLayoutHolder();
holder.txtGeoAreaName = (TextView)convertView.findViewById(R.id.txtGeoAreaName);
holder.txtGeoAreaName.setTag(convertView);
holder.listViewChildGeoAreas = (ListView)convertView.findViewById(R.id.listViewChildGeoAreas);
holder.listViewChildGeoAreas.setTag(convertView);
} else {
holder = (GeoAreaLayoutHolder) convertView.getTag();
}
GeoArea curGeoArea = _myGeoArea.get(position);
holder.txtGeoAreaName.setText(curGeoArea.name);
if(curGeoArea.subGeoAreas.size()>0){
ArrayList<GeoArea> testList = new ArrayList<GeoArea>();
AdapterGeoArea adapter = new AdapterGeoArea(_myContext, testList);
for(GeoArea childGeoArea:curGeoArea.subGeoAreas){
testList.add(childGeoArea);
}
holder.listViewChildGeoAreas.setAdapter(adapter);
}
return convertView;
}
static class GeoAreaLayoutHolder {
public TextView txtGeoAreaName;
public ListView listViewChildGeoAreas;
}
}
And here is my Activity that I am using to set it all up:
public class ActivityGeoAreas extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_geo_area);
GeoArea.searchTerm = "Bar & Grill";
GeoArea torontoArea = new GeoArea("cityOfToronto");
ArrayList<GeoArea> testList = new ArrayList<GeoArea>();
testList.add(torontoArea);
AdapterGeoArea adapter = new AdapterGeoArea(this, testList);
ListView lv = (ListView) findViewById(R.id.listViewChildGeoAreas);
lv.setAdapter(adapter);
}
}
When I try to run it, I get the error nullPointerException on the line:
holder.txtGeoAreaName.setText(curGeoArea.name);
What am I doing wrong?
You may want to check ExpandableListView may suit your needs better
http://developer.android.com/reference/android/widget/ExpandableListView.html
An example # http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
Continuing from my previous answer to your question ( i though that solved your problem)
To display just name in your listview
list_row.xml // this is the layout with textview to be inflated in getView.
Each row will have textview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textGeoArea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Choose Area"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
layout_geo_area.xml // with only listview no textview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listViewChildGeoAreas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="left" >
</ListView>
</RelativeLayout>
Now your adapter class
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row,parent,false);
// inflate list_row.xml with textview
holder = new ViewHolder();
holder.tv = (TextView)convertView.findViewById(R.id.textGeoArea);
holder.setTag(convertView);
}
else {
holder = (ViewHolder) convertView.getTag();
}
GeoArea curGeoArea = _myGeoArea.get(position);
holder.tv.setText(curGeoArea.name);
return convertView;
}
static class ViewHolder // use a view holder for smooth scrolling an performance
{
TextView tv;
}
You have a custom adapter.
public class AdapterGeoArea extends ArrayAdapter<GeoArea>{
Now you set the adapter to listview like below
AdapterGeoArea adapter = new AdapterGeoArea(this, testList);
ListView lv = (ListView) findViewById(R.id.listViewChildGeoAreas);
lv.setAdapter(adapter);
So why do you need the below. remove these
if(curGeoArea.subGeoAreas.size()>0){
ArrayList<GeoArea> testList = new ArrayList<GeoArea>();
AdapterGeoArea adapter = new AdapterGeoArea(_myContext, testList);
for(GeoArea childGeoArea:curGeoArea.subGeoAreas){
testList.add(childGeoArea);
}
holder.listViewChildGeoAreas.setAdapter(adapter);
I am new in android app dev and I am trying to create a ListFragment consisting of a TextView and a ListView. I want to try and populate the ListView with data from a custom class and thus created a custom ArrayAdapter. My app runs but doesn't show anything in the list and I can't figure out why not. I have logged the data and it is present in the holder and the text seems to be set but something is wrong.
row_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/TextViewStation"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="My Application" />
<TextView
android:id="#+id/TextViewType"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout" />
</LinearLayout>
</RelativeLayout>
list_layout.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"
android:orientation="vertical" >
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No items to display." />
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
SimpleArrayAdapter.java
public class SimpleArrayAdapter extends ArrayAdapter<Warning> {
private final Context context;
private final List<Warning> objects;
public SimpleArrayAdapter(Context context, int resource,
List<Warning> objects) {
super(context, resource);
this.context = context;
this.objects = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = new ViewHolder();
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row_layout, parent, false);
holder.v1 = (TextView) row.findViewById(R.id.TextViewStation);
holder.v2 = (TextView) row.findViewById(R.id.TextViewType);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final Warning warning = objects.get(position);
if (warning != null) {
holder.v1.setText(warning.getStation());
holder.v2.setText(warning.getType());
// Log.d("Adapter", "holder.v1.getText(): " + holder.v1.getText());
}
return row;
}
public static class ViewHolder {
TextView v1; // view1
TextView v2; // view2
}
#Override
public int getCount() {
return objects.size();
}
#Override
public Warning getItem(int position) {
// TODO Auto-generated method stub
return objects.get(position);
}
}
List_View.java - the ListFragment
public class List_View extends ListFragment {
List<Warning> items = new ArrayList<Warning>();
Gson gson = new Gson();
private SimpleArrayAdapter adapter;
private ListView mListView;
// #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ---- Get data!
JsonElement je = new JsonParser().parse(result);
JsonObject jo = je.getAsJsonObject();
JsonArray ja = jo.getAsJsonArray("warnings");
for (int i = 0; i < ja.size(); i++) {
Warning war = new Warning();
war = gson.fromJson(ja.get(i).getAsJsonObject(), Warning.class);
items.add(war);
}
// ----
}
// #Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater
.inflate(R.layout.list_layout, container, false);
// RESTManager manager = new RESTManager();
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d("Adapter", "items.size(): "+ items.size());
adapter = new SimpleArrayAdapter(getActivity(), R.layout.row_layout,
items);
setListAdapter(adapter);
}
}
In SimpleArrayAdapter, me logging the result from holder.v1.getText() returns correct value but still nothing is showing in the ListView in the app.
Try this :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
TextView station, type ;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row_layout, parent, false);
station = (TextView) row.findViewById(R.id.TextViewStation);
type = (TextView) row.findViewById(R.id.TextViewType);
}
Warning warning = objects.get(position);
if (warning != null) {
station.setText(warning.getStation());
type.setText(warning.getType());
// Log.d("Adapter", "holder.v1.getText(): " + holder.v1.getText());
}
return row;
}
I found the problem. Stupidly for some reason I had android:layout_width="0dip" for the textviews........ changed to android:layout_width="fill_parent" and boom, there is my data!