user
user

Reputation: 545

Create multiple list item views

I'm new to the android developping world

I have a MainActivity in my app, in which i display a list of teams coming from an external JSON file

So i have the activity_main.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">
    <!-- 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 :

<?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). So i created the activity_schedules.xml layout :

<?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. How can i do to create a different list_item layout.

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"

Thank you

Upvotes: 0

Views: 89

Answers (2)

V&#237;ctor Albertos
V&#237;ctor Albertos

Reputation: 8293

In your custom Adapter, you can specify the id of the layout that will be inflate as a view.

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.

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;
}

Upvotes: 1

Sid
Sid

Reputation: 711

  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

  2. activity_Schedules.xml you are using id as android:id"@android:id/list" which mean you are existing ids from Android system. If you wanna create a different id try this way "android:id"somename"

Upvotes: 1

Related Questions