• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps ListView, ArrayAdapter, getView()

tunneling

Lurker
Mar 23, 2010
6
1
I guess it's not too long after one get's started with android that you realize your going to have to override the getView() method of an ArrayAdapter.

I've been struggling with this for hours, and so before I throw my phone against the wall.. can someone enlighten me?

I'm basically using this tutorial

Android Series: Custom ListView items and adapters | Software Passion

I've added some Log.i trying to determine what's going on. All I can figure out is that the getView() method is never getting called.

SoftwarePassionView
package com.softberries.lve;

import java.util.ArrayList;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class SoftwarePassionView extends ListActivity{

private ProgressDialog m_ProgressDialog = null;
private ArrayList<Order> m_orders = null;
private OrderAdapter m_adapter;
private Runnable viewOrders;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

m_orders = new ArrayList<Order>();
this.m_adapter = new OrderAdapter(this, R.layout.row, m_orders);
setListAdapter(this.m_adapter);

viewOrders = new Runnable(){
@Override
public void run() {
getOrders();
}
};
Thread thread = new Thread(null, viewOrders, "MagentoBackground");
thread.start();
m_ProgressDialog = ProgressDialog.show(SoftwarePassionView.this,
"Please wait...", "Retrieving data ...", true);
}

private Runnable returnRes = new Runnable() {

@Override
public void run() {
if(m_orders != null && m_orders.size() > 0){
m_adapter.notifyDataSetChanged();
for(int i=0;i<m_orders.size();i++)
m_adapter.add(m_orders.get(i));
Log.i("RUNNABLE:", " adding to adapter");
}
m_ProgressDialog.dismiss();
m_adapter.notifyDataSetChanged();
}
};
private void getOrders(){
try{
m_orders = new ArrayList<Order>();
Order o1 = new Order();
o1.setOrderName("SF services");
o1.setOrderStatus("Pending");
Order o2 = new Order();
o2.setOrderName("SF Advertisement");
o2.setOrderStatus("Completed");
m_orders.add(o1);
m_orders.add(o2);
Thread.sleep(5000);
Log.i("ARRAY", ""+ m_orders.size());
} catch (Exception e) {
Log.e("BACKGROUND_PROC", e.getMessage());
}
runOnUiThread(returnRes);
}
private class OrderAdapter extends ArrayAdapter<Order> {

private ArrayList<Order> items;

public OrderAdapter(Context context, int textViewResourceId, ArrayList<Order> items) {
super(context, textViewResourceId, items);
this.items = items;
Log.i("ORDERADAPTER:", " instanciated");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Log.i("ORDERADAPTER","executing getView");
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
Log.i("ORDERADAPTER","view is null");
}
Order o = items.get(position);
if (o != null) {
Log.i("ORDERADAPTER","object is NOT null");
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
if (tt != null) {
tt.setText("Name: "+ o.getOrderName()); }
if(bt != null){
bt.setText("Status: "+ o.getOrderStatus());
}
} else {
Log.i("ORDERADAPTER","object is NULL");
}
return v;
}
}
}
Order
package com.softberries.lve;

public class Order {

private String orderName;
private String orderStatus;

public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
public String getOrderStatus() {
return orderStatus;
}
public void setOrderStatus(String orderStatus) {
this.orderStatus = orderStatus;
}
}
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="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<LinearLayout
android:eek:rientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/toptext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/bottomtext"
android:singleLine="true"
android:ellipsize="marquee"
/>
</LinearLayout>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:eek:rientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/header_message"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/main_no_items"/>
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/main_no_items"/>
</LinearLayout>

Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.softberries.lve"
android:versionCode="1"
android:versionName="1.0">
<application android:debuggable="true" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SoftwarePassionView"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-sdk android:minSdkVersion="4" />

</manifest>
 
i guess that i should have mentioned that i have had success using "android.R.layout.simple_list_item_1". although i haven't tried to @Override the getView() to customize the display.

I still don't understand what's going wrong with the code i posted. I've zipped it up from the workspace if that makes it easier for someone to try.
 

Attachments

  • SoftwarePassionView.zip
    53.5 KB · Views: 192
Upvote 0
Check the main.xml file in the SoftwarePassionView project. If you make the following items wrap_content in the xml file you will see the list.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:eek:rientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/header_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/main_no_items"/>
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/main_no_items"/>
</LinearLayout>
 
Upvote 0
When the height was set android:layout_height="fill_parent"

in

<TextView
android:id="@+id/header_message"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/main_no_items"/>

it pushed the layout

<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

off the screen. Since the ListView was not visible android knew it would not be able to draw it on the screen so getView() method was never called to render the objects.
 
  • Like
Reactions: tunneling
Upvote 0
As far as I know the getView() method is called when the Views are being drawn to the context, maybe after notifyDataSetChanged() has been called to refresh the attached View.

By override that method you can modify the display of the list item ,e.g. if you want to change the image resource of an ImageView in your list item (if any) if a certain condition met.

We don't need to call the method explicitly though. Hope this will help you :D
 
Upvote 0
I guess it's not too long after one get's started with android that you realize your going to have to override the getView() method of an ArrayAdapter.

I've been struggling with this for hours, and so before I throw my phone against the wall.. can someone enlighten me?

I'm basically using this tutorial

Android Series: Custom ListView items and adapters | Software Passion

I've added some Log.i trying to determine what's going on. All I can figure out is that the getView() method is never getting called.

SoftwarePassionView
Order
row.xmlmain.xml

Manifest
thanks!Take a look!
 
Upvote 0
When the height was set android:layout_height="fill_parent"

in

<TextView
android:id="@+id/header_message"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/main_no_items"/>

it pushed the layout

<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

off the screen. Since the ListView was not visible android knew it would not be able to draw it on the screen so getView() method was never called to render the objects.

Wow, I struggled with this for a few days. that did the trick! Thank you!
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones