0

我在这里做一个应用程序正在使用gridview中的textview显示A到Z值,之后当我点击字母表时我会去其他一些活动......直到我做到了,我的问题是如果我点击“A”字母表那么我必须去 Activity2 然后我回到以前的活动(GridViewAcvtivity)意味着现在我只需要以红色显示“A”字母文本,保持所有字母为默认颜色,然后我再次点击“B”字母,然后我会去一些其他 Activity2 然后我回到 GridviewActivity 意味着时间只有“A 和 B”字母应该是“红色”保持字母所有默认颜色..我尝试使用下面的代码,一旦我点击 A 字母,他们将去 Activity2 ,然后我回到 GridviewActivity 那个时候所有的字母都变成了红色,但我只想显示一个红色的字母,任何人都给我一些想法。

public class GridViewActivity extends Activity {

GridView gridView;

static final String[] MOBILE_OS = new String[] { "A", "B",
        "C", "D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    gridView = (GridView) findViewById(R.id.gridView1);

    gridView.setAdapter(new ImageAdapter(this, MOBILE_OS));

    gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
        Toast.makeText(
                    getApplicationContext(),""+position, Toast.LENGTH_SHORT).show();
            GlobalClass.value1=1;
            Intent i=new Intent(GridViewActivity.this,Activity2.class);
            i.putExtra("k1", "position");
            startActivity(i);
            }
    });

}
public class ImageAdapter extends BaseAdapter {
    private Context context;
    private final String[] mobileValues;

    public ImageAdapter(Context context, String[] mobileValues) {
        this.context = context;
        this.mobileValues = mobileValues;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from mobile.xml
            gridView = inflater.inflate(R.layout.mobile, null);

            // set value into textview
            TextView textView = (TextView) gridView
                    .findViewById(R.id.grid_item_label);
            textView.setText(mobileValues[position]);

            if(GlobalClass.value1==1)
            {
                textView.setTextColor(Color.RED);
            }
            } else {
            gridView = (View) convertView;
        }

        return gridView;
    }

    @Override
    public int getCount() {
        return mobileValues.length;
    }

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

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

}


String mLabelsIds[]={"RED","YELLOW","black"};
      }
4

1 回答 1

2

setOnItemClickListener()开始活动之前更改当前单击的字母的颜色。当您返回时,文本应该已经预先着色。

像这样的东西:

gridView.setOnItemClickListener(new OnItemClickListener() {
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
         TextView currentLetter = (TextView) v.findViewById(R.id.grid_item_label);
         currentLetter.setTextColor(Color.GREEN);

         // start Activity2 here
     }
}

作为旁注,请提供getItem()andgetItemId()方法的实现,否则您可能会遇到奇怪的行为。

@Override
public Object getItem(int position) {
   return mobileValues[position]; //return the element from array at specified position
}

@Override
public long getItemId(int position) {
   return position; //usually the item id is its position.
}
于 2013-06-20T12:46:21.663 回答