GridView上的空白屏幕

时间:2015-06-22 09:10:26

标签: android arrays gridview

我正在尝试使用字符串数组创建GridView。这些是XML,MainActivity和Adapter,但我得到的是一个空白屏幕。我正在更改背景,但结果是一样的,但点击屏幕上的某个点会向我显示吐司,说明该号码已被选中。有什么想法吗?

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 个答案:

答案 0 :(得分:1)

我执行了你的代码。 只需添加

numberView.setTextColor(Color.BLACK);

它会起作用! :)