2015-03-18 10 views
6

// My xml koduBenim GRIDVIEW yalnızca bir satır

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

yukarıdaki düzen yalnızca bir satır gösterir // gösterir benim gridview değerlerim. Ama gridview için 5 değerim var. Neden diğer değerleri göstermiyor. Çok denedim ama kullanmadım. Herkesin çözümü var mı?

+1

Bu soruyu cevaplamak için adaptör dosyanızı sağlamanız gerektiğini düşünüyorum. – chartsai

+0

ilk kaydırılabilir kaydırılabilir içine koyarak ... ikinci, * düzeni benim gridview içinde sadece bir satır değerleri gösterir * neden kodu olmadan (özellikle adaptör hakkında bilgi olmadan) söyleyin zor – Selvin

+0

@Selvin: - Bağdaştırıcı için soruyu kontrol edin . Soruyu güncelledim. –

cevap

10

Bu, ScrollView'un içinde GridView bulunması nedeniyle ortaya çıkar. Her iki mizanpaj kaydırılabilir olduğundan bu çok sorun çıkarır, sizin durumunuzda GridView'un yüksekliği uygun şekilde belirlenemez ve kaydırma olayları ScrollView tarafından yenilir.

ListView ile ListView kök öğesi olarak bildirebilir ve sonra da diğer kaydırılabilir içeriği üstbilgi veya altbilgi olarak ekleyebilirsiniz. GridView doğal olarak bunu desteklemez ancak neyse ki bu sorunu çözen HeaderGridView'un alt sınıf uygulaması vardır.

Yapmanız gereken şey, etkinlikle şişirilmiş olan xml'nize yalnızca HeaderGridView koymanızdır.

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

ve daha sonra farklı xml bir başlık görünümü olarak RelativeLayout uygulamak etkinlik olarak

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

(örneğin header.xml için) Eğer 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(...)) 

denedim senin için başlığa eklemek , cazibe gibi çalıştı. Umarım yardımcı olur!

+0

java.lang.RuntimeException: Etkinlik başlatılamıyor ComponentInfo {com.event/com.event.EventHome}: java.lang.NullPointerException –

+0

NullPointerException öğesinin nerede olduğunu belirlemek için tam bir stacktrace'e ihtiyacınız var. – Lamorak

+0

Başlıkta gerçekten RelativeLayout ile başlaması gerekiyor. Bir LinearLayout ile başlamak çökmesini sağlar. – strangeluck

1

Bulduğum sorun ve gider, gridview'ın satır verileriyle ilişkili olması durumunda, select deyiminin benim durumumda belirli bir alana sahip olması gerektiğini değerlendirmekteyim. Notu değerlendiriyorum ve bu not benim select deyimimde yer almıyor ve gridview geri dönüyordu tek satır, ne olursa olsun, seçtiğim nottaki Notu dahil ettiğimde sorun düzeltildi.

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