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

时间:2015-12-02 12:43:51

标签: java android listview android-listview android-arrayadapter

我在一个应用程序中工作,我希望在该应用程序中显示月份最高点和前/下个月的左/右箭头。到目前为止一切都很好。

本月下旬,我想显示一个名单。这是暂时的。我的应用程序处于第一阶段。 看起来很简单,但列表没有出现,我无法弄清楚原因。

以下是屏幕预览。

app screen preview

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

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>

提前致谢。

2 个答案:

答案 0 :(得分:2)

这里有很多东西:

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

name_item_view.xml 布局和文本字段都匹配高度。这可能有问题。尝试在这里为文本“包装内容”。还不清楚为什么你真的需要为textview包装一个linearlayout。也许只使用textview尝试使用更简单的版本。如果你想要大小的东西,你需要一些东西来说明大小。如果你的列表没有,那么元素必须。你可以有一个最小高度,但它真的需要一些东西才能正常工作。

一般来说,您应该避免使用自定义长度的列表视图。取决于你究竟想要做什么,将这些视图添加到线性布局可能是完全正确的,如果它不是那么多。否则你很快就会遇到问题。 listview被设计为一个大小和可滚动 - 不是在一个scrollcontainer。即使你可以做这个工作,它应该被认为是一个黑客。 注意:在recyclerview中,这将不再可能 因此,在这里调整布局以适应这一点可能是一个好主意。

在这里意味着: 将所有可用的空间用于列表视图。

现在,如果你喜欢滚动标题,你有两个选择: 使它成为一个标题并使用任何方式使这成为可能你可以找到(有libs,自己动手等) 使它成为一个工具栏,您只需使用appcompat libs就可以在人们滚动时使这个工具栏消失。这可能是目前更好的解决方案。

希望有所帮助

答案 1 :(得分:0)

更改来自&#34; wrap_content&#34;的ListView的android:layout_height在xml代码

中的某个特定值