2016-08-18 78 views
0

也許,有人可以給我一些原因,這不順心.. 我想學習這個教程 (http://sudarmuthu.com/blog/using-arrayadapter-and-listview-in-android-applications/) 但我想將此自定義適配器。 。無法在ArrayAdapter達到getView <>

menuListList.setAdapter(new ArrayAdapter<String>(MenuListActivity.this, R.layout.menu_list){ 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View row; 

      if (convertView == null){ 
       row = getLayoutInflater().inflate(R.layout.menu_list, null); 
      } else { 
       row = convertView; 
      } 
      Log.w("tes", "te3s"); 
      TextView menuListTitle = (TextView) row.findViewById(R.id.menuListTitle); 
      menuListTitle.setText(titleList[position]); 

      return row; 

     } 
    }); 

我之前和menuListList.setAdapter(後到達日誌),但我不能趕上日誌裏面和我的ListView(menuListList)變成空白

這是我的WH OLE類

public class MenuListActivity extends AppCompatActivity { 

ListView menuListList; 
String[] titleList = { 
     "Purchase Order", 
     "Receive Item", 
     "Expired Item" 
}; 
Integer[] imageList = { 
     R.drawable.po, 
     R.drawable.ri, 
     R.drawable.ex 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_menu_list); 
    menuListList = (ListView)findViewById(R.id.menuListList); 
    Log.w("tes", "tes1"); 
    menuListList.setAdapter(new ArrayAdapter<String>(MenuListActivity.this, R.layout.menu_list){ 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View row; 

      if (convertView == null){ 
       row = getLayoutInflater().inflate(R.layout.menu_list, parent, false); 
      } else { 
       row = convertView; 
      } 
      Log.w("tes", "te2s"); 
      TextView menuListTitle = (TextView) row.findViewById(R.id.menuListTitle); 
      menuListTitle.setText(titleList[position]); 

      return row; 

     } 
    }); 
    Log.w("tes", "te3s"); 
} 

}

我能達到 「TES1」 和 「te3s」 的log.w但不是 「te2s」

+0

粘貼完整的類,我們可以幫助你。 –

+0

你在擴展listActivtiy嗎? –

+0

哪裏是適配器的數據源?傳遞數組titleList作爲ArrayAdapter構造函數中的第三個參數https://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context,int,T []) – Raghunandan

回答

1

這是你的完整的例子

代碼您提供的代碼片段可從此示例中獲得

listview需要,因爲您正在擴展ListActivity.

public class MainActivity extends ListActivity { 
    private List<String> strings; 
    private LayoutInflater mInflater; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     strings = new ArrayList<String>(); 

     strings.add("This"); 
     strings.add("is"); 
     strings.add("a"); 
     strings.add("long"); 
     strings.add("list"); 
     strings.add("which"); 
     strings.add("will"); 
     strings.add("be"); 
     strings.add("displayed"); 
     strings.add("in"); 
     strings.add("the"); 
     strings.add("screen"); 
     strings.add("with"); 
     strings.add("one"); 
     strings.add("word"); 
     strings.add("in"); 
     strings.add("a"); 
     strings.add("line"); 

     setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, strings) { 
      @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 
       View row; 

       if (null == convertView) { 
        row = mInflater.inflate(R.layout.list_item, null); 
       } else { 
        row = convertView; 
       } 

       TextView tv = (TextView) row.findViewById(android.R.id.text1); 
       tv.setText(getItem(position)); 

       return row; 
      } 
     }); 
    } 
} 

更新定義適配器全部演示

MainActivity

public class MainActivity extends Activity { 

    Context context; 

    private CoustomAdapter coustomAdapter; 
    private ListView menuListList; 
    private ArrayList<CategoriesVo> dataList; 


    String[] titleList = { 
      "Purchase Order", 
      "Receive Item", 
      "Expired Item" 
    }; 
    Integer[] imageList = { 
      R.drawable.image_1, 
      R.drawable.image_1, 
      R.drawable.image_1 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     context = this; 

     //connect list view 
     menuListList = (ListView) findViewById(R.id.menuListList); 
     // data array for coustom adapter 
     dataList = new ArrayList<>(); 

     //Adding data to array then we pass it to our adapter 
     for (int i = 0; i < titleList.length; i++) { 

      CategoriesVo categoriesVo = new CategoriesVo(); 
      categoriesVo.setImage(imageList[i]); 
      categoriesVo.setCname(titleList[i]); 
      dataList.add(categoriesVo); 
     } 

     // passing to adatepet 
     coustomAdapter = new CoustomAdapter(this, dataList); 
     menuListList.setAdapter(coustomAdapter); 
    } 
} 

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 


    <ListView 
     android:id="@+id/menuListList" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 
</RelativeLayout> 

CoustomAdapter

public class CoustomAdapter extends BaseAdapter { 

    Context myContext; 
    private static LayoutInflater inflater = null; 
    ArrayList<CategoriesVo> data; 

    public CoustomAdapter(Context context, ArrayList<CategoriesVo> data) { 
     myContext = context; 
     this.data = data; 
     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public int getCount() { 
     return data.size(); 
    } 

    @Override 
    public CategoriesVo getItem(int position) { 
     return data.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(final int position, View convertView, final ViewGroup parent) { 
     View row = convertView; 
     ViewHolder holder; 

     if (convertView == null) { 
      row = inflater.inflate(R.layout.row_layout, null); 

      holder = new ViewHolder(); 
      holder.title = (TextView) row.findViewById(R.id.textView); 
      holder.image = (ImageView) row.findViewById(R.id.imageView); 
      row.setTag(holder); 
     } else { 
      holder = (ViewHolder) row.getTag(); 
     } 

     CategoriesVo item = data.get(position); 
     holder.title.setText(item.getCname()); 
     holder.image.setImageResource(item.getImage()); 
     return row; 
    } 

    static class ViewHolder { 

     TextView title; 
     ImageView image; 
    } 
} 

row_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/base" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" 
    android:orientation="horizontal" 
    android:weightSum="10"> 


    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" /> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="20dp" 
     android:layout_toEndOf="@+id/imageView" 
     android:text="New Text" /> 
</RelativeLayout> 

CategoriesVo

public class CategoriesVo { 

    String cid, cname; 
    int image; 

    public CategoriesVo() { 
    } 

    public String getCid() { 
     return cid; 
    } 

    public void setCid(String cid) { 
     this.cid = cid; 
    } 

    public int getImage() { 
     return image; 
    } 

    public void setImage(int image) { 
     this.image = image; 
    } 

    public String getCname() { 
     return cname; 
    } 

    public void setCname(String cname) { 
     this.cname = cname; 
    } 
} 
+0

如果還有問題,請將您的全班同學粘貼在這裏我會修復它。 –

+0

我不能將它粘貼在這裏 – Ampersanda

+0

編輯您的問題,並將其添加爲更新。 –

相關問題