我的gridview只显示一行

时间:2015-03-18 10:42:34

标签: android android-layout gridview

//我的xml代码

<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;
   }
}

上面的布局在gridview中只显示了一行值。但是我对gridview有5个以上的值。为什么它没有显示其他值。我试了很多但没有用。有没有人有解决方案。

2 个答案:

答案 0 :(得分:14)

这可能是由GridViewScrollView引起的。由于两个布局都是可滚动的,因此会导致很多问题,在这种情况下,GridView的高度无法正确确定,滚动事件会被ScrollView吃掉。

使用ListView,您只需将ListView声明为根元素,然后将其他可滚动内容添加为页眉或页脚。 GridView本身不支持这一点,但幸运的是HeaderGridView的子类实现解决了这个问题。

你应该做的只是将HeaderGridView放到活动膨胀的xml中。

<?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" />

然后将RelativeLayout实现为不同xml中的标题视图(例如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>

在您的活动中,您将标题添加到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(...))

尝试过,像魅力一样工作。希望它有所帮助!

答案 1 :(得分:0)

我发现的问题和修复是,如果gridview有行数据绑定,那么select语句必须在我的情况下具有该特定字段我正在评估Grade并且​​此等级未包含在我的select语句中且gridview返回单个除了我在选择语句中包含成绩的那一刻,问题已经解决了

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