如何将simple_list_item_1更改为我自己的xml文件以获取列表项?

时间:2013-10-11 11:52:03

标签: android intellij-idea listitem custom-lists

我在androidapplication-project中做一个列表,我想在其中显示我自己的自定义列表项。我怎么能这样做?

我的java类是KategoriLista,整个布局的xml文件是kategorier_lista,项目或单元格的xml文件是kategorier_list_item。

我正在使用Intellij。

以下是代码:

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>

1 个答案:

答案 0 :(得分:4)

好的,为了更好地了解如何实现自定义列表视图,我建议您检查this tutorial。从教程中回过头来。

开发自定义适配器要控制数据分配并支持多个视图的分配,您可以创建自己的适配器实现。为此,您将扩展现有的适配器实现或直接对BaseAdapter类进行子类化。

ListView为每个数据元素调用适配器上的getView()方法。在此方法中,适配器确定行的布局以及数据如何映射到此布局中的视图。

此布局的根通常是ViewGroup(LayoutManager),并包含其他几个视图,例如一个ImageView和一个TextView。

因此,下面的代码实现了一个自定义listView。在每一行我们有两个textview和图标。看看(你可以在这里找到完整的例子(Example CustomListView)):

结果:

enter image description here

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

}