0

I am trying to display the data in image and text in a gridview of 2 by 2. I can already fetch the data from the xml using json to fetch it. Below is the my code. I have commented the part where I am trying to display it in girdview, it crashes when that line of code is being run. What have I done wrong.

public class NewArrivalsFragment extends Fragment {
    private TextView tv;
    private Context c;
    private JSONObject json;
    private JSONArray products;
    private List<String> list;
    private GridView gridView;
    private String[] products_array;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    c = getActivity();



    View view = inflater.inflate(R.layout.fragment_general, container,
            false);
    tv = (TextView) view.findViewById(R.id.textView1);
    tv.setText(getJSON());

    //gridView = (GridView) view.findViewById(R.id.gridview);

    /*
    GridView gridview = (GridView) view.findViewById(R.id.textView1);
    //gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(c, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
    */

    return view;
}

private String getJSON() {
    String returnStr = "";
    SQLiteHelper sqliteHelper = new SQLiteHelper(c);
    SQLiteDatabase database = sqliteHelper.getReadableDatabase();       
    Cursor cursor = database.rawQuery("SELECT * FROM "+ MyGlobalConstant.TABLE_JSON +" WHERE " +
            MyGlobalConstant.JSON_NAME +" = 'Product'", null);          
    while (cursor.moveToNext()) {       
        returnStr = cursor.getString(cursor.getColumnIndex(MyGlobalConstant.JSON_DATA));
    }

    cursor.close();
    database.close();


    return returnStr;
}

}

fragment_general.xml

<?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:gravity="center" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
Gene Lim
  • 1,068
  • 2
  • 14
  • 36

1 Answers1

0

Modify your Layout with this

 <?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:gravity="center" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="About Us..."
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <GridView
        android:id="@+id/gridView1"
        android:numColumns="auto_fit"
        android:gravity="center"
        android:columnWidth="50dp"
        android:stretchMode="columnWidth"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    </LinearLayout>

Hope this could help ...

Nitesh Tiwari
  • 4,742
  • 3
  • 28
  • 46