添加到ListView控件 [英] Adding to ListView

查看:170
本文介绍了添加到ListView控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去列表视图添加到我的活动。以下是是假设发生:
用户点击一个按钮,并使用一个的AsyncTask从互联网上获取数据。然后该数据应该被添加到列表视图。该数据来自表网上因此有10个表中的行那么就应该在ListView 10个值。


  1. 但我有一些问题。
    首先可以在列表视图被添加到$ P $对现有布局和动态更新?


  2. 难道一个列表视图意味着你不能使用的setContentView()方法,必须使用setListAdapter?


  3. 如果您已经有安排上有两个按钮,可以将列表视图中他们下面加入,仍然使用的setContentView()方法来实现?布局

感谢

修改

更新code - 哪些作品:

  @覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.new_sightings);    ListView控件列表= getListView();
    list.setAdapter(新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,价值观));
    list.setTextFilterEnabled(真);
}

XML布局

  XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =FILL_PARENT
  机器人:方向=垂直>  < TableLayout机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
  >  <的TableRow机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
  >  <的TextView机器人:ID =@ + ID / txtTitle
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_weight =1
机器人:比重=中心
机器人:文字=ISS predictions
机器人:文字样式=大胆
机器人:文字颜色=#FFFFFF
    />  <按钮
机器人:ID =@ + ID / reloadTen
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =FILL_PARENT
机器人:layout_gravity =右
机器人:layout_weight =1
机器人:文字=刷新
机器人:文字颜色=#FFFFFF
机器人:文字样式=大胆
机器人:背景=#000000
/>  < /&的TableRow GT;  < / TableLayout>   < ListView控件机器人:ID =@ + ID /安卓名单
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =15dp
机器人:文字样式=大胆  />
< / LinearLayout中>


解决方案

1)是的,你可以有一个的ListView 在布局,并更新了显示的数据的ListView 。通常情况下,你会做,从 ArrayAdapter 继承缩小差距的一类基础数据,并为你的的ListView 生成视图之间。

当要更新显示的数据,使用加()和<一个href=\"http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged%28%29\"相对=nofollow> notifyDataSetChanged()的适配器上。

2)号你叫 setListAdapter(),只有当你的活动从 ListActivity 继承你有一个的ListView 元素 @android的ID:ID /列表。这里有这样的元素的例子:

 &LT; ListView的机器人:ID =@机器人:ID /列表
               机器人:layout_width =match_parent
               机器人:layout_height =match_parent
               机器人:layout_weight =1/&GT;

您仍然可以使用的setContentView()所需的XML布局,使用通过。

3)是的,基于上述原因。

Im trying to add a list view to my activity. Here is what is suppose to happen: The user clicks on a button and that uses an asyncTask to get data from the internet. That data should then be added to the list view. The data comes from a table online therefore it there are 10 rows in the table then there should be 10 values in the Listview.

  1. But I'm having some problems. First of all can a listview be added to a pre-existing layout and be updated on the fly?

  2. Does a list view mean you cannot use the setContentView() method and have to use setListAdapter?

  3. If you have a layout that already has two buttons arranged on it can the list view be added in below them and still be implemented using the setContentView() method?

Thanks

Edit

Updated Code - Which works:

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

    ListView list = getListView();
    list.setAdapter( new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, values ) );
    list.setTextFilterEnabled( true );
}

XML Layout

?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation = "vertical">

  <TableLayout

android:layout_width = "fill_parent"
android:layout_height = "wrap_content"  
  >

  <TableRow

android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_weight = "1"
  >

  <TextView

android:id = "@+id/txtTitle"
android:layout_width = "wrap_content"
android:layout_height = "fill_parent"
android:layout_weight = "1"
android:gravity = "center"
android:text = "ISS Predictions" 
android:textStyle="bold"
android:textColor = "#FFFFFF"
    />

  <Button 
android:id="@+id/reloadTen" 
android:layout_height="wrap_content" 
android:layout_width ="fill_parent"
android:layout_gravity = "right"
android:layout_weight = "1"
android:text = "Reload"
android:textColor = "#FFFFFF"
android:textStyle = "bold"
android:background = "#000000"
/>

  </TableRow>

  </TableLayout>

   <ListView

android:id = "@+id/android:list"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:textSize = "15dp"
android:textStyle = "bold"

  />


</LinearLayout>

解决方案

1) Yes, you can have a ListView in your layout, and update the data displayed by the ListView. Typically you'd make a class that inherits from ArrayAdapter to bridge the gap between your underlying data and generating views for your ListView.

When you want to update the data displayed, use add() and notifyDataSetChanged() on your adapter.

2) No. You call setListAdapter() only when your activity inherits from ListActivity and you have a ListView element with an id of @android:id/list. Here's an example of such an element:

<ListView android:id="@android:id/list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:layout_weight="1"/>

You still use setContentView() to pass in the desired XML layout to use.

3) Yes, for the reasons explained above.

这篇关于添加到ListView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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