0

我显示列表视图programmiclly使用下面的代码: 下面是如何在列表视图以编程方式显示:限制列表视图,以25项

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它说令牌(S),错位构建体 语法错误公众诠释获取计数“令牌(S),错位构建体” 语法错误在返回25 - 太虚方法不能返回值

回答

2

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

您可以检查一个例子:http://www.vogella.com/tutorials/AndroidListView/article.html#listview_listviewexample

+0

感谢您的及时响应。那它会是更像这样的东西吗? @Override public int getCount(){ messagesList.setAdapter(messageAdapter); return 25; } – John 2014-10-11 19:07:25

+0

Yep
但考虑到使用该代码,您将始终在列表中获得25个元素。你将需要使用像这样:'if(super.getCount()> 25)return 25 else return super.getCount()' – 2014-10-11 19:17:34

+0

我想这可以工作,因为我想在列表中最多25个,并且它不坏死必须是25,只是最大数量 – John 2014-10-11 19:20:29

1

嘿,你想获得与限制列表中,你有两个选择 1.您设置数组列表的大小, 2.您可以设定,当你的数据投入循环并设置限制

+0

谢谢。我宁愿选择1,那将如何工作? – John 2014-10-11 20:04:11

+0

从哪里获得数据,只需获得第一个0到24的项目并不是全部,那么您会自动在您的列表视图中获得25个项目,就像(i = 0; i> = 24,i ++)一样。 – Saveen 2014-10-11 23:57:44