繁体   English   中英

带有自定义适配器的ListView不会出现

[英]ListView with custom adapter doesn't appear

我在一个应用中工作,我想在该月的前几个月显示该值,并在前一个月/下一个月显示一个左/右箭头。 到现在为止还挺好。

本月下旬,我想显示一个名称列表。 这是现在。 我的应用程序处于第一阶段。 看起来很简单,但是列表没有出现,我也不知道为什么。

这是屏幕的预览。

应用程序屏幕预览

这是我使用的所有类和布局。

MainActivity.java

package team.proodeutikiekriksitoumpas;


import java.util.ArrayList;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    public GregorianCalendar month, itemmonth;// calendar instances.

    ListView NamesListView, DataListView;
    ArrayList<String> NamesFeedList, DataFeedList;
    MyNamesAdapter NamesAdapter;
    //, DataAdapter;

    //public CalendarAdapter adapter;// adapter instance
    public Handler handler;// for grabbing some event values for showing the dot marker.
    //public ArrayList<String> items; // container to store calendar items which needs showing the event marker


    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //DataListView = (ListView) findViewById(R.id.DataListView);

        NamesFeedList = new ArrayList<String>();
        NamesFeedList.add("Kostis A");
        NamesFeedList.add("Apostolis B");
        NamesAdapter = new MyNamesAdapter(this, R.layout.name_item_view, R.id.name, NamesFeedList);

        NamesListView = (ListView) findViewById(R.id.NamesListView);
        NamesListView.setAdapter(NamesAdapter);
        /*
        DataFeedList = new ArrayList<String>();
        DataFeedList.add("Ok");
        DataFeedList.add("Ok");
        DataAdapter = new ArrayAdapter<String>(this, R.layout.data_item_view, R.id.data, DataFeedList);
        DataListView.setAdapter(DataAdapter);
        */
        month = (GregorianCalendar) GregorianCalendar.getInstance();
        itemmonth = (GregorianCalendar) month.clone();

        //Sitems = new ArrayList<String>();
        //adapter = new CalendarAdapter(this, month);

        handler = new Handler();
        //handler.post(calendarUpdater);

        TextView title = (TextView) findViewById(R.id.title);
        title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));

        RelativeLayout previous = (RelativeLayout) findViewById(R.id.previous);
        previous.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                setPreviousMonth();
                refreshCalendar();
            }
        });

        RelativeLayout next = (RelativeLayout) findViewById(R.id.next);
        next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                setNextMonth();
                refreshCalendar();

            }
        });
    }

    protected void setNextMonth() {
        if (month.get(GregorianCalendar.MONTH) == month
                .getActualMaximum(GregorianCalendar.MONTH)) {
            month.set((month.get(GregorianCalendar.YEAR) + 1),
                    month.getActualMinimum(GregorianCalendar.MONTH), 1);
        } else {
            month.set(GregorianCalendar.MONTH,
                    month.get(GregorianCalendar.MONTH) + 1);
        }

    }

    protected void setPreviousMonth() {
        if (month.get(GregorianCalendar.MONTH) == month
                .getActualMinimum(GregorianCalendar.MONTH)) {
            month.set((month.get(GregorianCalendar.YEAR) - 1),
                    month.getActualMaximum(GregorianCalendar.MONTH), 1);
        } else {
            month.set(GregorianCalendar.MONTH,
                    month.get(GregorianCalendar.MONTH) - 1);
        }

    }

    public void refreshCalendar() {
        TextView title = (TextView) findViewById(R.id.title);

        //adapter.refreshDays();
        //adapter.notifyDataSetChanged();
        //handler.post(calendarUpdater); // generate some calendar items

        title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
    }

    /**
     * public Runnable calendarUpdater = new Runnable() {

        @Override
        public void run() {
            items.clear();

            // Print dates of the current week
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
            String itemvalue;
            for (int i = 0; i < 7; i++) {
                itemvalue = df.format(itemmonth.getTime());
                itemmonth.add(GregorianCalendar.DATE, 1);
                items.add("2012-09-12");
                items.add("2012-10-07");
                items.add("2012-10-15");
                items.add("2012-10-20");
                items.add("2012-11-30");
                items.add("2012-11-28");
            }

            //adapter.setItems(items);
            //adapter.notifyDataSetChanged();
        }
    };
    **/
}

