The list is not displayed

advertisements

I have created a custom listview ,but it is not showing up. I am getting an blank activity.

CUSTOM ADAPTER.JAVA

package com.example.jobs_on_call_adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.jobs_on_call.app.R;

public class Resource_list_adapter extends BaseAdapter{
    Context c;

    public Resource_list_adapter(Context c) {
        super();
        this.c = c;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

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

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

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        if(arg1==null){
            arg1=LayoutInflater.from(c).inflate(R.layout.resource_lists, arg2,false);
            TextView name=(TextView) arg1.findViewById(R.id.tv_name);
            TextView gender=(TextView) arg1.findViewById(R.id.tv_gender);
            TextView age=(TextView) arg1.findViewById(R.id.tv_age);
            TextView qualification=(TextView) arg1.findViewById(R.id.tv_qualification);
            TextView jobs=(TextView) arg1.findViewById(R.id.tv_jobs);

            name.setText("anuj");
            gender.setText("Male");
            age.setText("23");
            qualification.setText("B.E.IT");
            jobs.setText("xyz");

        }
        return arg1;

    }

}

ACTIVITY.JAVA

    package com.example.jobs_on_call.app;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ListView;

    import com.example.jobs_on_call_adapter.Resource_list_adapter;

    public class Resource_List extends Activity {

        ListView resource_list;
        Resource_list_adapter adapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.resource_lists);
            resource_list=(ListView) findViewById(R.id.lv_resources);
            adapter=new Resource_list_adapter(this);
            resource_list.setAdapter(adapter);
        }

    }

CUSTOM LIST.XML
<?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/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />

    <TextView
        android:id="@+id/tv_gender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="36dp"
        android:layout_toRightOf="@+id/tv_name"
        android:text="TextView" />

    <TextView
        android:id="@+id/tv_age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/tv_gender"
        android:text="TextView" />

    <TextView
        android:id="@+id/tv_qualification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tv_name"
        android:layout_marginTop="17dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/tv_jobs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tv_qualification"
        android:layout_marginTop="15dp"
        android:text="TextView" />

</RelativeLayout>

LIST.XML

<?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/lv_resources"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

I have created a custom listview ,but it is not showing up. I am getting an blank activity.


If you have only 1 record then change this

 @Override
public int getCount() {
    // TODO Auto-generated method stub
    return 0;
}

to

 @Override
public int getCount() {
    // TODO Auto-generated method stub
    return 1;
}

You need to return record count by using this method.

Update: try this way in your getView(...)

  LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  convertView=inflater.inflate(R.layout.resource_lists,parent,false);