ListView不会出现在应用程序中

时间:2017-12-28 19:18:38

标签: java android list listview view

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

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

但是当我使用textView时,会出现文本视图,但列表视图不会出现:/

并且代码没有显示任何错误!

这是我的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"/>

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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;
    }


}

答案 2 :(得分:0)

在代码中进行以下更改:

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