我正在做一个应用程序在gridview中使用textview显示A到Z的值,之后当我点击字母表时我会去其他活动...我做了,我的问题是如果我点击“A”字母然后我必须去Activity2然后我回到以前的活动(GridViewAcvtivity)意味着现在我只需要显示红色的“A”字母文本,保留默认颜色的所有字母,然后再次点击“B”字母,然后我将去其他一些Activity2然后我回到GridviewActivity意味着时间只有“A和B”字母应该是“红色”仍然是字母表所有默认颜色..我尝试使用下面的代码,一旦我点击一个字母表,他们是转到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"};
}
答案 0 :(得分: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()
和getItemId()
方法的实现,否则您可能会遇到奇怪的行为。
@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.
}