2016-10-13 4 views
0

私はいくつかの調査を行いましたが、良い例は見つかりませんでした。フラグメントのカスタムレイアウトを使用して、カーソルからGridViewを作成する方法

現在の設定:

ArrayList<String> arrCursor = new ArrayList<String>(); 
List<mydata> md = mydatabase.getthedata("1"); 
for (mydata cn1 : md) { 
    arrCursor.add(cn1.getTheFirst()); 
    arrCursor.add(cn1.getTheSecond()); 
} 
ArrayAdapter arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrCursor); 
gv.setAdapter(arrayAdapter); //the gridview 

GridViewのレイアウト:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.myapp.chao"> 
    <GridView 
     android:id="@+id/gvalldata" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:numColumns="auto_fit" 
     android:gravity="center" 
     android:stretchMode="columnWidth" 
     android:columnWidth="90dp" 
     android:horizontalSpacing="10dp" 
     android:verticalSpacing="10dp" /> 
</FrameLayout> 

上記だけの単純なリストではなく、カスタム1を表示します。

カスタムアダプタ・プロセスを開始するには、私はアイテムリストのレイアウトを設定している:

<?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="match_parent" 
    android:orientation="vertical" 
    android:padding="10dp"> 

    <TextView 
     android:text="TextView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/tvfirst" 
     android:layout_marginBottom="10dp" 
     android:gravity="center" 
     android:textSize="25dp" /> 

    <TextView 
     android:text="TextView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/tvsecond" /> 

</LinearLayout> 

にはどうすればアイテムリストレイアウトを使用してGridViewコントロールを表示し、ボックスになりますカスタムアダプタを追加することができます。私が探しています何

:これは、カスタムアダプタを作る仕事をし
gv.setAdapter(新しいCustomAdapter(これ、prgmNameList、prgmImages))をGridViewのためにそのアダプタを設定する必要があり

enter image description here

+0

例が見つかりました:http://www.appsrox.com/android/tutorials/showcase/8/ ... – Si8

+1

'ArrayAdapter'はあなたのアプリケーションで動作しますが、代わりに' CursorAdapter'を使用することをお勧めします。データセットの反復処理や配列へのコピーが不要になります。はるかに効率的で、notifyDataSetChanged()がアダプタで呼び出されたときにすぐにデータを更新します。 [Here](https://github.com/codepath/android_guides/wiki/Populating-a-ListView-with-a-CursorAdapter)はその例です。 –

+0

@GaryBakそれはうまくいくかもしれません。私はそれをテストしています。ありがとう。 ListViewの代わりに私はGridViewを使用しています。 – Si8

答えて

2

public class CustomAdapter extends BaseAdapter{ 

    String [] result; 
    Context context; 
int [] imageId; 
     private static LayoutInflater inflater=null; 
    public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) { 
     // TODO Auto-generated constructor stub 
     result=prgmNameList; 
     context=mainActivity; 
     imageId=prgmImages; 
     inflater = (LayoutInflater)context. 
       getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return result.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public class Holder 
    { 
     TextView tv; 
     ImageView img; 
    } 
    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     Holder holder=new Holder(); 
     View rowView; 

      rowView = inflater.inflate(R.layout.program_list, null); 
      holder.tv=(TextView) rowView.findViewById(R.id.textView1); 
      holder.img=(ImageView) rowView.findViewById(R.id.imageView1); 

     holder.tv.setText(result[position]); 
     holder.img.setImageResource(imageId[position]); 

     rowView.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show(); 
      } 
     }); 

     return rowView; 
    } 

} 
+0

代わりに 'prgmNameList'と' prgmImages'を送信するのではなく、 'arrCursor'を送信して代わりに使うべきですか? – Si8

2

はArrayAdapterコンストラクタの代わりに、Androidの一つに、独自のカスタムレイアウト(R.layout.custom_layout)を挿入してください(***アンドロイド。** R.layout.simple_list_item_1 *)

したがって、例えば ArrayAdapter arrayAdapter = new ArrayAdapter(getActivity()、R.layout.custom_layout、arrCursor);

+0

私はそれを使うとアプリケーションがクラッシュします。 – Si8

+0

申し訳ありませんが、カスタムアダプターも必要であることを忘れてしまいました。しかし、あなたはソートされていればいいと思う。 – CaptainBluMagic

関連する問題