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

2023-09-07 01:11:14 作者:柠の萌°

我想创建一个的ListView 定制布局。在每个项目中的的ListView 看起来应该如图所示 item.xml 贴在下面。

在code,我用

 适配器=新的ArrayAdapter< T>(getApplicationContext(),R.layout.listi_tems_layout,topicsList);
 

但它不工作,因为的 ArrayAdapter&LT构造; T> 接受第二个参数 INT 的东西像

android.R.layout.simple_list_item_1

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

  R.layout.listi_tems_layout
 
excle2016在现有选项卡中增加自定义组和命令

我应该使用哪种适配器或如何解决此问题。谢谢

项目

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向
机器人:weightSum =3>

<的TextView
    机器人:ID =@ + ID / tvlist_topic
    机器人:layout_width =0dp
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_weight =1/>
< ImageView的
    机器人:ID =@ + ID / ivList_delete
    机器人:layout_width =0dp
    机器人:layout_height =WRAP_CONTENT
    机器人:可点击=真
    机器人:SRC =@可绘制/ delete_icon
    机器人:contentDescription =图标,从列表视图中删除项目
    机器人:layout_weight =1/>
<复选框
    机器人:ID =@ + ID / cbList_hook
    机器人:layout_width =0dp
    机器人:layout_height =WRAP_CONTENT
    机器人:检查=假
    机器人:layout_weight =1/>
 

mainlayout

 < RelativeLayout的
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=$ {relativePackage} $ {activityClass}。>
....
....
....

<的ListView
    机器人:ID =@ + ID / lvEco_topics
    机器人:layout_width =match_parent
    机器人:layout_height =470dp
    机器人:layout_below =@ ID / tvEco_topic
    机器人:layout_marginTop =30dp
    机器人:滚动条=垂直
    机器人:分隔=@机器人:可绘制/ alert_light_frame>< / ListView控件>
<按钮
    机器人:ID =@ + ID / btEco_save
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_below =@ ID / lvEco_topics
    机器人:重力=中心
    机器人:文本=保存/>
 

code

 公共类MainActivity延伸活动{

私人的ArrayList<字符串> topicsList;
私人ListAdapter适配器;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main_activity);

    topicsList =新的ArrayList<字符串>();
    topicsList.add(TOPIC1);
    topicsList.add(TOPIC2);
    topicsList.add(topic3​​);
    topicsList.add(topic4);
    topicsList.add(topic5);
    topicsList.add(topic6);

    适配器=新的ArrayAdapter< T>(getApplicationContext(),R.layout.listi_tems_layout,topicsList);
 

解决方案

创建一个类,像这样的基础转接器扩展,然后将该适配器

 进口的java.util.ArrayList;

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

进口com.example.test.R;
进口com.itoks.model.ClientDetails;

公共类ClientListAdapter扩展了BaseAdapter {
    // ArrayList的<字符串>姓名,公司名称,电子邮件,ID,状态;
    ArrayList的< ClientDetails> clientArrayList;
    语境℃;

    公共ClientListAdapter(上下文C,ArrayList的< ClientDetails>列表){
        clientArrayList =清单;
        this.c = C;

    }

    @覆盖
    公众诠释getCount将(){
        // TODO自动生成方法存根
        返回clientArrayList.size();
    }

    @覆盖
    公共对象的getItem(INT位置){
        // TODO自动生成方法存根
        返回clientArrayList.get(位置);
    }

    @覆盖
    众长getItemId(INT位置){
        // TODO自动生成方法存根
        返回的位置;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        // TODO自动生成方法存根
        查看排= NULL;
        LayoutInflater充气=(LayoutInflater)C
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        如果(convertView == NULL){
            行= inflater.inflate(R.layout.listview_item_clients,父母,
                    假);
        } 其他 {
            行= convertView;
        }
        ClientDetails细节= clientArrayList.get(位置);
        TextView的名称=(TextView中)row.findViewById(R.id.tvClientFullName);
        name.setText(detail.name);
        TextView的电子邮件=(的TextView)row.findViewById(R.id.tvClientEmail);
        email.setText(detail.email);
        TextView的ID =(TextView中)row.findViewById(R.id.tvClientID);
        id.setText(ID:+ detail.id);
        TextView的公司=(TextView中)一行
                .findViewById(R.id.tvClientCompanyName);
        company.setText(detail.company);
        TextView的状态=(TextView中)row.findViewById(R.id.tvClientStatus);
        status.setText(状态:+ detail.status);
        返回行;
    }

}
 

I am trying to create a ListView with customized layout. each item in the listView should look like as shown in the item.xml posted below.

in the code, i used

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

but it is not working because the constructor of the ArrayAdapter<T> accepts the second parameter as int something like

android.R.layout.simple_list_item_1

, and in my case it is customized layout which is

R.layout.listi_tems_layout

which adapter should i use or how to solve this. thanks

Item:

<?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"/>

code:

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

解决方案

create a class and extends with base adapter like this and then set this adapter

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

}