2015-06-22 202 views
-1

我試圖用一串字符串創建一個GridView。這些是XML,MainActivity和Adapter,但是我得到的是一個空白屏幕。我改變了背景,但結果是一樣的,但點擊屏幕上的一個點出現在我面前,說明已經選擇了數字。有什麼想法嗎 ?GridView上的空白屏幕

XML:

GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" 
    android:columnWidth="90dp" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
    android:id="@+id/gridview"> 
</GridView> 

MainActivity:

package kangel.customtest; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.GridView; 
import android.widget.Toast; 


public class MainActivity extends ActionBarActivity { 
    GridView gridView; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     gridView = (GridView)findViewById(R.id.gridview); 
     gridView.setAdapter(new NumberAdapter(this)); 
     gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Toast.makeText(getBaseContext(),"Hai selezionato il numero "+position, Toast.LENGTH_LONG).show(); 
      } 
     }); 
    } 
} 

適配器:

package kangel.customtest; 

import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AbsListView; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.TextView; 

/** 
* Created by Andrea on 22/06/2015. 
*/ 
public class NumberAdapter extends BaseAdapter { 

    private Context context; 
    private String numeriGiocabili[]= 
      {"1","2","3","4","5","6","7","8","9","10", 
      "11","12","13","14","15","16","17","18","19","20", 
      "21","22","23","24","25","26","27","28","29","30", 
      "31","32","33","34","35","36","37","38","39","40", 
      "41","42","43","44","45","46","47","48","49","50", 
      "51","52","53","54","55","56","57","58","59","60"}; 

    public NumberAdapter(Context context){ 
     this.context = context; 
    } 
    @Override 
    public int getCount() { 
     return numeriGiocabili.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     TextView numberView; 

     if (convertView == null){ 
      numberView = new TextView(context); 
      numberView.setLayoutParams(new GridView.LayoutParams(80, 80)); 
      numberView.setPadding(8, 8, 8, 8); 
      numberView.setTextColor(context.getResources().getColor(R.color.abc_background_cache_hint_selector_material_light)); 
     } 
     else { 
      numberView = (TextView) convertView; 
     } 

     numberView.setText(numeriGiocabili[position]); 
     return numberView; 

    } 
} 

回答

1

我執行我們的代碼。 只需添加

numberView.setTextColor(Color.BLACK);

它會工作! :)

+0

爲什麼當我點擊數字1告訴我,我點擊了數字0? – Kangel

+0

是的,當我們將適配器設置爲gridview時,它將從0開始設置位置。使用position-1哪裏有你想要顯示的相應位置 – user765

+0

我必須在哪裏更改? – Kangel