Reputation:
I have got a listview with a custom adapter.
Following is my getView method
@Override
public View getView(int position, View convertView, ViewGroup parent) {
view = convertView;
if (convertView == null) {
//inflate the view for each row of listview
view = layoutInflater.inflate(R.layout.rating_item, null);
mViewHolder = new ViewHolder();
mViewHolder.mstatus = (TextView) view.findViewById(R.id.ratingStatus);
mViewHolder.mCheckBox = (CheckBox) view.findViewById(R.id.checkBox);
view.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) view.getTag();
}
mViewHolder.mstatus.setText("Status");
return view;
}
Then Im trying to update the texview mStatus
in my code as follows,
RatingAdapter ratingAdapter = new RatingAdapter(getActivity(), starOneList);
TextView status = (TextView) getViewByPosition(0, mListView).findViewById(R.id.ratingStatus);
status.setText("New Status");
mListView.setAdapter(ratingAdapter);
Toast.makeText(getActivity(), "Log" +
status.getText().toString(), Toast.LENGTH_SHORT).show();
ratingAdapter.notifyDataSetChanged();
The problem is the the texview is being set but in ui the change is not visible. I mean, the toast message that I get is "New Status" but in ui, the change is not being effected and the textview still displays the old text Status
. Why is that so?
Upvotes: 1
Views: 160
Reputation: 3319
Try this.
{
if (convertView == null) {
//inflate the view for each row of listview
view = layoutInflater.inflate(R.layout.rating_item, null);
starOneList= HERE_GET_YOUR_LIST_ITEM USING POSITION;
mViewHolder = new ViewHolder();
mViewHolder.mstatus = (TextView) view.findViewById(R.id.ratingStatus);
mViewHolder.mCheckBox = (CheckBox) view.findViewById(R.id.checkBox);
view.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) view.getTag();
}
mViewHolder.mstatus.setText(starOneList.status); // this is your value that change in activity and only need to notify adapter
return view;
}
Then
// if string array
starOneList[POSITION]="New Status";
//or if model class
starOneList.get(POSITION).setStatus("New Status")
RatingAdapter ratingAdapter = new RatingAdapter(getActivity(), starOneList);
mListView.setAdapter(ratingAdapter);
Toast.makeText(getActivity(), "Log" +status.getText().toString(), Toast.LENGTH_SHORT).show();
ratingAdapter.notifyDataSetChanged();
Upvotes: 0
Reputation: 1802
use to following code for refresh your adapter after update to adapter:
youradapter.notifyDataSetChanged();
Upvotes: 1
Reputation:
You have to define like this.
if (convertView == null) {
//inflate the view for each row of listview
view = layoutInflater.inflate(R.layout.rating_item, null);
mViewHolder = new ViewHolder();
mViewHolder.setTag(R.id.ratingStatus, mViewHolder.findViewById(R.id.ratingStatus));
mViewHolder.setTag(R.id.checkBox, mViewHolder.findViewById(R.id.checkBox));
view.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) view.getTag();
}
mViewHolder.mstatus.setText("Status");
return view;
}
Upvotes: 0
Reputation: 381
You have to pass the text which you want to update on textview each time. you are now setting textview each time statically i.e. mViewHolder.mstatus.setText("Status"); instead of this your code will be like this mViewHolder.mstatus.setText(newtext); where newtext is some variable which hold new string value
Upvotes: 2
Reputation: 567
set the tag in the textview in getview method ,and when you are calling in fragment then get the tag with yourTextview.getTag(),then you set the text to it
Upvotes: 0