我想使用ArrayAdapter / BaseAdapter将数据显示到gridview,但是当我单击片段时总是停止。如果使用Toast显示,数据可以显示,但是如果gridview我setAdapter变为强制关闭并且没有显示错误通知。
这是我的片段:
public class BlankFragment extends Fragment {
public static final String url="http://10.0.2.2/db_m_market_localhost/action/select_picture.php";
GridView gridView;
ArrayList<object_product> arrayProduk;
Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.laptop_gridview, container, false);
arrayProduk = new ArrayList<>();
getData();
gridView = (GridView)view.findViewById(R.id.gv);
return view;
}
private void getData(){
//Creating a json array request to get the json from our api
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Displaying our grid
showGrid(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
//Creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
//Adding our request to the queue
requestQueue.add(jsonArrayRequest);
}
private void showGrid(JSONArray jsonArray){
//Looping through all the elements of json array
for(int i = 0; i<jsonArray.length(); i++){
arrayProduk.clear();
object_product ob;
//Creating a json object of the current index
JSONObject obj = null;
try {
obj = jsonArray.getJSONObject(i);
//ambil data from mysql
int id = obj.getInt("product_id");
String name = obj.getString("product_nm");
int hrg = obj.getInt("product_price");
String descrip = obj.getString("product_desc");
double brt = obj.getDouble("product_weight");
int jmlh = obj.getInt("product_count");
String clr = obj.getString("product_color");
String imageUrl = obj.getString("product_img");
//masukan ke file object_product
ob= new object_product();
ob.setId(id);
ob.setNama(name);
ob.setDesc(descrip);
ob.setHrg(hrg);
ob.setImgUrl(imageUrl);
ob.setBrt(brt);
ob.setJmlh(jmlh);
ob.setColor(clr);
//add to arraylist
arrayProduk.add(ob);
Toast.makeText(getContext(), ""+imageUrl, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
//Creating GridViewAdapter Object
GridViewAdapter gridViewAdapter = new GridViewAdapter(getContext(), arrayProduk);
//Adding adapter to gridview
//gridView.setAdapter(new GridViewAdapter(this.getActivity(), arrayProduk));
gridView.setAdapter(gridViewAdapter);
}
}
这是我的目标:
public class object_product {
int id, jmlh, hrg;
double brt;
String nama, desc, imgUrl, color;
//buat set and get
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getJmlh() {
return jmlh;
}
public void setJmlh(int jmlh) {
this.jmlh = jmlh;
}
public int getHrg() {
return hrg;
}
public void setHrg(int hrg) {
this.hrg = hrg;
}
public double getBrt() {
return brt;
}
public void setBrt(double brt) {
this.brt = brt;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
这是我的适配器:
public class GridViewAdapter extends BaseAdapter {
//Context
Context context;
//Array List that would contain the urls and the titles for the images
ArrayList<object_product> arrayProduct;
LayoutInflater layoutInflater;
public GridViewAdapter (Context context, ArrayList<object_product> arrayProduct){
//Getting all the values
this.context = context;
this.arrayProduct = arrayProduct;
}
@Override
public int getCount() {
return arrayProduct.size();
}
@Override
public Object getItem(int position) {
return arrayProduct.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Creating a linear layout
if(convertView == null){
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.laptop_item_gridview, parent,false);
}
//object_product product = object_product.getInstance();
ImageView imageView = (ImageView)convertView.findViewById(R.id.img);
TextView textView= (TextView)convertView.findViewById(R.id.t1);
TextView textView1 = (TextView)convertView.findViewById(R.id.t2);
object_product product =(object_product) this.getItem(position);
//sett text
textView.setText(product.getNama());
textView1.setText(product.getHrg());
//use picasso
Picasso.with(context).load(product.getImgUrl()).into(imageView);
return convertView;
}
}
我的日志:
05-21 07:20:16.960 28150-28150/com.example.jemmycalak.thisismymarket E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jemmycalak.thisismymarket, PID: 28150
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.jemmycalak.thisismymarket.Adapter.GridViewAdapter.getView(GridViewAdapter.java:78)
at android.widget.AbsListView.obtainView(AbsListView.java:2263)
at android.widget.GridView.onMeasure(GridView.java:1044)
at android.view.View.measure(View.java:16497)
at android.support.v4.widget.SwipeRefreshLayout.onMeasure(SwipeRefreshLayout.java:614)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:735)
at android.view.View.measure(View.java:16497)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1079)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(Zygote
05-21 07:20:16.960 1590-1836/system_process W/ActivityManager: Force finishing activity com.example.jemmycalak.thisismymarket/.MainActivity
05-21 07:20:16.990 1590-1836/system_process D/dalvikvm: GC_FOR_ALLOC freed 533K, 22% free 8131K/10408K, paused 9ms, total 10ms
05-21 07:20:17.010 1590-1817/system_process I/ActivityManager: Delay finish: com.google.android.gms/.stats.service.DropBoxEntryAddedReceiver
05-21 07:20:17.060 1715-1728/com.google.android.gms.persistent D/dalvikvm: GC_FOR_ALLOC freed 937K, 18% free 5063K/6124K, paused 8ms, total 8ms
05-21 07:20:17.070 1590-1602/system_process I/ActivityManager: Resuming delayed broadcast
05-21 07:20:17.500 1590-1605/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{afe0fba0 u0 com.example.jemmycalak.thisismymarket/.MainActivity t28 f}
05-21 07:20:17.690 1748-1748/com.android.launcher V/RenderScript: 0xb7b0ece0 Launching thread(s), CPUs 2
05-21 07:20:28.150 1590-1605/system_process W/ActivityManager: Activity destroy timeout for ActivityRecord{afe0fba0 u0 com.example.jemmycalak.thisismymarket/.MainActivity t28 f}