Java – ListView does not work in android

ListView does not work in android… here is a solution to the problem.

ListView does not work in android

I’m trying to make a notification app for my college project.

I’m trying to create a custom adapter for a listview, but the activity that contains the ListView shows nothing. I think I did something wrong with the getView() method in CustomAdapter.java.

The CustomAdapter class is used to create a subview for a ListView. ListvieChild.xml defines the layout of a single line in a ListView Tith XML file – ListActivity .xml

    package com.example.client_nic;

import java.util.ArrayList;

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.BaseAdapter;
    import android.widget.TextView;
    import android.widget.Toast;

public class CustomAdapter extends BaseAdapter {
        public Context context =null;
        public ArrayList<String> nam = null;
        public ArrayList<String> date= null;
        public ArrayList<String> time = null;
        LayoutInflater inflater =null;

public CustomAdapter(Context context, ArrayList<String> nam,
                ArrayList<String> date, ArrayList<String> time) {
            super();
            nam = new ArrayList<String>();
            date = new ArrayList<String>();
            time = new ArrayList<String>();

this.context = context;
            this.nam = nam;
            this.date = date;
            this.time = time;

inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
        public int getCount() {
             TODO Auto-generated method stub
            return nam.size();
        }

@Override
        public Object getItem(int arg0) {
             TODO Auto-generated method stub
            return arg0;
        }

@Override
        public long getItemId(int arg0) {
             TODO Auto-generated method stub
            return arg0;
        }

@Override
        public View getView(int pos, View view, ViewGroup arg2) {

 TODO Auto-generated method stub
            Log.e("name",nam.get(pos));
            Log.e("date",date.get(pos));
            Log.e("time", time.get(pos));

view = inflater.inflate(R.layout.listviewchild, arg2,false);
            if(view.isActivated()){
                Toast.makeText(context, "yes",Toast.LENGTH_SHORT).show();
            }

TextView nametv = (TextView)view.findViewById(R.id.textView1);
            TextView datetv = (TextView)view.findViewById(R.id.textView2);
            TextView timetv = (TextView)view.findViewById(R.id.textView3);

nametv.setText(nam.get(pos));
            datetv.setText(date.get(pos));
            timetv.setText(time.get(pos));
            return view;
        }

}

listviewchild.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

<TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:height="30dp" />
  <TextView android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:height="20dp"
      android:layout_below="@+id/textView1"
      />
  <TextView android:id="@+id/textView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:height="20dp"
      android:layout_marginLeft="30dp"
      android:layout_toRightOf="@+id/textView2"
      android:layout_below="@+id/textView1"

/>

</RelativeLayout>

listactivity.xml

<?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" >
<ListView android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

</ListView>    

</LinearLayout>

Solution

Try the following:

Package com.example.client_nic;

import java.util.ArrayList;

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.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class CustomAdapter extends BaseAdapter {
    public Context context =null;
    public ArrayList<String> nam = null;
    public ArrayList<String> date = null;
    public ArrayList<String> time = null;
    LayoutInflater inflater = null;

public CustomAdapter(Context context, ArrayList<String> nam,
            ArrayList<String> date, ArrayList<String> time) {
        nam = new ArrayList<String>();
        date = new ArrayList<String>();
        time = new ArrayList<String>();

this.context = context;
        this.nam = nam;
        this.date = date;
        this.time = time;

inflater = LayoutInflater.from(context);

}

@Override
    public int getCount() {
         TODO Auto-generated method stub
        return nam.size();
    }

@Override
    public Object getItem(int arg0) {
         TODO Auto-generated method stub
        return arg0;
    }

@Override
    public long getItemId(int arg0) {
         TODO Auto-generated method stub
        return arg0;
    }

@Override
    public View getView(int pos, View view, ViewGroup arg2) {

 TODO Auto-generated method stub
        Log.e("name",nam.get(pos));
        Log.e("date",date.get(pos));
        Log.e("time", time.get(pos));

view = inflater.inflate(R.layout.listviewchild, arg2, false);
        if(view.isActivated()){
            Toast.makeText(context, "yes",Toast.LENGTH_SHORT).show();
        }

TextView nametv = (TextView)view.findViewById(R.id.textView1);
        TextView datetv = (TextView)view.findViewById(R.id.textView2);
        TextView timetv = (TextView)view.findViewById(R.id.textView3);

nametv.setText(nam.get(pos));
        datetv.setText(date.get(pos));
        timetv.setText(time.get(pos));
        return view;
    }

}

This should appropriately exaggerate your views and work. If there are other problems, you might pass empty data to the adapter.

A few side notes

1) You are passing three ArrayLists to populate the ListView with data. Why not just pass a single array list with custom Java objects? The fields of a Java object are name, data, time.

Namely:

public class MyObject{
    public String name;
    public String date;
    public String time;

MyObject(String name, String date, String time){
        this.name = name;
        this.date = date;
        this.time = time;
    }

define getters and setters...
}

And:

 public CustomAdapter(Context context, ArrayList<MyObject> objs) {
     ...
     ...
 }

2) You’re inflating the View every time. Instead, use ViewHolder Pattern Save View data instead of redrawing and finding resources each time in your application. Android takes advantage of reclaimed views, so you want to maximize it and conserve resources and reduce the latency (a lot of overhead!) each time the View is loaded. I won’t give you an example of this, but look for the link to learn how to do this yourself!

Related Problems and Solutions