Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My gridview shows only one row

//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 values in my gridview. But i have morethan 5 values for gridview. Why is it not showing the other values. I tried a lot but no use. Does anyone have solution.

like image 561
Jithin Varghese Avatar asked Mar 18 '15 10:03

Jithin Varghese


1 Answers

This is probably caused by having GridView inside of ScrollView. Since both layouts are scrollable this causes a lot of problems, in your case the height of GridView cannot be properly determined and the scroll events are eaten by the ScrollView.

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

What you should do is put only the HeaderGridView to your xml inflated by the 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 view in different xml (for example 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 the header 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 it, worked like a charm. Hope it helps!

like image 170
Lamorak Avatar answered Nov 09 '22 07:11

Lamorak