2016-04-10 139 views
0

我在ArrayList上創建了數據對象shopObjects。我仔細檢查他們是否已滿。不是空的,並試圖創建一個適配器。這是我第一次這樣做,而且名單是空的。我不確定我做錯了什麼。LIstView適配器沒有顯示自定義適配器的內容

**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> 

我看了其他教程,但他們似乎都圍繞着同樣的邏輯工作,並嘗試3但仍然沒有內容。

登錄:

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

我建議你打印一些日誌,看看調用了多少次'getView',並且每次'getView'被調用是否'shopClass shop = getItem(position);'返回一個正確的商店實例。 – shhp

+0

@shhp現在它崩潰,我不知道爲什麼。 – baldemora

+0

看來'shopList =(ListView)findViewById(R.id.list);'返回一個空對象。你確定這裏的'R'是你的包而不是android嗎? – shhp

回答

0

以防萬一任何人有類似的問題的問題是<android.support.percent.PercentRelativeLayout架構不能正常工作,一旦我使用了一個不同的問題沒有問題。

0

也許問題是因爲你的ListView的高度是不正確的。嘗試將android:layout_heightwrap_content更改爲match_parent

+0

仍顯示爲空,但現在它只調用getView 5次。這是我有多少物體 – baldemora

相關問題