java - 创建多个列表项 View

标签 java android xml android-layout sdk

我是 Android 开发世界的新手

我的应用程序中有一个 MainActivity,其中显示来自外部 JSON 文件的团队列表

所以我有activity_main.xml布局:

<?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">
    <!-- Main ListView 
         Always give id value as list(@android:id/list)
    -->

     <Button
     android:layout_height="wrap_content"
     android:layout_width="fill_parent"
     android:text="@string/schedules"
     android:onClick="getSchedules" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

我有 list_item.xml 布局:

<?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="wrap_content"
    android:orientation="vertical"
    android:padding="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <!-- Name Label -->

    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dip"
        android:paddingTop="6dip"
        android:textColor="#43bd00"
        android:textSize="16sp"
        android:textStyle="bold" />

    <!-- Email label -->


    <!-- Mobile number label -->
    <TextView
        android:id="@+id/alias"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:text="Mobile: "
        android:textColor="#5d5d5d"
        android:textStyle="bold" />

     <TextView
        android:id="@+id/city"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dip"
        android:textColor="#acacac" />

</LinearLayout>

(我按照教程做到了..)

现在,我创建了另一个 Activity SchedulesActivity,其中我执行完全相同的操作,但使用不同的数据(不同的字段)。所以我创建了activity_schedules.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" >

    <Button
     android:layout_height="wrap_content"
     android:layout_width="fill_parent"
     android:text="@string/teams"
     android:onClick="getTeams" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

现在,我需要创建另一个 list_item.xml 来显示其他数据,但它已经存在。我该如何创建不同的 list_item 布局。

第二个问题:在activity_schedules.xml中,每次我尝试将ListView的id从列表更改为任何其他id时,我都会收到错误“找不到与给定名称匹配的源”

谢谢

最佳答案

在自定义适配器中,您可以指定将作为 View 膨胀的布局的 id。

您可以创建两个不同的适配器并在每个适配器中设置所需的 id,或者您可以通过适配器构造函数传递您需要扩充的布局的 id。

private int _idLayout;

public ListAdapter(Context context, int idLayout) {
    super(context, idLayout);
    _idLayout = idLayout;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView==null){
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(_idLayout, parent, false);
    }

    return convertView;
}

关于java - 创建多个列表项 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25628391/

相关文章:

java - 方法的输入参数为异常时的Mock语句

java - 使用特定值计算税金

java - 如果一个 bean 由 Spring 管理,这是否意味着所有创建的 bean 也必须被管理

android - 在 Activity 中永久隐藏导航栏

android - 使用Android Management API创建企业时如何获取enterpriseToken?

android - 如何在我的类(class)中找到 FrameLayout

Android 布局——在一个 ListView 行中垂直堆叠两个 TextView

java - 如何使用JPA验证数据库中是否存在具有某些字段值的记录?

android - 更改屏幕方向时 EditText 中的值错误

javascript - 使用javascript获取xml文件的子节点值