列表未显示

时间:2014-05-26 10:10:59

标签: android listview

我创建了一个自定义列表视图,但它没有显示出来。 我的活动空白了。

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>

我创建了一个自定义列表视图,但它没有显示出来。 我的活动空白了。

3 个答案:

答案 0 :(得分:1)

如果您只有1条记录,请更改此

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

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

您需要使用此方法返回记录计数。

更新:getView(...)

中尝试这种方式
  LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
  convertView=inflater.inflate(R.layout.resource_lists,parent,false);       

答案 1 :(得分:0)

put adapter.notifyDataSetChanged();在你的onCreate函数结束时。

答案 2 :(得分:0)

只需使用此类更改适配器类

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 1;
}

@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 convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if(convertView==null){
        LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
          convertView=inflater.inflate(R.layout.grid,parent,false);       
    //  arg1=LayoutInflater.from(c).inflate(R.layout.grid, arg2,false);
        TextView name=(TextView) convertView.findViewById(R.id.textView);
        TextView gender=(TextView) convertView.findViewById(R.id.textView1);
        TextView age=(TextView) convertView.findViewById(R.id.textView2);
        TextView qualification=(TextView) convertView.findViewById(R.id.textView3);
        TextView jobs=(TextView) convertView.findViewById(R.id.textView4);

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

    }
    return convertView;

}



 }