2016-04-10 23 views
0

Ich habe Datenobjekte shopObjects auf einer ArrayList erstellt. Ich überprüfe, ob sie voll werden. Nicht leer und versuchte einen Adapter zu erstellen. Ich mache das zum ersten Mal und die Liste ist leer. Ich bin mir nicht sicher, was ich ehrlich falsch mache.LIstView Adapter zeigt keinen Inhalt mit benutzerdefiniertem Adapter an

**CustomListAdapter.java** 
    public class CustomListAdapter extends ArrayAdapter<shopClass> { 
    ArrayList<shopClass> shopObjects; 
    private final int resource; 

    public CustomListAdapter(Context context, ArrayList<shopClass> PshopObjects, int resource) { 
     super(context, R.layout.list_row, PshopObjects); 
     shopObjects = PshopObjects; 
     this.resource = resource; 
    } 

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

     // We need to get the best view (re-used if possible) and then 
     // retrieve its corresponding ViewHolder, which optimizes lookup efficiency 
     View view = getWorkingView(convertView); 
     ViewHolder viewHolder = getViewHolder(view); 
     shopClass shop = getItem(position); 

     if (shopObjects == null) { 
      System.out.println("it is null on CustomListarrya"); 
     } 

     // Setting the title view is straightforward 
     viewHolder.titleView.setText(shop.getName()); 
     viewHolder.subTitleView.setText(shop.getDescription()); 

//  TextView title = (TextView) alertView.findViewById(R.id.title); 
//  TextView description = (TextView) alertView.findViewById(R.id.description); 
//  LinearLayout bkgImage = (LinearLayout) alertView.findViewById(R.id.bkgImage); 
// 
//  // getting movie data for the row 
// 
//  // bkg image 
////  bkgImage.setBackground(); 
// 
//  // title 
//  title.setText(shop.getName()); 
// 
//  // description 
//  description.setText(shop.getDescription()); 
     return view; 
    } 

    private View getWorkingView(final View convertView) { 
     // The workingView is basically just the convertView re-used if possible 
     // or inflated new if not possible 
     View workingView = null; 

     if(null == convertView) { 
      final Context context = getContext(); 
      final LayoutInflater inflater = (LayoutInflater)context.getSystemService 
        (Context.LAYOUT_INFLATER_SERVICE); 

      workingView = inflater.inflate(resource, null); 
     } else { 
      workingView = convertView; 
     } 

     return workingView; 
    } 

    private ViewHolder getViewHolder(final View workingView) { 
     // The viewHolder allows us to avoid re-looking up view references 
     // Since views are recycled, these references will never change 
     final Object tag = workingView.getTag(); 
     ViewHolder viewHolder = null; 


     if(null == tag || !(tag instanceof ViewHolder)) { 
      viewHolder = new ViewHolder(); 

      viewHolder.titleView = (TextView) workingView.findViewById(R.id.title); 
      viewHolder.subTitleView = (TextView) workingView.findViewById(R.id.description); 
      //viewHolder.imageView = (ImageView) workingView.findViewById(R.id.news_entry_icon); 

      workingView.setTag(viewHolder); 

     } else { 
      viewHolder = (ViewHolder) tag; 
     } 

     return viewHolder; 
    } 

    /** 
    * ViewHolder allows us to avoid re-looking up view references 
    * Since views are recycled, these references will never change 
    */ 
    private static class ViewHolder { 
     public TextView titleView; 
     public TextView subTitleView; 
     //public ImageView imageView; 
    } 

} 

ShopActivity.java

public class ShopActivity extends AppCompatActivity { 
    ListView shopList; 
    CustomListAdapter adapter; 
    private ArrayList<shopClass> shopObjects; 

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

     shopList = (ListView) findViewById(R.id.list); 
     ShopData start = new ShopData(); 

