ListView未出现在应用程序中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ListView未出现在应用程序中相关的知识,希望对你有一定的参考价值。

我是初学者,正在开发android,所以请带我一点:3

我已经创建了一个自定义listView并传递给它一些值,但是当我运行应用程序时,没有任何显示

但是当我用它放一个textView时,会出现文本视图,但列表视图不是apear:/

并且代码不显示任何错误!

这是我的MainActivity.java

ArrayList<Item> customList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    customList=new ArrayList<Item>();



    for (int i=0; i<pics.length;i++){
        ImageView pic= new ImageView(this);
        pic.setImageResource(pics[i]);
        Item item= new Item(null,names[i],desc[i],times[i]);
        customList.add(item);
    }


    ListView lv = (ListView) findViewById(R.id.listview);
    CustomAdapter myAdapter = new CustomAdapter(customList);
    lv.setAdapter(myAdapter);


}




class CustomAdapter extends BaseAdapter{


    ArrayList<Item> myList;

    public CustomAdapter(ArrayList<Item> list) {
        this.myList=list;
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater infalter= getLayoutInflater();
        View view = infalter.inflate(R.layout.custom_row,null);

        ImageView image=(ImageView) findViewById(R.id.image);
        TextView name=(TextView) findViewById(R.id.title);
        TextView message=(TextView) findViewById(R.id.desc);
        TextView time=(TextView) findViewById(R.id.time);

        image=myList.get(position).getPic();
        name.setText(myList.get(position).getName());
        message.setText(myList.get(position).getMessage());
        time.setText(myList.get(position).getTime());





        return view;
    }
}

}

这是我的XML文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hp.myapplication.MainActivity">




<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listview"
    tools:layout_editor_absoluteY="8dp"
    tools:layout_editor_absoluteX="8dp"/>

答案

更改getCount()以返回列表中的项目数list.getSize()。你的getcount是0。

另一答案

这应该让您开始使用PROPER方式进行列表视图。

我想你的问题就在这里。

   for (int i=0; i<pics.length;i++){
        ImageView pic= new ImageView(this);
        pic.setImageResource(pics[i]);
        Item item= new Item(null,names[i],desc[i],times[i]);
        customList.add(item);
    }

你不想在这里添加imageview ... Imageview将属于自定义布局。如果你在一个新项目中看到我的下面的代码,我想你会更详细地理解它

我的主要活动

ListView listview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listview = (ListView) findViewById(R.id.listview);

    List<Model> list = new ArrayList<>();
    list.add(new Model("top-one", "bot-one"));
    list.add(new Model("top-two", "bot-two"));
    list.add(new Model("top-three", "bot-three"));
    list.add(new Model("top-four", "bot-four"));
    list.add(new Model("top-five", "bot-five"));
    list.add(new Model("top-six", "bot-six"));
    list.add(new Model("top-seven", "bot-seven"));
    list.add(new Model("top-eight", "bot-eight"));

    ListAdapter adapter = new ListAdapter(listview.getContext(), list);
    listview.setAdapter(adapter);
}

主要活动XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.fuhnatik.customlistview.MainActivity">

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>

存储数据的模型

public class Model {

    String top, bottom;

    public Model(String top, String bottom) {
        this.top = top;
        this.bottom = bottom;
    }

    public String getTop() {
        return top;
    }

    public void setTop(String top) {
        this.top = top;
    }

    public String getBottom() {
        return bottom;
    }

    public void setBottom(String bottom) {
        this.bottom = bottom;
    }
}

单品

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


<TextView
    android:id="@+id/top"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:text="TOP SECTION HERE"
    android:textSize="16sp" />

<TextView
    android:id="@+id/bottom"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="BOTTOM SECTION HERE"
    android:textSize="12sp" />



</LinearLayout>

适配器

public class ListAdapter extends ArrayAdapter<Model> {


    private Context activityContext;
    private List<Model> list;
    public static final String TAG = "ListView";

    public ListAdapter(Context context, List<Model> list){
        super(context, R.layout.single_listview, list);
        this.activityContext = context;
        this.list = list;
    }


    @Override
    public View getView(final int position, View view, ViewGroup viewGroup){

        final ViewHolder viewHolder;

        if (view == null) {
            view = LayoutInflater.from(activityContext).inflate(R.layout.single_listview, null);
            viewHolder = new ViewHolder();

            viewHolder.top = (TextView) view.findViewById(R.id.top);
            viewHolder.bottom = (TextView) view.findViewById(R.id.bottom);

            viewHolder.top.setText(list.get(position).getTop());
            viewHolder.bottom.setText(list.get(position).getBottom());

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        return view;
    }

    private static class ViewHolder {

        TextView top;
        TextView bottom;
    }


}
另一答案

在代码中执行以下更改:

@Override
public Object getItem(int position) {
    return myList.get(position);
}

@Override
public int getCount() {
    return myList.size();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater infalter= getLayoutInflater();
    View view = infalter.inflate(R.layout.custom_row,null);

    ImageView image=(ImageView) view.findViewById(R.id.image);
    TextView name=(TextView) view.findViewById(R.id.title);
    TextView message=(TextView) view.findViewById(R.id.desc);
    TextView time=(TextView) view.findViewById(R.id.time);

    image=myList.get(position).getPic();
    name.setText(myList.get(position).getName());
    message.setText(myList.get(position).getMessage());
    time.setText(myList.get(position).getTime());

    return view;
}

以上是关于ListView未出现在应用程序中的主要内容,如果未能解决你的问题,请参考以下文章

ListView 未显示在片段中

未找到 ID 的 Android 片段

未填充 ListView。 ViewPager 和片段的问题

ListView 数据未在 Kotlin 中显示

未创建 Android listview 内容视图

Listview 项目未在 Fragment Android 中显示