简体   繁体   English

创建多个列表项视图

[英]Create multiple list item views

I'm new to the android developping world 我是android开发世界的新手

I have a MainActivity in my app, in which i display a list of teams coming from an external JSON file 我的应用程序中有一个MainActivity,其中显示了来自外部JSON文件的团队列表

So i have the activity_main.xml layout : 所以我有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>

And i have the list_item.xml layout : 而且我有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>

(i followed a tutorial to do it..) (我按照教程进行操作。)

Now, i created another activity, SchedulesActivity, in which i do exactly the same thing but with different data (different fields). 现在,我创建了另一个活动SchedulesActivity,其中我做完全相同的事情,但使用不同的数据(不同的字段)。 So i created the activity_schedules.xml layout : 所以我创建了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>

And now, i need to create another list_item.xml, to display the other data, but it already exists. 现在,我需要创建另一个list_item.xml,以显示其他数据,但是它已经存在。 How can i do to create a different list_item layout. 我该如何创建不同的list_item布局。

Second question : In activity_schedules.xml, everytime i try to change the id of the ListView from list to whatever other id, i got an error "No sources found that matches the given name" 第二个问题:在activity_schedules.xml中,每次我尝试将ListView的ID从列表更改为其他ID时,都会出现错误“找不到与给定名称匹配的源”

Thank you 谢谢

  1. list_item, if you wanna show same kind of data I mean if the first list_item that you have created and the second list_item you gonna create carry same fields, if yes no need to create one more you can use the same list_item by inflating it where ever you want to show if no you can create an entirely different list_item with a different name list_item,如果您想显示相同类型的数据,我的意思是,如果您创建的第一个list_item和要创建的第二个list_item包含相同的字段,如果是,则无需再创建一个,则可以通过在任何地方放大它来使用相同的list_item您想要显示是否可以使用不同的名称创建完全不同的list_item

  2. activity_Schedules.xml you are using id as android:id"@android:id/list" which mean you are existing ids from Android system. activity_Schedules.xml,您将id用作android:id“ @android:id / list”表示您是Android系统中的现有ID。 If you wanna create a different id try this way "android:id"somename" 如果您要创建其他ID,请尝试“ android:id” somename”

In your custom Adapter, you can specify the id of the layout that will be inflate as a view. 在自定义适配器中,您可以指定将作为视图膨胀的布局的ID。

You can create two different Adapter and set in every one the desired id, or you can pass through the Adapter constructor the id of the the layout that you need to inflate. 您可以创建两个不同的适配器,并在每个适配器中设置所需的ID,也可以通过Adapter构造函数传递需要膨胀的布局的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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM