【问题标题】:Limit a listview to 25 items将列表视图限制为 25 个项目
【发布时间】:2014-10-11 18:57:17
【问题描述】:

我正在使用以下代码以编程方式显示列表视图: 以下是以编程方式显示列表视图的方式:

 messagesList = (ListView) findViewById(R.id.listMessages);
            messageAdapter = new MessageAdapter(this);

我想将列表限制为 25 个项目,并且它不是无限的。

下面是布局。

<RelativeLayout 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:background="@drawable/white_wallpaper"
                >

    <ListView
        android:id="@+id/listMessages"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/divider"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:inputType="text|textNoSuggestions"
        android:padding="0dip"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        tools:listitem="@layout/message_left" />

    <RelativeLayout 
        android:id="@+id/divider"
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="@color/off_white"
        android:layout_above="@+id/relSendMessage" />

    <RelativeLayout
            android:id="@+id/relSendMessage"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:background="#ddd"
            android:paddingLeft="10dp"
            android:layout_alignParentBottom="true">

        <EditText
            android:id="@+id/messageBodyField"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignBottom="@+id/sendButton"
            android:layout_alignTop="@+id/sendButton"
            android:layout_marginBottom="-4dp"
            android:layout_marginRight="10dp"
            android:inputType="text|textNoSuggestions"
            android:layout_toLeftOf="@+id/sendButton"
            android:background="@android:color/white"
            android:hint="@string/message_elipses"
            android:textColor="#000000"
            android:textColorLink="#adefda"
            android:textSize="14sp" />

        <Button
                android:id="@+id/sendButton"
                android:layout_width="72dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_margin="4dp"
                android:background="@drawable/button_send" />
    </RelativeLayout>

    <Button
        android:id="@+id/iSchedule"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:alpha="0.7"
        android:background="#C11B17"
        android:text="Schedule"
        android:textColor="#f2f2f2"
        android:textSize="20sp"
        android:textStyle="bold"
        android:typeface="serif" />

</RelativeLayout>

我得到了以下建议,但我不确定如何将其集成到上述代码中:

@Override
public int getCount() {
    return 25;
}

提前致谢,如有任何澄清,请告诉我。

更新

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.messaging);

        feedBack = new FeedbackDialog(this, "ID");



        final TextView iSchedule = (TextView) this.findViewById(R.id.iSchedule);
        iSchedule.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
               MessagingActivity1.this.startActivity(new Intent(MessagingActivity1.this, ScheduleMatchOptionActivity.class));
            }
        });

        Parse.initialize(this, "ID", "ID");

        bindService(new Intent(this, MessageService.class), serviceConnection,
                BIND_AUTO_CREATE);
        Intent intent = getIntent();
        recipientId = intent.getStringExtra("RECIPIENT_ID");
        currentUserId = ParseUser.getCurrentUser().getObjectId();
        messageAdapter = new MessageAdapter(this);

        @Override
        public int getCount() {
    messagesList.setAdapter(messageAdapter);
            return 25;
        }

        messagesList = (ListView) findViewById(R.id.listMessages);


        populateMessageHistory();
        messageBodyField = (EditText) findViewById(R.id.messageBodyField);
        findViewById(R.id.sendButton).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        sendMessage();
                    }
                });
    }

但是,我收到以下错误 在实时@override 中,它说令牌上的语法错误,错位的构造 在公共 int 中获取计数“令牌上的语法错误,错误的构造” 在 return 25 - Void 方法不能返回值

【问题讨论】:

    标签: java android android-layout android-activity android-listview


    【解决方案1】:

    您想使用该代码(或类似代码)覆盖 ListAdapter 中的默认 getCount 方法:http://developer.android.com/reference/android/widget/ListAdapter.html

    您可以查看示例:http://www.vogella.com/tutorials/AndroidListView/article.html#listview_listviewexample

    【讨论】:

    • 感谢您的及时回复。那么它会更像这样吗? @Override public int getCount() { messagesList.setAdapter(messageAdapter);返回 25; }
    • 是的
      但是请注意,使用该代码,您将始终在列表中获得 25 个元素。您将需要使用类似:if(super.getCount()&gt;25) return 25 else return super.getCount()
    • 我想这可能会起作用,因为我希望列表中最多有 25 个,而且不一定必须是 25,这就是最大数量
    • 谢谢。我在我的初始帖子下添加了一个更新部分。它突出了我遇到的许多问题谢谢
    • 诺诺。您必须在自定义 MessageAdapter 类中覆盖它。在您更新的示例中,您试图覆盖 onCreate 方法中的方法;如果该代码对应于 MessageAdapter,则 getCount 部分必须与 onCreate 处于同一级别。
    【解决方案2】:

    嘿,你想得到有限制的列表,你有两个选择 1.您设置数组列表大小, 2.你可以设置,当你得到数据放循环和设置限制

    【讨论】:

    • 谢谢。我更喜欢选项 1,这将如何工作?
    • 从哪里获取数据,只是获取第一个 0 到 24 项而不是全部,因此您会自动在列表视图中获取 25 项,就像简单地 for(i = 0 ;i >= 24 ,i++);
    猜你喜欢
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多