Only one row is displayed in my gridview

// My xml code

<ScrollView android:id="@+id/scrollview" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginTop="35dp"> <RelativeLayout android:id="@+id/relativeLayout2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:background="@android:color/background_light" android:gravity="center" android:paddingBottom="5dp" > <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView3" android:layout_below="@+id/textView3" android:text="Venue, Date" android:textSize="12sp" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView4" android:layout_below="@+id/textView4" android:text="Description" android:textSize="12sp" android:paddingBottom="5dp" /> <ImageView android:id="@+id/imageView7" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView2" android:layout_marginTop="14dp" android:src="@drawable/demo" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:text="Event of the Week" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/imageView7" android:layout_marginLeft="14dp" android:text="Event Name" /> <GridView android:id="@+id/gridView1" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_below="@+id/textView6" android:layout_marginTop="14dp" android:horizontalSpacing="10dp" android:numColumns="2" android:paddingBottom="5dp" android:verticalSpacing="10dp" /> <TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView5" android:layout_centerHorizontal="true" android:layout_marginTop="14dp" android:text="Today&apos;s Events" /> </RelativeLayout> </ScrollView> 

// MainActivity

 class LoadProfile extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(EventHome.this); pDialog.setMessage("Loading..."); pDialog.setIndeterminate(false); pDialog.setCancelable(false); pDialog.show(); } protected String doInBackground(String... args) { // Building Parameters String json = null; PROFILE_URL = "http://www.example.com/filter_event_android.php?pin="+phone; try { List<NameValuePair> params = new ArrayList<NameValuePair>(); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(PROFILE_URL); httppost.setEntity(new UrlEncodedFormEntity(params)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); json = EntityUtils.toString(resEntity); Log.i("All Events: ", json.toString()); } catch (Exception e) { e.printStackTrace(); } return json; } @SuppressLint("InlinedApi") @Override protected void onPostExecute(String json) { super.onPostExecute(json); pDialog.dismiss(); try{ event = new JSONObject(json); final ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>(); JSONArray user = event.getJSONArray("events"); String contains=json.toString(); if(contains.contains("id")) { for (int i = 0; i < user.length(); i++) { JSONObject object = user.getJSONObject(i); HashMap<String, String> map = new HashMap<String, String>(); map.put("id", object.getString("id")); map.put("name", object.getString("name")); map.put("date_d", object.getString("date_d")); map.put("location", object.getString("location")); map.put("images", "http://www.example.com/"+object.getString("images")); arraylist.add(map); } String[] from = {"name", "date_d", "location", "images"}; int[] to = {R.id.textView1, R.id.textView2, R.id.textView3, R.id.iv_flag}; ListAdapter adapters = new MyAdapter(EventHome.this,arraylist,R.layout.list_event_home,from,to); gv1.setAdapter(adapters); } else { gv1.setVisibility(View.GONE); TextView dynamicTextView = new TextView(EventHome.this); dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); dynamicTextView.setText("No events available"); } gv1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Intent i = new Intent(EventHome.this,EventSingle.class); i.putExtra("event_id", arraylist.get(arg2).get("id")); startActivity(i); } }); }catch(Exception e) { e.printStackTrace(); } } } 

//MyAdapter.java

 public class MyAdapter extends SimpleAdapter{ public MyAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to){ super(context, data, resource, from, to); } public View getView(int position, View convertView, ViewGroup parent){ // here you let SimpleAdapter built the view normally. View v = super.getView(position, convertView, parent); // Then we get reference for Picasso ImageView img = (ImageView) v.getTag(); if(img == null){ img = (ImageView) v.findViewById(R.id.iv_flag); v.setTag(img); // <<< THIS LINE !!!! } // get the url from the data you passed to the `Map` @SuppressWarnings("rawtypes") String url = (String) ((Map)getItem(position)).get("images"); // do Picasso Picasso.with(v.getContext()).load(url).into(img); // return the view return v; } } 

The above layout shows only one row value in my gridview. But I have more than 5 values ​​for gridview. Why it does not show other meanings. I tried a lot, but did not use. Anyone have a solution.

+6
source share
2 answers

This is probably due to the fact that the GridView inside the ScrollView . Since both layouts scroll, this causes a lot of problems, in your case the height of the GridView cannot be determined correctly, and the scroll events will be eaten using ScrollView .

With ListView you can simply declare a ListView as the root element, and then add other scrollable content as headers and footers. GridView does not initially support this, but, fortunately, there is a subclass implementation of HeaderGridView that solves this problem.

What you need to do is to put only HeaderGridView in your xml, overestimated by activity.

 <?xml version="1.0" encoding="utf-8"?> <your.package.HeaderGridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:horizontalSpacing="10dp" android:numColumns="2" android:verticalSpacing="10dp" /> 

and then implement the RelativeLayout as a header in different xml (e.g. header.xml )

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativeLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:background="@android:color/background_light" android:gravity="center" android:paddingBottom="5dp"> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView3" android:layout_below="@+id/textView3" android:text="Venue, Date" android:textSize="12sp" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView4" android:layout_below="@+id/textView4" android:text="Description" android:textSize="12sp" android:paddingBottom="5dp" /> <ImageView android:id="@+id/imageView7" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView2" android:layout_marginTop="14dp" android:src="@drawable/demo" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:text="Event of the Week" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/imageView7" android:layout_marginLeft="14dp" android:text="Event Name" /> <TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView5" android:layout_centerHorizontal="true" android:layout_marginTop="14dp" android:text="Today&apos;s Events" /> </RelativeLayout> 

In your activity, you add a title to your HeaderGridView

 HeaderGridView gridView = (HeaderGridView) findViewById(R.id.gridView1); View header = getLayoutInflater().inflate(R.layout.header, gridView, false); gridView.addHeaderView(header); // set adapter AFTER adding headerViews gridView.setAdapter(MyAdapter(...)) 

Tried, worked like a charm. Hope this helps!

+11
source

The problem and fix that I discovered is that if the gridview has row-bound data, the select statement should have this particular field in my case, I evaluate the class, and this class was not included in my select statement, and gridview returned a single the string no matter what, but the moment I included the class in my selection, the problem was fixed.

 Protected Sub GVRos_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GVRos.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then Dim stat As String 'This row evaluates and puts the value in stat as per the Finalised columon result stat = DataBinder.Eval(e.Row.DataItem, "Grade").ToString() If stat = "A" Then e.Row.BackColor = System.Drawing.Color.LightGreen ElseIf stat = "B" Then e.Row.BackColor = System.Drawing.Color.LightBlue ElseIf stat = "C" Then e.Row.BackColor = System.Drawing.Color.Yellow ElseIf stat = "D" Then e.Row.BackColor = System.Drawing.Color.Orange ElseIf stat = "F" Then e.Row.BackColor = System.Drawing.Color.Red Else e.Row.BackColor = System.Drawing.Color.White End If End If End Sub 
+1
source

Source: https://habr.com/ru/post/983957/


All Articles