如何向Arrayadapter添加自定义布局?

时间:2015-01-28 12:54:53

标签: android android-layout listview android-arrayadapter

我正在尝试使用自定义布局创建ListViewlistView中的每个项目应如下面item.xml所示。

在代码中,我使用了

adapter = new ArrayAdapter<T>(getApplicationContext(), R.layout.listi_tems_layout, topicsList);

但它无效,因为ArrayAdapter<T>的构造函数接受第二个参数为int类似

android.R.layout.simple_list_item_1

,在我的情况下,它是自定义布局

R.layout.listi_tems_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="wrap_content"
android:orientation="horizontal"
android:weightSum="3">

<TextView 
    android:id="@+id/tvlist_topic"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
<ImageView 
    android:id="@+id/ivList_delete"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:src="@drawable/delete_icon"
    android:contentDescription="icon to delete item from the Listview"
    android:layout_weight="1"/>
<CheckBox 
    android:id="@+id/cbList_hook"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:checked="false"
    android:layout_weight="1"/>

mainlayout

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
....
....
....

<ListView 
    android:id="@+id/lvEco_topics"
    android:layout_width="match_parent"
    android:layout_height="470dp"
    android:layout_below="@id/tvEco_topic"
    android:layout_marginTop="30dp"
    android:scrollbars="vertical"
    android:divider="@android:drawable/alert_light_frame"></ListView>
<Button 
    android:id="@+id/btEco_save"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/lvEco_topics"
    android:gravity="center"
    android:text="Save"/>

public class MainActivity extends Activity {

private ArrayList<String> topicsList;
private ListAdapter adapter;

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

    topicsList = new ArrayList<String>();
    topicsList.add("topic1");
    topicsList.add("topic2");
    topicsList.add("topic3");
    topicsList.add("topic4");
    topicsList.add("topic5");
    topicsList.add("topic6");

    adapter = new ArrayAdapter<T>(getApplicationContext(), R.layout.listi_tems_layout, topicsList);

5 个答案:

答案 0 :(得分:6)

  

ArrayAdapter接受第二个参数作为int之类的东西   android.R.layout.simple_list_item_1

如果没有自定义getView ArrayAdapter方法,则自定义布局需要一个带android:id="@android:id/text1" id的TextView并在一个TextView中显示值。

要使用当前代码运行应用,请在android:id="@android:id/text1"布局中为TextView添加R.layout.listi_tems_layout

因为R.layout.listi_tems_layout布局还包含TextView的其他视图,所以通过扩展ArrayAdapter类来创建自定义适配器也可以访问其他视图。

请参阅以下示例:Custom ArrayAdapter for a ListView (Android)

答案 1 :(得分:2)

创建一个类并使用这样的基本适配器进行扩展,然后设置此适配器

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.test.R;
import com.itoks.model.ClientDetails;

public class ClientListAdapter extends BaseAdapter {
    // ArrayList<String> name, company, email, id, status;
    ArrayList<ClientDetails> clientArrayList;
    Context c;

    public ClientListAdapter(Context c, ArrayList<ClientDetails> list) {
        clientArrayList = list;
        this.c = c;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return clientArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return clientArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View row = null;
        LayoutInflater inflater = (LayoutInflater) c
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            row = inflater.inflate(R.layout.listview_item_clients, parent,
                    false);
        } else {
            row = convertView;
        }
        ClientDetails detail = clientArrayList.get(position);
        TextView name = (TextView) row.findViewById(R.id.tvClientFullName);
        name.setText(detail.name);
        TextView email = (TextView) row.findViewById(R.id.tvClientEmail);
        email.setText(detail.email);
        TextView id = (TextView) row.findViewById(R.id.tvClientID);
        id.setText("ID : " + detail.id);
        TextView company = (TextView) row
                .findViewById(R.id.tvClientCompanyName);
        company.setText(detail.company);
        TextView status = (TextView) row.findViewById(R.id.tvClientStatus);
        status.setText("Status:" + detail.status);
        return row;
    }

}

答案 2 :(得分:1)

据我了解你。您正在尝试在ArrayAdapter中使用自定义布局。这是不可能的。 Yout需要编写自己的自定义适配器扩展arrayAdapter类,您可以使用topicsList定义如何处理自定义布局。像这样:

public class ChatUserAdapter extends ArrayAdapter<UserChat> {

private Activity context;
private List<UserChat> userList;
private int ilayout;

public ChatUserAdapter(Activity context, int layout, List<UserChat> userList) {
    super(context, layout, userList);
    this.context = context;
    this.userList = userList;
    this.ilayout = layout;
}

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

static class ViewHolder {
    public TextView tvEmail;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    View rowView = convertView;
    if (rowView == null) {

        LayoutInflater layoutInflater = context.getLayoutInflater();
        rowView = layoutInflater.inflate(ilayout, null, true);
        viewHolder = new ViewHolder();
        viewHolder.tvEmail = (TextView) rowView.findViewById(R.id.tv_email);
        rowView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) rowView.getTag();
    }

    UserChat item = userList.get(position);
    viewHolder.tvEmail.setText(item.getName() + " " + item.getSurname());
    return rowView;
}
}

答案 3 :(得分:1)

我不确定它是否仍然相关, 来自源ArrayAdapter似乎有构造函数接受第三个参数中的textview资源,不确定它是否在2017年新增加了:D,

ArrayAdapter adapter = new ArrayAdapter<>(context,YourCustomLayoutID,TextViewIDinYourLayout,ListData);

从源代码来看,如果你没有提供textview布局id,它将假设整个视图是一个textview,所以我认为可以使用自定义视图而无需扩展/创建新的适配器(我知道,我讨厌自己创建一个),但你需要提供用于文本的文本视图

答案 4 :(得分:0)

请创建自己的ItemAdapter,扩展ArrayAdapter&lt;&gt;。

public class ItemAdapter extends ArrayAdapter<Item> {
private int resource;
private List<Item> items;

public ItemAdapter(Context context, int resource, List<Item> items) {
    super(context, resource, items);
    this.resource = resource;
    this.items = items;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LinearLayout itemView;
    final Item item = getItem(position);

    if (convertView == null) {
        itemView = new LinearLayout(getContext());

        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        layoutInflater.inflate(resource, itemView, true);
    }
    else {
        itemView = (LinearLayout) convertView;
    }

在MainActivity onCreate()

中加载item.xml
public class MainActivity{
    private List<Item> items;
    private ItemAdapter itemAdapter;
    @Override
      protected void onCreate(Bundle savedInstanceState) {
      itemAdapter = new ItemAdapter(this, R.layout.item, items);