Reputation: 121
am doing one application here am displaying A to Z values using textview in gridview,after that when i click alphabet am going to some other activity..up to that i did,my problem is if i click "A" alphabet then i have to go Activity2 then i am coming back to previous activity(GridViewAcvtivity) means now i need to show only "A" alphabet text in red color,remain all alphabets in default color,then again i click "B" alphabet,then i will go some other Activity2 then i am coming back to GridviewActivity means that time only "A and B" alphabets should be in "red color" remain alphabets all default color..i tried using below code,once i click A alphabet,them am going to Activity2,then am coming back to GridviewActivity that time all alphabets chanaging to red color,but i want show only A alphabet in red color,any one give me some idea.
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"};
}
Upvotes: 0
Views: 5058
Reputation: 16043
In setOnItemClickListener()
before starting the activity change the color of the currently clicked letter. When you'll return, the text should be pre-colored already.
Something like this:
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
}
}
As a side note, provide implementations for the getItem()
and getItemId()
methods, otherwise you may get strange behaviours.
@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.
}
Upvotes: 2