2016-03-29 11 views
-2

Es muss etwas in meinem Code fehlen oder falsch sein, damit das ListView nichts anzeigt.CustomAdapter und ListView

Ich habe dieses Beispiel Use array adapter with more views in row in listview gefolgt

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


    } 
} 

[das Layout die Zeile] dies (das Layout der Aktivität nicht gezeigt)

<?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> 
+1

Sie fehlen '_name.setText()' in getView – Blackbelt

+0

In Ihrem benutzerdefinierten Adapter, setzen Sie nicht Wert für '' _name, _surname und _ranking'', so dass es nichts –

+0

angezeigt und immer noch nicht funktioniert nach dem Hinzufügen jene? –

Antwort

0

Versuche:

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

diese Zeilen in Ihrer getView() Methode hinzufügen, Sie sind fehlende Werte zu Eigenschaften setzen, wie Blackbelt sagte,

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

ändern 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; 
    } 
Verwandte Themen