在gridview中显示特定的textview



在我的自定义适配器中,我将其设置为生成字符串数组,并在gridview上显示它们,并在每行上使用特定样式的textview。我已经设置了一个布局与3 textviews,我将需要。我试着按照一些教程和例子去做,但还是没能弄明白。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="1dp">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/score_num_tile"
        android:id="@+id/tileWhite"
        android:background="#ffffffff"
        android:padding="2dp"
        style="@style/base_style_right.text_size"/>
    <TextView
        style="@style/base_style_right.text_size"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/score_num_tile"
        android:id="@+id/tileBlue"
        android:background="#ff3143bb"
        android:padding="2dp" />
    <TextView
        style="@style/base_style_right.text_size"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/score_num_tile"
        android:id="@+id/tileRed"
        android:background="#ffca4130"
        android:padding="2dp" />
    <TextView
        style="@style/base_style_right.text_size"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/score_num_tile"
        android:id="@+id/tileYellow"
        android:background="#ffaca93c"
        android:padding="2dp" />
</LinearLayout>

适配器

package counter.prebuild;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
 * Created by CoreyNW on 7/9/2014.
 */
public class ScoreAdapter extends BaseAdapter {
    private Context scoreContext;
    private String[] tileArray;
    int _count;
    private int m;
    public ScoreAdapter (Context c, int i, boolean parMode) {
        scoreContext = c;
        _count = i;
        setTileValues(i, parMode);
        Log.i("Running", "Is Running");
    }
    @Override
    public int getCount() {
        return 0;
    }
    @Override
    public Object getItem(int i) {
        return tileArray[i];
    }
    @Override
    public long getItemId(int i) {
        return 0;
    }
    @Override
    public View getView(int pos, View convView, ViewGroup viewGroup) {
        LayoutInflater inflater = (LayoutInflater) scoreContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View gridView;
        Log.i("getView", "Is Running");
        if (convView == null) {
            gridView = new View(scoreContext);
            gridView = inflater.inflate(R.layout.score_tile, null);
            TextView whiteTile = (TextView) gridView.findViewById(R.id.tileWhite);
            TextView blueTile = (TextView) gridView.findViewById(R.id.tileBlue);
            TextView redTile = (TextView) gridView.findViewById(R.id.tileRed);
            TextView yellowTile = (TextView) gridView.findViewById(R.id.tileYellow);

            TextView current;
            int kfc = 0;
            int counter = 1;
            for (int i = 0; i < m; i++) {
                switch (i) {
                    case 1:
                        current = blueTile;
                        break;
                    case 2:
                        current = redTile;
                        break;
                    case 3:
                        current = yellowTile;
                        break;
                    default:
                        current = whiteTile;
                        break;
                }
                for (int k = kfc; k < _count * counter; k++) {
                    current.setText(tileArray[pos]);
                }
                kfc += 18;
                counter++;
            }
            Log.i("gridViewUsed", "Used If");
        } else {
            gridView = convView;
            Log.i("gridViewDebug", "Used Else");
        }
        // FIXME : NOT DISPLAYING ANYTHING
        return gridView;
    }
    private void setTileValues(int holeCount, boolean mode) {
        m = mode ? 4 : 9;
        tileArray = new String[m * holeCount];
        for (int i = 0; i <holeCount; i++) {
            tileArray[i] = String.valueOf(i++);
        }
        for (int i = 18; i <holeCount*2; i++) {
            tileArray[i] = "4";
        }
        for (int i = 36; i <holeCount*3; i++) {
            tileArray[i] = "4";
        }
        for (int i = 54; i <holeCount*4; i++) {
            tileArray[i] = "6";
        }
        // TODO : SETUP TEXT VIEWS IF !MODE
    }
}

不引用多个textview,只是使用一组颜色来改变背景颜色似乎工作。

public class ScoreAdapter extends BaseAdapter {
    private Context scoreContext;
    private String[] tileArray;
    private int[] tileColors;
    private int dims;
    public ScoreAdapter (Context c, int holes, int dimens) {
        scoreContext = c;
        dims = dimens;
        setTileValues(holes);
        setTileColors(holes);
    }
    @Override
    public int getCount() {
        return tileArray.length;
    }
    @Override
    public Object getItem(int i) {
        return tileArray[i];
    }
    @Override
    public long getItemId(int i) {
        return 0;
    }
    @Override
    public View getView(int pos, View convView, ViewGroup viewGroup) {
        LayoutInflater infated = (LayoutInflater) scoreContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View gridView;
        if (convView == null) {
            gridView = infated.inflate(R.layout.score_tile, null);
            TextView textView = (TextView) gridView.findViewById(R.id.scoreTile);
            textView.setWidth(dims);
            textView.setHeight(dims);
            textView.setText(tileArray[pos]);
            textView.setBackgroundColor(tileColors[pos]);
            // TODO : POPULATE GRID VIEW HORIZONTALLY FIRST
        } else {
            gridView = convView;
        }
        return gridView;
    }
    private void setTileValues(int holeCount) {
        int counter = 0;
        tileArray = new String[9 * holeCount];
        int hole = 1;
        for (int i = 0; i <holeCount; i++) {
            tileArray[i] = String.valueOf(hole);
            counter++;
            hole++;
        }
        for (int i = counter; i <holeCount*2; i++) {
            tileArray[i] = "4";
            counter++;
        }
        for (int i = counter; i <holeCount*3; i++) {
            tileArray[i] = "4";
            counter++;
        }
        for (int i = counter; i <holeCount*4; i++) {
            tileArray[i] = "6";
            counter++;
        }
        for (int i = counter; i < holeCount*9; i++) {
            tileArray[i] = "S";
            counter++;
        }
    }
    private void setTileColors(int holeCount) {
        tileColors = new int[9 * holeCount];
        int white = Color.parseColor("#ffededed");
        int blue = Color.parseColor("#ff3143bb");
        int red = Color.parseColor("#ffca4130");
        int yellow = Color.parseColor("#ffaca93c");
        int counter = 0;
        for (int i = 0; i < holeCount; i++) {
            tileColors[i] = white;
            counter++;
        }
        for (int i = counter; i < holeCount*2; i++) {
            tileColors[i] = blue;
            counter++;
        }
        for (int i = counter; i < holeCount*3; i++) {
            tileColors[i] = red;
            counter++;
        }
        for (int i = counter; i < holeCount*4; i++) {
            tileColors[i] = yellow;
            counter++;
        }
        for (int i = counter; i < holeCount*9; i++) {
            tileColors[i] = white;
            counter++;
        }
    }
    public String getValue (int pos) {
        return tileArray[pos];
    }
}

最新更新