簡體   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