有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在Android中动态创建多个ListView

我目前正在安卓上开发一个基本的新闻聚合应用程序,我知道会有很多内容,但我基本上只是在创建一个,因为我认为这是一个开始安卓开发的好地方

我的目标是显示来自多个提要的文章。每个提要都有自己的水平滑动列表,有点像pulse新闻应用程序。到目前为止,我已经找到了创建“HorizontalListView”的教程,现在我的应用程序显示了一个基本的水平滑动列表视图,如下所示:

屏幕截图链接:http://www.dev-smart.com/wp-content/uploads/2011/03/device-200x300.png

脉冲应用程序屏幕截图:http://a1525.phobos.apple.com/us/r1000/080/Purple/v4/ba/6a/01/ba6a01d1-f0b7-4bb7-3f94-5a5761653e3c/mzl.zxnjzzmk.480x480-75.jpg

代码:

public class HorizontalListViewDemo extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.listviewdemo);

    HorizontalListView listview = (HorizontalListView) findViewById(R.id.listview);
    listview.setAdapter(mAdapter);


}

private static String[] dataObjects = new String[]{ "Text #1",
    "Text #2",
    "Text #3", "Text #4"  }; 

private BaseAdapter mAdapter = new BaseAdapter() {

    private OnClickListener mOnButtonClicked = new OnClickListener() {

        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(HorizontalListViewDemo.this);
            builder.setMessage("hello from " + v);
            builder.setPositiveButton("Cool", null);
            builder.show(); 
        }
    };

    @Override
    public int getCount() {
        return dataObjects.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);
        TextView title = (TextView) retval.findViewById(R.id.title);
        Button button = (Button) retval.findViewById(R.id.clickbutton);
        button.setOnClickListener(mOnButtonClicked);
        title.setText(dataObjects[position]);
        return retval;
    }   
};
}

XML:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
>

<LinearLayout
  xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
  安卓:orientation="vertical"
  安卓:layout_width="fill_parent"
  安卓:layout_height="wrap_content"
  安卓:background="#000"
  >

  <com.devsmart.安卓.ui.HorizontalListView
    安卓:id="@+id/listview"
    安卓:layout_width="fill_parent"
    安卓:layout_height="250dp"
    安卓:background="#ddd"
  />

</LinearLayout>
</ScrollView>

正如我所提到的,每个HorizontalListView将表示1个提要,并如上所述显示其关联文章。因此,我在开发过程中的下一步是尝试创建这些HorizontalListView的动态视图,以便用户可以从应用程序中声明提要,然后在已定义提要的下方动态创建提要

不管这个过程中涉及到的所有其他工作,我实际上只是请求帮助创建一个新的HorizontalListView,它可以回收已经定义的XML listview布局 id="listview". I know I could get this to work if I where to pre-define all feeds and just create unique XML layouts with unique id's, but I'm wondering is there a way I can define a new HorizontalListView that reuses existing layouts.

我希望你们能帮我解释一下

谢谢

更新1: 使用视图

//Defining main XML file "listviewdemo.xml"
setContentView(R.layout.listviewdemo);
//Creating a new view to apply to my HorizontalListViews, /res/layout/listviewStyle.xml
View view = getLayoutInflater().inflate(R.layout.listviewStyle, null);

//Defining my first HorizontalListView      
HorizontalListView listview = (HorizontalListView) view.findViewById(R.id.listviewReuse);
listview.setAdapter(mAdapter);

共 (1) 个答案

  1. # 1 楼答案

    HorizontalListView listview = (HorizontalListView) findViewById(R.id.listview);
    

    此行将始终返回使用R.id.listview找到的第一个视图。当您有多个列表视图时,这不是很好

    现在不再调用活动。findViewById()在子视图上调用findViewById()。可以在布局中包含每个listview。然后调用findViewById以查找作为listview父级的布局。调用在布局上找到了findViewById

    第一次找到listView后,请记住它以备将来使用

    在您发布的更新代码中,删除此行。它没有做任何事情b/c此视图不属于布局R布局。listviewdemo

    View view = getLayoutInflater().inflate(R.layout.listviewStyle, null);
    

    然后在xml中为R.layout。listviewdemo将两个ListView添加到同一个列表中。xml(假设这是您的目标)。不管是什么情况,您都应该将所有视图添加到同一布局中,然后使用findViewById检索视图