繁体   English   中英

自定义适配器和列表视图

[英]CustomAdapter and ListView

我的代码中一定有什么遗漏或错误,以便 ListView 不显示任何内容。

我已经按照这个例子使用数组适配器在列表视图中的行中有更多视图

[我的模型]

package com.example.apinato.layoutexample;

public class MyModel {
    private String name;
    private String surname;
    private String ranking;

    public MyModel(String name, String address, String ranking) {
        this.name = name;
        this.surname = address;
        this.ranking = ranking;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }
    public String getSurname() {
        return surname;
    }

    public void setRanking(String ranking) {
        this.surname= ranking;
    }
    public String getRanking() {
        return ranking;
    }

}

[我的自定义适配器]

package com.example.apinato.layoutexample;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

public class MyCustomAdapter extends ArrayAdapter {
    private final Context context;
    private final List list;


    public MyCustomAdapter(Context context, int position, ArrayList<com.example.apinato.layoutexample.MyModel> list) {
        super(context, position);
        this.context = context;
        this.list = list;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View rowView = convertView;


        // Get a new instance of the row layout view
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        rowView = inflater.inflate(R.layout.row, parent, false);


        TextView _name = (TextView) rowView.findViewById(R.id.titoloriga1);
        TextView _surname = (TextView) rowView.findViewById(R.id.descrizioneriga1);
        TextView _ranking = (TextView) rowView.findViewById(R.id.ranking1);

        //added according to your suggestions    
        com.example.apinato.layoutexample.MyModel myModel = (com.example.apinato.layoutexample.MyModel) list.get(position);

        _name.setText(myModel.getName());
        _surname .setText(myModel.getName());
        _ranking .setText(myModel.getName());

        return rowView;
    }

}

[我的活动]

package com.example.apinato.layoutexample;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MainActivity extends Activity {

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

        final ListView listview = (ListView) findViewById(R.id.listview);

        ArrayList<com.example.apinato.layoutexample.MyModel> list = new ArrayList<com.example.apinato.layoutexample.MyModel>();
        list.add(new com.example.apinato.layoutexample.MyModel("aaaaa1","aaaaa1","2"));
        list.add(new com.example.apinato.layoutexample.MyModel("bbbbb2","bbbbb2","3"));
        list.add(new com.example.apinato.layoutexample.MyModel("ccccc3","ccccc3","1"));
        list.add(new com.example.apinato.layoutexample.MyModel("ddddd4", "ddddd4", "5"));
        list.add(new com.example.apinato.layoutexample.MyModel("ccccc5", "ccccc5", "4"));

        final com.example.apinato.layoutexample.MyCustomAdapter adapter = new com.example.apinato.layoutexample.MyCustomAdapter(this, R.layout.row, list);
        listview.setAdapter(adapter);


    }
}

[行的布局](活动的布局未显示)

<?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:background="@drawable/customborder"

    android:orientation="vertical"
    android:layout_marginTop="24dp"
    android:layout_marginBottom="24dp"
    android:layout_marginLeft="24dp"
    android:layout_marginRight="24dp"

    android:padding="8dp">

    <!-- upper line -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff">

        <ImageView
            android:id="@+id/imgriga1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:src="@drawable/facebook_128" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/imgriga1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/titoloriga1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Titolo" />

            <TextView
                android:id="@+id/ranking1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="stelle" />

            <TextView
                android:id="@+id/descrizioneriga1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Descrizione" />
        </LinearLayout>
    </RelativeLayout>

    <!-- separator-->
    <View android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#cccccc" >
    </View>

    <!-- lower line-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <ImageView
            android:id="@+id/imgriga2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginRight="8dp"
            android:layout_marginTop="6dp"
            android:src="@drawable/google_drive_48" />

        <TextView
            android:id="@+id/titoloriga2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@id/imgriga2"
            android:text="Condividi" />

        <TextView
            android:id="@+id/ranking2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/titoloriga2"
            android:layout_toRightOf="@id/imgriga2"
            android:text="Condividi questo contenuto con i tuoi amici" />


    </RelativeLayout>

</LinearLayout>

更改您的自定义适配器

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View rowView = convertView;


        // Get a new instance of the row layout view
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        rowView = inflater.inflate(R.layout.row, parent, false);


        TextView _name = (TextView) rowView.findViewById(R.id.titoloriga1);
        TextView _surname = (TextView) rowView.findViewById(R.id.descrizioneriga1);
        TextView _ranking = (TextView) rowView.findViewById(R.id.ranking1);
        _name.setText(list.get(position).getName());
        _surname.setText(list.get(position).getSurname());
        _ranking.setText(list.get(position)getRanking());
        return rowView;
    }

尝试这个:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View rowView = convertView;


        // Get a new instance of the row layout view
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        rowView = inflater.inflate(R.layout.row, parent, false);


        TextView _name = (TextView) rowView.findViewById(R.id.titoloriga1);
        TextView _surname = (TextView) rowView.findViewById(R.id.descrizioneriga1);
        TextView _ranking = (TextView) rowView.findViewById(R.id.ranking1);

        MyModel myModel = list.get(position);

        _name.setText(myModel.getName()); 
        _surname .setText(myModel..getSureName());
        _ranking .setText(myModel.getRanking());

        return rowView;
    }

在您的getView()方法中添加这些行,您缺少将值设置为 Blackbelt 所说的属性,

_name.setText(list.get(position).getName()));
_surname.setText(list.get(position).getSureName()));
_ranking.setText(list.get(position).getRanking());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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