android – 我的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>

//主要活动

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,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个以上的值.为什么它没有显示其他值.我尝试了很多但没有用.有没有人有解决方案.

解决方法

这可能是由ScrollView中的GridView引起的.由于两个布局都是可滚动的,这会导致很多问题,在这种情况下,无法正确确定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(...))

尝试过它,就像一个魅力.希望能帮助到你!

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...