将列表视图限制为25个项目 [英] Limit a listview to 25 items

查看:72
本文介绍了将列表视图限制为25个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am displaying a listview programmiclly using the following codes: Below is how the listview is displayed programmatically:

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

我想将列表限制为25个,并且列表不是无限的.

I would want to limit the list to 25 items, and where it is not infinite.

下面是布局.

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

已向我提出以下建议,但是我不确定如何将其集成到上面的代码中:

I have been suggested the following, but I am not sure how to integrate it within the above code:

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

在此先感谢您,如果有任何疑问,请告诉我.

Thanks in advance, and for any clarification, let me know.

更新

@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中,它说令牌上的语法错误,构造错误放置 在public int中获得计数令牌语法错误,构造放置错误" 在返回25中-无效方法无法返回值

However, I receive the following error in the live @override it says Syntax error on token(s), misplaced construct(s) in the public int get count "Syntax error on token(s), misplaced construct(s)" In the return 25 - Void methods cannot return a value

推荐答案

您要使用该代码(或类似代码)覆盖ListAdapter中的默认getCount方法:

You want to use that code (or similar) to override the default getCount method in ListAdapter: http://developer.android.com/reference/android/widget/ListAdapter.html

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

You can check an example in: http://www.vogella.com/tutorials/AndroidListView/article.html#listview_listviewexample

这篇关于将列表视图限制为25个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