Austin Musice
Austin Musice

Reputation: 553

Custom ListAdapter Never Calling GetView

I've been trying to figure out what is going on with this for a little while, maybe I have been staring at it too long... I feel like it's something simple.

Here is my ListActivity class

    public class ResultsListActivity extends ListActivity {
    private ArrayList<Result> results = new ArrayList<Result>();
    private ListView list;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sp_history);
        //set up list
        list = new ListView(this);

    }

    @Override
    protected void onResume()
    {
        super.onResume();
        results = openAndQueryDatabase();
        displayResultList();
    }
    private void displayResultList() {


        list.setAdapter(new HistoryArrayAdapter(this.getBaseContext(), results));

    }
    private ArrayList<Result> openAndQueryDatabase() {
        DatabaseHandler dbh = new DatabaseHandler(getBaseContext());
        ArrayList<Result> toReturn = dbh.getAllResults();
        return toReturn;

    }//open and query database

    public int getCount() {
        return null == results ? 0 : results.size();
    }

    }

Here is my ListAdapterClass

    public class HistoryArrayAdapter extends ArrayAdapter<Result> {


  private final Context context;
  private final ArrayList<Result> values;
  private int layoutId;

  public HistoryArrayAdapter(Context context, ArrayList<Result> results) {
    super(context, R.layout.history_row, results);
    this.context = context;
    this.layoutId = R.layout.history_row;
    this.values = results;
  }

  @Override
    public int getCount() {
        return values.size();// more than zero
    }

  @Override
    public Result getItem(int position) {

        return values.get(position);// may be in your case
    }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      if(convertView==null)
      {
                      // inflate the layout
                      LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                      convertView = inflater.inflate(layoutId, parent, false);
      }// if convertView == null

    //set up our views
    TextView uploadSpeed = (TextView) convertView.findViewById(R.id.upload_speed_history);
    TextView downloadSpeed = (TextView) convertView.findViewById(R.id.download_speed_history);
    TextView pingTime = (TextView) convertView.findViewById(R.id.ping_history_text);
    TextView networkName = (TextView) convertView.findViewById(R.id.history_network_name);
    TextView dateTime = (TextView) convertView.findViewById(R.id.history_dateTime);
    ImageView imageView = (ImageView) convertView.findViewById(R.id.history_connection);

    //set up our values
    Result row = values.get(position);
    uploadSpeed.setText(row.getUpload());
    downloadSpeed.setText(row.getDownload());
    pingTime.setText(row.getPingTime());
    networkName.setText(row.getNetworkName());
    dateTime.setText(row.getDateTime());
    if(row.getNetworkType() == "NETWORK_WIFI")
        imageView.setImageResource(R.drawable.ico_wifi);
    else
        imageView.setImageResource(R.drawable.ico_cell);

    return convertView;
  }//getView

     } 

Here is the .xml for the ListView

     <?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">

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp" />

 </LinearLayout>

And Here is the Row Layout

     <?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="wrap_content"
     android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/realtive_line_1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/history_network_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="26dp"
        android:layout_toRightOf="@+id/imageView1"
        android:text="TextView" />

    <ImageView
        android:id="@+id/history_connection"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft = "20dip"
        android:layout_marginTop = "5dip"
        android:src="@drawable/ico_wifi" />

    <TextView
        android:id="@+id/history_dateTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="65dp"
        android:layout_below="@+id/network_name"
        android:text="2013-08-15 12:35:00 PM" 
        android:textSize="10sp" />

</RelativeLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_below="@+id/realtive_line_1"
    >

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="15dip"
        android:layout_height="15dip"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="23dp"
        android:layout_marginTop = "15dip"
        android:src="@drawable/ico_ping" />

    <TextView
        android:id="@+id/ping_history_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="25dp"
        android:layout_toRightOf="@+id/imageView2"
        android:text="30 ms"
        android:textSize = "15sp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/results_icon_up"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ico_up"
        android:layout_marginRight = "40dip" />

    <ImageView
        android:id="@+id/results_icon_down"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_alignParentRight="true"
        android:layout_marginTop="5dip"
        android:layout_below ="@+id/results_icon_up"
        android:src="@drawable/ico_down"
        android:layout_marginRight = "40dip" />

    <TextView
        android:id="@+id/upload_speed_history"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/results_icon_down"
        android:layout_marginRight="15dp"
        android:layout_toLeftOf="@+id/results_icon_up"
        android:text="4.26 Mbps"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/download_speed_history"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/upload_speed_history"
        android:layout_alignTop="@+id/results_icon_down"
        android:text="24.56 Mbps"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="20dip"
    android:layout_height="20dip"
    android:layout_alignParentRight ="true"
    android:layout_centerVertical="true"
    android:src="@drawable/arrow" />

 </RelativeLayout>

Things that I do know... I do have results. I can see it in the debugger and they are populated fine. I know I have a few things that look like unnecessary methods, there is work to be done there to make sure the app isn't in the middle of a process before it populates the list.

Upvotes: 0

Views: 180

Answers (2)

d3m0li5h3r
d3m0li5h3r

Reputation: 1967

AS you've stated that you are using Asynchronous call to get the Database results... even though while debugging you are getting the results, chances are that while actually running, the results might be getting after the adapter has been set in the following line hence the length of results in adapter is 0, so your getView() isn't getting a callback.

Either can have a Custom Listener that will get a call back when the results have been fetched and then you can set the adapter to the listview OR another solution might be to hold the instance of your adapter and then add the acquired results to the adapter using the Adapter.add() method and then call Adapter.notifyDataSetChanged() method.

Edit:- Also as you are using ListActivity, there's no need to create a new ListView instance. Just call getListView() method to obtain the ListView instance as the above answerer suggested.

Upvotes: 0

SathishKumar
SathishKumar

Reputation: 1644

Change your onCreate() like below,

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sp_history);
        //set up list
        list = getListView();

    }

Upvotes: 2

Related Questions