简体   繁体   中英

ListView doesn't appear in the application

I'm pretty beginner with the android developing, so take me easy :3

I've created a custom listView and passed into it some values, but when I run the app nothing appears

but when I put a textView with it, the text view appears but the list view doesn't apear :/

and the code doesn't show any errors !

this is my 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;
    }
}

}

and this is my XML file

<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"/>

Change your getCount () to return the number of items in the list , list.getSize() . Your getcount is 0.

This should get you started on the PROPER way to do a listview.

I think your problem is here.

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

You dont want to add the imageview here...the Imageview will belong in a custom layout. If you see my below code in a new project, i think youll understand it in better detail

My Main Activity

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

Main Activity 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>

A Model to store my data

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

Single Item

<?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>

The adapter

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


}

Do the following changes in the code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM