为什么要使用其他布局文件来呈现ListView? [英] Why should I use an additional layout file to present a ListView?

查看:57
本文介绍了为什么要使用其他布局文件来呈现ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android技术的新手,可以通过网络教程学习.在绑定列表视图时,我很困惑,为什么我们创建一个单独的布局文件并在arrayadapter中调用该布局文件

I am new in android technology and learning via net tutorials. While binding List View i am in a confusion that why we create a separate layout file and call that layout file in arrayadapter

推荐答案

以下所示代码的来源:

Source for the code shown below: http://www.vogella.com/articles/AndroidListView/article.html

在创建简单的 ListView 时,您将使用以下内容:

When you are creating a simple ListView, you will use something like this:

XML:

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

    <ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

JAVA:

ListView listView = (ListView) findViewById(R.id.mylist);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
        "Linux", "OS/2" };

// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1, values);


// Assign adapter to ListView
listView.setAdapter(adapter);

在创建自定义 ListView 时,通常会执行以下操作:

When creating a custom ListView, you will typically do something like this:

package de.vogella.android.listactivity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MySimpleArrayAdapter(Context context, String[] values) {
        super(context, R.layout.rowlayout, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        textView.setText(values[position]);
        // Change the icon for Windows and iPhone
        String s = values[position];
        if (s.startsWith("iPhone")) {
            imageView.setImageResource(R.drawable.no);
        } else {
            imageView.setImageResource(R.drawable.ok);
        }
        return rowView;
    }
}

在此示例中,如果您查看以下行: View rowView = inflater.inflate(R.layout.rowlayout,parent,false); R.layout.rowlayout 是用于显示自定义 ListView .

In this example, if you look at the line: View rowView = inflater.inflate(R.layout.rowlayout, parent, false);, the R.layout.rowlayout is your custom layout used to show your custom ListView.

请参阅答案顶部的源链接,以获取有关 ListView的的详细教程.

Refer to the source link at the top of the answer for a detailed tutorial on ListView's.

这篇关于为什么要使用其他布局文件来呈现ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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