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

宇宙

我在一个应用程序中工作,我想在该月的前几个月显示该值,并在前一个月/下一个月显示一个向左/向右的箭头。到目前为止,一切都很好。

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

这是屏幕的预览。

应用程序屏幕预览

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

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库只是为了使此工具栏在人们滚动时消失。目前可能是更好的解决方案。

希望能帮助到你

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android Studio中带有自定义适配器的自定义ListView

来自分类Dev

具有自定义适配器的listview中的Textview不会更新

来自分类Dev

ListView的自定义适配器

来自分类Dev

自定义ListView的适配器

来自分类Dev

android:带有自定义适配器的AutoCompleteTextView

来自分类Dev

自定义适配器视图(带有图片)

来自分类Dev

android:带有自定义适配器的AutoCompleteTextView

来自分类Dev

带有片段中的自定义适配器的ListView

来自分类Dev

带有自定义适配器的alertdialog listview不显示任何内容

来自分类Dev

带有自定义适配器的 Android ListView 仅显示最后一项

来自分类Dev

带有对齐特定行的自定义适配器的 Android ListView

来自分类Dev

ListView所有元素在正确的文本旁边显示“假”文本?自定义适配器可能出现故障?

来自分类Dev

使用自定义数组适配器,ListFragment中的项目不会显示在Listview中

来自分类Dev

使用自定义数组适配器,ListFragment中的项目不会显示在Listview中

来自分类Dev

具有ListView片段和自定义ListView适配器的Android Pull-to-Refresh

来自分类Dev

ListView自定义适配器丢失imageView

来自分类Dev

自定义适配器显示RuntimeException-ListView

来自分类Dev

使用自定义适配器过滤ListView

来自分类Dev

自定义listview适配器上的OnclickListner

来自分类Dev

ListView自定义适配器重复项

来自分类Dev

自定义基础适配器到ListView

来自分类Dev

使用自定义适配器从listview搜索项目

来自分类Dev

Android ListView onClickListener自定义适配器

来自分类Dev

使用自定义适配器未显示ListView

来自分类Dev

自定义ListView适配器中的NullPointerException

来自分类Dev

ListView自定义适配器丢失imageView

来自分类Dev

Android自定义ListView /适配器

来自分类Dev

在自定义适配器中遍历ListView

来自分类Dev

Android ListView onItemclick以及自定义适配器

Related 相关文章

  1. 1

    Android Studio中带有自定义适配器的自定义ListView

  2. 2

    具有自定义适配器的listview中的Textview不会更新

  3. 3

    ListView的自定义适配器

  4. 4

    自定义ListView的适配器

  5. 5

    android:带有自定义适配器的AutoCompleteTextView

  6. 6

    自定义适配器视图(带有图片)

  7. 7

    android:带有自定义适配器的AutoCompleteTextView

  8. 8

    带有片段中的自定义适配器的ListView

  9. 9

    带有自定义适配器的alertdialog listview不显示任何内容

  10. 10

    带有自定义适配器的 Android ListView 仅显示最后一项

  11. 11

    带有对齐特定行的自定义适配器的 Android ListView

  12. 12

    ListView所有元素在正确的文本旁边显示“假”文本?自定义适配器可能出现故障?

  13. 13

    使用自定义数组适配器,ListFragment中的项目不会显示在Listview中

  14. 14

    使用自定义数组适配器,ListFragment中的项目不会显示在Listview中

  15. 15

    具有ListView片段和自定义ListView适配器的Android Pull-to-Refresh

  16. 16

    ListView自定义适配器丢失imageView

  17. 17

    自定义适配器显示RuntimeException-ListView

  18. 18

    使用自定义适配器过滤ListView

  19. 19

    自定义listview适配器上的OnclickListner

  20. 20

    ListView自定义适配器重复项

  21. 21

    自定义基础适配器到ListView

  22. 22

    使用自定义适配器从listview搜索项目

  23. 23

    Android ListView onClickListener自定义适配器

  24. 24

    使用自定义适配器未显示ListView

  25. 25

    自定义ListView适配器中的NullPointerException

  26. 26

    ListView自定义适配器丢失imageView

  27. 27

    Android自定义ListView /适配器

  28. 28

    在自定义适配器中遍历ListView

  29. 29

    Android ListView onItemclick以及自定义适配器

热门标签

归档