0

我想在我填充了 arrayadapter 项的 gridview 的备用单元格上设置颜色,我也不想扩展 BaseAdapter 类:

我用过的代码:

     public class Fragment2 extends Fragment {
    public final static String EXTRA_MESSAGE = "com.MESSAGE";
   TextView selection;
      public int idToModify;
    DataManipulator dm;
   private GridView gridView;
    public static ArrayList<String> ArrayName = new ArrayList<String>();
    List<Object[]> names2 = null;
 String[] stg1;
     String[] stgid;

  @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag2, container, false);
     }

   @Override
  public void onStart() {
  super.onStart();

   dm = new DataManipulator(getActivity());
   names2 = dm.selectAll();

   stg1 = new String[names2.size()];
   stgid = new String[names2.size()];
   int x = 0;

   for (Object[] name : names2) {
       stgid[x] = (String) name[0];
       stg1[x] = (String) name[1] + " - " + (String) name[2] + " - "
               + (String) name[3] + " - " + (String) name[4];
       x++;
   }

   gridView = (GridView) getView().findViewById(R.id.gridView1);

   ArrayAdapter<String> adapter = new ArrayAdapter<String>(
           getActivity(), android.R.layout.simple_list_item_1, stg1);

   gridView.setAdapter(adapter);

 }
}
 }

我也尝试扩展 Base 适配器类并成功,但我只是用 arrayadapter 填充 gridview 并希望在不扩展代码的情况下为单元格设置颜色。谢谢

4

1 回答 1

0

而不是 android.R.layout.simple_list_item_1 应该传递您的自定义布局。

   ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            getActivity(), R.layout.custom_layout, stg1);

custom_layout.xml 的样子

 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ef0"
  />
于 2013-02-20T06:47:59.493 回答