     shopObjects = start.getShopData(); 
     adapter = new CustomListAdapter(this, shopObjects, R.layout.list_row); 
     for (int i = 0; i<2; i++){ 
      System.out.println(shopObjects.get(i).toString()); 
      adapter.add(shopObjects.get(i)); 
     } 
     shopList.setAdapter(adapter); 


    } 

Activity_shop.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.csthack.beinnovative.destination_brooklyn.ShopActivity" 
    tools:showIn="@layout/activity_shop"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent"> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:divider="@color/list_divider" 
     android:dividerHeight="1dp" 
     android:listSelector="@drawable/list_row_selector" /> 
    </LinearLayout> 
</LinearLayout> 

listrow.xml

<?xml version="1.0" encoding="utf-8"?> 
<!--Style for the list--> 

<android.support.percent.PercentRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" > 
    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/list_row_selector" 
     app:layout_heightPercent="33%" 
     app:layout_widthPercent="100%"> 

     <!-- Background Image --> 
     <LinearLayout 
      android:id="@+id/bkgImage" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/coffe" 
      android:orientation="vertical" 
      android:alpha="0.65"/> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_gravity="bottom|start" 
      android:layout_alignParentBottom="true" 
      android:paddingLeft="10dp"> 

      <!-- Shop Title --> 
      <TextView 
       android:id="@+id/title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Test" 
       android:textSize="@dimen/title" 
       android:textStyle="bold" 
       android:textColor="@color/genre" 
       android:alpha="1.0"/> 

      <!-- Description --> 
      <TextView 
       android:id="@+id/description" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/title" 
       android:layout_marginTop="5dp" 
       android:layout_marginBottom="10dp" 
       android:text="blah blah blahg blah blah blah " 
       android:textColor="@color/genre" 
       android:textSize="@dimen/genre" /> 

     </LinearLayout> 
    </RelativeLayout> 
</android.support.percent.PercentRelativeLayout> 

Ich habe mir andere Tutorials angeschaut, aber alle scheinen nach der gleichen Logik zu funktionieren und versuchten 3, aber immer noch keinen Inhalt.

Log:

04-10 03:12:44.384 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.388 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.391 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.393 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.395 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.397 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.398 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.399 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.400 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.401 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.402 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.403 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.404 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.404 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.431 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.432 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.433 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.434 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.435 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.436 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.437 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.438 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.439 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.440 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.441 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.441 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.442 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.443 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.445 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.448 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.451 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.454 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.456 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.459 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.462 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.484 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.485 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.486 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.487 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.488 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.489 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.490 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.491 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.492 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.492 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.493 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.494 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.495 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.496 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.521 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.523 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.524 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.525 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.526 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.527 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.528 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.529 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.530 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.531 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.531 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.532 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.533 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
04-10 03:12:44.534 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: [email protected] 
+0

Ich schlage vor, Sie einige Protokolle zu drucken, um zu sehen, wie oft "getView" aufgerufen wird und für jede Zeit, die getView aufgerufen wird, ob 'shopClass shop = getItem (position);' eine korrekte Shop-Instanz zurückgibt. – shhp

+0

@shhp jetzt stürzt es ab und ich bin mir nicht sicher warum. – baldemora

+0

Es scheint 'shopList = (ListView) findViewById (R.id.list);' gibt ein Null-Objekt zurück. Bist du sicher, dass das "R" hier dein Paket statt Android ist? – shhp

Antwort

0

Nur für den Fall, dass jemand ein ähnliches Problem hat, war das Problem das <android.support.percent.PercentRelativeLayout Schema funktionierte nicht richtig, sobald ich ein anderes verwendet, gab es kein Problem.

0

Vielleicht ist das Problem ist, weil Ihr ListView ‚s Höhe nicht korrekt ist. Versuchen Sie, android:layout_height von wrap_content zu zu ändern.

+0

zeigt immer noch leer, aber jetzt ruft es nur getView 5 mal. Wie viele Objekte habe ich – baldemora

Verwandte Themen