如何使用Fragment中的自定义布局从Cursor填充GridView

时间:2016-10-13 10:55:39

标签: android android-fragments 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>

以上只显示一个简单的列表而不是自定义列表。

要启动自定义适配器进程,我已设置项目列表布局:

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

如何添加自定义适配器,该适配器将使用项目列表布局和框显示网格视图。

我在寻找:

enter image description here

2 个答案:

答案 0 :(得分:2)

这应该可以使自定义适配器并将该适配器设置为gridview
gv.setAdapter(new CustomAdapter(this,prgmNameList,prgmImages));

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;
    }

} 

答案 1 :(得分:2)

尝试在ArrayAdapter构造函数中插入您自己的自定义布局( R.layout.custom_layout ),而不是安装android(*** android。** R.layout.simple_list_item_1 *)

所以E.g     ArrayAdapter arrayAdapter = new ArrayAdapter(getActivity(),R.layout.custom_layout,arrCursor);