MyNamesAdapter.java

package team.proodeutikiekriksitoumpas;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MyNamesAdapter extends ArrayAdapter<String> {

    // declaring our ArrayList of Strings
    private ArrayList<String> objects;

    public MyNamesAdapter(Context context, int resource, int textViewResourceId, ArrayList<String> objects) {
        super(context, resource, textViewResourceId, objects);
        this.objects = objects;
        Log.v(null, "listaaaaaaaaa");
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        //return super.getView(position, convertView, parent);

        // assign the view we are converting to a local variable
        View v = convertView;

        // first check to see if the view is null. if so, we have to inflate it.
        // to inflate it basically means to render, or show, the view.
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.name_item_view, null);
        }

        /*
         * Recall that the variable position is sent in as an argument to this method.
         * The variable simply refers to the position of the current object in the list. 
         * (The ArrayAdapter iterates through the list we sent it)
         * 
         * Therefore, s refers to the current String object.
         */
        String s = null;
        if(objects != null) {
            s = objects.get(position);
            Log.v(null, s);
        }
        else{
            Log.v(null, "null objects");
        }

        if (s != null) {
            Log.v(null, "to textviews");
            // This is how you obtain a reference to the TextViews.
            // These TextViews are created in the XML files we defined.
            TextView name = (TextView) v.findViewById(R.id.name);

            // check to see if each individual textview is null.
            // if not, assign some text!
            if (name != null){
                name.setText(s);
            }
        }

        return v;
    }


}

activity_main.xmml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/previous"
            android:layout_width="40dip"
            android:layout_height="30dip"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true" >

            <ImageView
                android:contentDescription="@string/desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:background="@drawable/arrow_left" />
        </RelativeLayout>

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dip"
            android:textColor="#000000"
            android:text="November"
            android:textSize="18sp"
            android:textStyle="bold" />

        <RelativeLayout
            android:id="@+id/next"
            android:layout_width="40dip"
            android:layout_height="30dip"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true" >

            <ImageView
                android:contentDescription="@string/desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:background="@drawable/arrow_right" />
        </RelativeLayout>
    </RelativeLayout>

    <ListView
        android:id="@+id/NamesListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>



</LinearLayout>

name_item_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text=""
        android:textSize="18sp"
        android:textStyle="bold" />

</LinearLayout>

提前致谢。

这里有很多事情:

activity_main.xml将android:orientation =“ horizo​​ntal”更改为“ vertical”。 如前所述,您希望列表显示在下面。

name_item_view.xml的布局和文本字段都与高度匹配。 这可能是有问题的。 尝试在此处为文本“包装内容”。 也不清楚为什么您实际上需要为textview包装线性布局。 也许只使用textview尝试使用更简单的版本。 如果您要调整大小,则需要能够说明大小的东西。 如果您的列表没有,那么元素必须。 您可能有一个minimum_height,但实际上确实需要一些东西才能正常工作。

通常,您应该避免使用自定义长度的列表视图。 根据您到底要执行什么操作,最好将那些视图添加到linearlayout中(如果它不是那么多的话)。 否则您很快就会遇到问题。 listview被设计为具有大小和可滚动性-不在scrollcontainer中。 即使您可以完成这项工作,也应将其视为黑客。 注意:在recyclerview中这将不再可行,因此在此处调整布局以适应此情况可能是一个好主意。

在这里意味着:将所有可用的剩余空间用于listview。

现在,如果您希望标题可以滚动出来,则有2个选项:将其设为标题,并使用任何可以使之成为可能的方式(有lib,自己完成等操作)使其成为工具栏,您可以仅使用appcompat库只是为了使该工具栏在人们滚动时消失。 目前可能是更好的解决方案。

希望能帮助到你

将ListView的android:layout_height从“ wrap_content”更改为xml代码中的某些特定值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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