CustomAdapter和ListView

时间:2016-03-29 09:37:08

标签: android listview adapter listadapter

我的代码中必定存在缺失或错误的内容,以便ListView不会显示任何内容。

我已经按照这个例子Use array adapter with more views in row in listview

[为MyModel]

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

}

[myCustomAdapter]

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

}

[MyActivity]

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>

3 个答案:

答案 0 :(得分:1)

更改customAdapter

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

答案 1 :(得分:0)

试试这个:

  function call_procedure($procedure, $parameters = array()) {
      $result = $this->db->query("CALL ".$procedure);
      return $result->result_array();
  }

答案 2 :(得分:0)

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

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