我试图以2乘2的网格视图显示图像和文本中的数据。我已经可以使用json从xml中获取数据来获取它。以下是我的代码。我已经评论了我试图在girdview中显示它的部分,它在运行该行代码时崩溃了。我做错了什么
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>
答案 0 :(得分:0)
使用此
修改布局 <?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>
希望这可以帮助......