Change listviews field property from main activity
Question:
I am working on an Android application.In my main activity i
have to implement a list.The following is the sample shape of my page
step 1)
I craete variable in my activity
|----------------------| \
| |Button| | \
|----------------------| \
|listview row1 | \ \
|listview row1 | \ \---------Screen
|listview row1 | / --/----- ListView
| |/ /
| | /
| | /
|______________________|/
The button is in my activity page and listview rows are creating in
baseadapter.Listview is containing a textview.Now I have to change the
Textviews background color when I click the button from activity and
next time I click the button the textviews color will retain the old
color.How can I do it friends?. I declared textview in getview() method.Solution 1:
There are probably other methods, but I'd cycle through the list rows in the OnClick method for the button. Something like:
In your activity field definitions:
In your activity field definitions:
static final int colourA=Color.argb(255,255,0,0);
static final int colourB=Color.argb(255,0,255,0);
int currentColour=colourA;
In your activity OnCreate: Button myButton = (Button) findViewById(R.id.myButton);
final ListView myListView = (ListView) findViewByID(R.id.myListView);
//change myButton to your button id, and myListView to your ListView id
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//This is the code to toggle the colours, you can do pretty much whatever you want here though
if (currentColour==colourA){
currentColour=colourB;
} else {
currentColour=colourA;
}
//This cycles through all the root views in the ListView. If you want to change the
//colour of only one view in the row layout, in the for loop use
//rowView.findViewById(R.id.myViewInRow).setBackgroundColor(currentColour);
//instead, to get the relevant view in the row
View rowView;
for (int i=0;i<myListView.getChildCount();i++){
rowView=myListView.getChildAt(i);
rowView.setBackgroundColor(currentColour);
}
}
});
Solution 2:
At last I got the solution.It may helpful to others.But I am not about the quality of my code.
step 1)
I craete variable in my activity
static int hidestate=0;
And in the hide buttons on click method I write thishide_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (hidestate==0) {
hidestate=1;
sMSConversationAdapter.notifyDataSetChanged();
hide_btn.setText("Show All");
}else {
hidestate=0;
sMSConversationAdapter.notifyDataSetChanged();
hide_btn.setText("Hide All");
}
}
});
Step 2)
The following is my BaseAdapter class getView()public View getView(final int position, View convertView, ViewGroup parent) {
View vi=convertView;
final TextView message;
if(convertView==null)
vi = inflater.inflate(R.layout.smsconversation_row, null);
RelativeLayout nws=(RelativeLayout)vi.findViewById(R.id.all);
message=(TextView)vi.findViewById(R.id.snt_txt);
if (SMSConversationHome.hidestate==1) {
message.setVisibility(View.INVISIBLE);
}
else{
message.setVisibility(View.VISIBLE);
}
}
Comments
Post a Comment