简体   繁体   中英

How can I change the simple_list_item_1 to my own xml file for list item?

I am doing a list in a androidapplication-project and I want to show my own customised list items in it. How can I do this?

My java class is KategoriLista and my xml file for the whole layout is kategorier_lista and xml file for the item or cell is kategorier_list_item.

I am using Intellij.

Here is the code:

KategoriLista.java:

package com.example.sbny;


import android.*;
import android.R;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.content.Intent;



public class KategoriLista extends ListActivity{




    String  katlista [] = {
            "KupongerLista","Dagligvaror","Bygg","Handel","Noje","Hotell"
    };

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

        //Skapar en arrayadapter
        setListAdapter(new ArrayAdapter<String>(KategoriLista.this,     R.layout.simple_list_item_1,katlista));`

}

kategorier_lista.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"
        android:background="@drawable/bakgrundsbild">


    <ListView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/listView"/>
</LinearLayout>

kategorier_list_item.xml:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="?android:attr/listPreferredItemHeight"
            android:padding="6dp" >

<ImageView
        android:id="@+id/annonsorimage"
        android:layout_width="60dp"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip"

        android:src="@drawable/images" />

<TextView
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/annonsorimage"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="Description"
        android:textSize="12sp" />

<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/secondLine"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/annonsorimage"
        android:gravity="center_vertical"
        android:text="Example application"
        android:textSize="16sp" />

</RelativeLayout>

Ok, to understand better how to implement a custom listview, i advise you check this tutorial . Retrived from the tutorial.

Developing a custom Adapter To control the data assignment and to support this assignment to several Views, you create your own Adapter implementation. For this you would extend an existing adapter implementations or by sub-classing the BaseAdapter class directly.

ListView calls the getView() method on the adapter for each data element. In this method the adapter determines the layout of the row and how the data is mapped to the Views in this layout.

This root of the layout is typically a ViewGroup (LayoutManager) and contains several other Views, eg an ImageView and a TextView.

So, the code below, implements a custom listView. On each line we have two textview and on icon. Take a look(You can sse the full example here (Example CustomListView )):

Result:

在此处输入图片说明

list_layout.xml

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

<ListView android:id="@android:id/list" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#EEEEEE"/>

<TextView android:id="@android:id/empty" android:layout_width="match_parent"
android:layout_height="wrap_content" android:background="#0000FF"
android:text="@string/list_is_empty"/>  

two_line_icon.xml

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

    <ImageView android:id="@+id/icon" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <TextView android:id="@+id/text1" android:layout_width="match_parent"
     android:layout_height="wrap_content" android:text="Texto 1"/>

    <TextView android:id="@+id/text2" android:layout_width="match_parent"
     android:layout_height="wrap_content" android:text="Descricao"/>

    </LinearLayout>

    </LinearLayout>

ListWithIcon.java

public class ListWithIcon extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_layout);

    String[] dados = {"Item 1", "Item 2", "Item 3",
            "Item 4", "Item 5", "Item 6", "Item 7"};

    String[] dados2 = {"desc 1", "desc 2", "desc 3",
            "desc 4", "desc 5", "desc 6", "desc 7"};

    MyAdapter myAdapter = new MyAdapter(this, dados, dados2); 

    setListAdapter(myAdapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Toast.makeText(this, l.getItemAtPosition(position).toString(),
            Toast.LENGTH_SHORT).show();
}
}

MyAdapter.java

public class MyAdapter extends BaseAdapter {

private String[] data;
private String[] data2;
private Context context;

public MyAdapter(Context context, String[] data1, String[] data2) {
    super();
    this.data = data1;
    this.data2 = data2;
    this.context = context;
}

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

@Override
public Object getItem(int position) {
    return data[position];
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = LayoutInflater.from(context).
            inflate(R.layout.two_line_icon, parent, false);

    TextView text1 = (TextView) rowView.findViewById(R.id.text1);
    TextView text2 = (TextView) rowView.findViewById(R.id.text2);
    ImageView icon = (ImageView) rowView.findViewById(R.id.icon);

    text1.setText(data[position]);
    text2.setText(data2[position]);
    icon.setImageResource(R.drawable.ic_launcher);

    return rowView;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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