I have a ListView that is correctly populated with an array on create, but when I scroll, the data changes and is all out of order. I don't know wether the problem is in my CustomListViewAdapter or in my DetailsPage. Here's the For Loop that I am using to generate the textViews:
if (currentObject.setTime != null && currentObject.setName != null) {
String[] temporaryNames = currentObject.setName;
int totalNames = (currentObject.setTime.length / currentObject.setName.length);
for (int i = 1; i < totalNames; i++) {
currentObject.setName = ArrayUtils.addAll(currentObject.setName,temporaryNames);
}
}
listView.setAdapter(new BusRouteCustomListViewAdapter(this, currentObject.setName, currentObject.setTime));
and here is the code for my customListViewAdapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View v = convertView;
if (v == null)
if (locations[position] != null && times[position] != null) {
v = inflater.inflate(R.layout.busdetailrow, null);
holder.tv1 = (TextView) v.findViewById(R.id.text);
holder.tv2 = (TextView) v.findViewById(R.id.text2);
holder.tv1.setText(locations[position]);
holder.tv2.setText(times[position]);
} else {
v = inflater.inflate(R.layout.null_row, null);
}
return v;
}
You replace the code with these lines of code
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder;
View v = convertView;
if (v == null)
{
holder = new Holder();
v = inflater.inflate(R.layout.busdetailrow, null);
holder.tv1 = (TextView) v.findViewById(R.id.text);
holder.tv2 = (TextView) v.findViewById(R.id.text2);
v.setTag(holder);
} else {
holder = (Holder) v.getTag();
}
holder.tv1.setText(locations[position]);
holder.tv2.setText(times[position]);
return v;
}
Use your getView() like below.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
if (convertView == null){
if (locations[position] != null && times[position] != null) {
convertView = inflater.inflate(R.layout.busdetailrow, null);
holder.tv1 = (TextView) v.findViewById(R.id.text);
holder.tv2 = (TextView) v.findViewById(R.id.text2);
holder.tv1.setText(locations[position]);
holder.tv2.setText(times[position]);
} else {
}
convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}
return convertView;
}
You didnt implement design holder probably. make following changes to your getView then it will work fine
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
if (convertView == null){
if (locations[position] != null && times[position] != null) {
convertView = inflater.inflate(R.layout.busdetailrow, null);
holder.tv1 = (TextView) v.findViewById(R.id.text);
holder.tv2 = (TextView) v.findViewById(R.id.text2);
holder.tv1.setText(locations[position]);
holder.tv2.setText(times[position]);
}
else
{
}
convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}
return convertView;
}
Related
I trying make a listview with much data but when i build, item data on listview multiple repeating.
This is myJava CustomAdapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.Name = (TextView) convertView.findViewById(R.id.Recipe_Name);
holder.Image_Block = (ImageView) convertView.findViewById(R.id.Recipe_Image);
holder.Text_Recipe = (TextView) convertView.findViewById(R.id.Recipe_Text);
holder.Text_Rarity = (TextView) convertView.findViewById(R.id.Recipe_Rarity);
RowItem row_pos = rowItems.get(position);
holder.Image_Block.setImageResource(row_pos.getImage_Block());
holder.Name.setText(row_pos.getName());
holder.Text_Recipe.setText(row_pos.getText_Recipe());
holder.Text_Rarity.setText(row_pos.getText_Rarity());
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
Anyone can fix ? Thanks
Your data isn't repeating--when you're recycling an old view, you're not initializing it to new values. Move the code that sets values (not the code that makes connections) outside of the if-else statement.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.Name = (TextView) convertView.findViewById(R.id.Recipe_Name);
holder.Image_Block = (ImageView) convertView.findViewById(R.id.Recipe_Image);
holder.Text_Recipe = (TextView) convertView.findViewById(R.id.Recipe_Text);
holder.Text_Rarity = (TextView) convertView.findViewById(R.id.Recipe_Rarity);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
RowItem row_pos = rowItems.get(position);
holder.Image_Block.setImageResource(row_pos.getImage_Block());
holder.Name.setText(row_pos.getName());
holder.Text_Recipe.setText(row_pos.getText_Recipe());
holder.Text_Rarity.setText(row_pos.getText_Rarity());
return convertView;
}
I have a listview and custom adapter....when I run program it will give me error in getview.How can I resolve that error.
code:-
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = View.inflate(m_Context, R.layout.deallisting_card_view, null);
viewHolder.m_Header = (TextView) convertView.findViewById(R.id.headingText);
viewHolder.m_Subheader = (TextView) convertView.findViewById(R.id.subHeaderText);
viewHolder.m_DummyText = (TextView) convertView.findViewById(R.id.subHeadingText);
viewHolder.m_logoImage = (ImageView) convertView.findViewById(R.id.appImage);
viewHolder.m_getBtn = (Button) convertView.findViewById(R.id.getDealBtn);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
}
in covertview = View.inflate(m_Context...); in this line I am getting error
this should be done like this.
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.deallisting_card_view, null);
viewHolder.m_Header = (TextView) convertView.findViewById(R.id.headingText);
viewHolder.m_Subheader = (TextView) convertView.findViewById(R.id.subHeaderText);
viewHolder.m_DummyText = (TextView) convertView.findViewById(R.id.subHeadingText);
viewHolder.m_logoImage = (ImageView) convertView.findViewById(R.id.appImage);
viewHolder.m_getBtn = (Button) convertView.findViewById(R.id.getDealBtn);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder= new ViewHolder();
vi=convertView;
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
vi= mInflater.inflate( R.layout.deallisting_card_view, null);
viewHolder.m_Header = (TextView) convertView.findViewById(R.id.headingText);
viewHolder.m_Subheader = (TextView) convertView.findViewById(R.id.subHeaderText);
viewHolder.m_DummyText = (TextView) convertView.findViewById(R.id.subHeadingText);
viewHolder.m_logoImage = (ImageView) convertView.findViewById(R.id.appImage);
viewHolder.m_getBtn = (Button) convertView.findViewById(R.id.getDealBtn);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
}
Try above code, hope it will work :)GlbMP
I am working on social application and it's about to complete but I got stuck on one issue that is image flickering. When there is around 9 to 10 images on screen and if I scroll the page then the image flicker take place.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.view_explore_icon, null);
holder = new ViewHolder();
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.back = (RelativeLayout) convertView.findViewById(R.id.back_layout);
holder.img = (ImageView) convertView.findViewById(R.id.img_grid_album);
convertView.setTag(holder);
ImageLoader.getInstance().displayImage(
Static_Urls.explore_logo_pic + categoryList.get(position).cat_logo,
holder.img);
if (pos == position) {
holder.back.setBackgroundResource(R.drawable.explore_selected_image);
} else {
holder.back.setBackgroundResource(R.drawable.explore_blank_image);
}
holder.img.setTag(position);
holder.img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pos = (int) view.getTag();
txt_cat_name.setText(categoryList.get(position).category);
//notifyDataSetChanged();
new GetAllExplorePic().execute(categoryList.get(position).id);
}
});
return convertView;
}
displace the below lines of code inside of if(convertview == null)
holder.back = (RelativeLayout) convertView.findViewById(R.id.back_layout);
holder.img = (ImageView) convertView.findViewById(R.id.img_grid_album);
convertView.setTag(holder);
your final code
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.view_explore_icon, null);
holder = new ViewHolder();
holder.back = (RelativeLayout) convertView.findViewById(R.id.back_layout);
holder.img = (ImageView) convertView.findViewById(R.id.img_grid_album);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// holder.back = (RelativeLayout) convertView.findViewById(R.id.back_layout);
// holder.img = (ImageView) convertView.findViewById(R.id.img_grid_album);
// convertView.setTag(holder);
ImageLoader.getInstance().displayImage(
Static_Urls.explore_logo_pic + categoryList.get(position).cat_logo,
holder.img);
if (pos == position) {
holder.back.setBackgroundResource(R.drawable.explore_selected_image);
} else {
holder.back.setBackgroundResource(R.drawable.explore_blank_image);
}
holder.img.setTag(position);
holder.img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pos = (int) view.getTag();
txt_cat_name.setText(categoryList.get(position).category);
//notifyDataSetChanged();
new GetAllExplorePic().execute(categoryList.get(position).id);
}
});
return convertView;
}
I am getting a ClassCastException when using two different ViewHolders
for my Groups an in ExpandableListView.
I have a separate ViewHolder for Group in position 0. The rest of the Groups use a different one.
This error happens after I already create the views by scrolling down and scroll back up to the Group in position 0.
I know my issue is in this line:
vhPropertyInfo = (ViewHolderPropertyInfo) convertView.getTag();
When I scroll back to Group position 0, it tries to cast (ViewHolderPropertyInfo) to the convertView that is not null, but it is actually (ViewHolderGroup) type. How do I resolve this?
#Override
public View getGroupView(int groupPosition, boolean isExpandable, View convertView, ViewGroup parent) {
if (groupPosition == 0) {
ViewHolderPropertyInfo vhPropertyInfo;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_property_info, null);
vhPropertyInfo = new ViewHolderPropertyInfo();
vhPropertyInfo.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
vhPropertyInfo.mTvCountry = (TextView) convertView.findViewById(R.id.tv_country);
vhPropertyInfo.mTvCity = (TextView) convertView.findViewById(R.id.tv_city);
vhPropertyInfo.mTvDistrict = (TextView) convertView.findViewById(R.id.tv_district);
vhPropertyInfo.mTvPostalCode = (TextView) convertView.findViewById(R.id.tv_postal_code);
vhPropertyInfo.mTvStreet = (TextView) convertView.findViewById(R.id.tv_street);
vhPropertyInfo.mTvSubStreet = (TextView) convertView.findViewById(R.id.tv_sub_street);
convertView.setTag(vhPropertyInfo);
}
else {
vhPropertyInfo = (ViewHolderPropertyInfo) convertView.getTag();
}
vhPropertyInfo.mTvName.setText("House Lannister");
vhPropertyInfo.mTvCountry.setText("Westeros");
vhPropertyInfo.mTvCity.setText("Kings Landing");
vhPropertyInfo.mTvDistrict.setText("West Side");
vhPropertyInfo.mTvPostalCode.setText("12345");
vhPropertyInfo.mTvStreet.setText("123 Kings Lane");
vhPropertyInfo.mTvSubStreet.setText("Hut 23");
return convertView;
}
else {
ViewHolderGroup vhGroup;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_property_group, null);
vhGroup = new ViewHolderGroup();
vhGroup.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
convertView.setTag(vhGroup);
}
else {
vhGroup = (ViewHolderGroup) convertView.getTag();
}
// Group 0 is property info so have to subtract one position
vhGroup.mTvName.setText(getGroup(groupPosition));
return convertView;
}
}
public final class ViewHolderGroup {
public TextView mTvName;
}
public final class ViewHolderPropertyInfo {
public TextView mTvName;
public TextView mTvCountry;
public TextView mTvCity;
public TextView mTvDistrict;
public TextView mTvPostalCode;
public TextView mTvStreet;
public TextView mTvSubStreet;
}
EDIT 1: So I fixed the problem, but checking the instance of convertView.getTag() if it was the same type of ViewHolder type, but my solution doesn't seem very elegant. Anyone know a way to do this cleaner?
#Override
public View getGroupView(int groupPosition, boolean isExpandable, View convertView, ViewGroup parent) {
if (groupPosition == 0) {
ViewHolderPropertyInfo vhPropertyInfo;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_property_info, null);
vhPropertyInfo = new ViewHolderPropertyInfo();
vhPropertyInfo.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
vhPropertyInfo.mTvCountry = (TextView) convertView.findViewById(R.id.tv_country);
vhPropertyInfo.mTvCity = (TextView) convertView.findViewById(R.id.tv_city);
vhPropertyInfo.mTvDistrict = (TextView) convertView.findViewById(R.id.tv_district);
vhPropertyInfo.mTvPostalCode = (TextView) convertView.findViewById(R.id.tv_postal_code);
vhPropertyInfo.mTvStreet = (TextView) convertView.findViewById(R.id.tv_street);
vhPropertyInfo.mTvSubStreet = (TextView) convertView.findViewById(R.id.tv_sub_street);
convertView.setTag(vhPropertyInfo);
}
else {
if (convertView.getTag() instanceof ViewHolderPropertyInfo) {
vhPropertyInfo = (ViewHolderPropertyInfo) convertView.getTag();
}
else {
convertView = mInflater.inflate(R.layout.row_property_info, null);
vhPropertyInfo = new ViewHolderPropertyInfo();
vhPropertyInfo.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
vhPropertyInfo.mTvCountry = (TextView) convertView.findViewById(R.id.tv_country);
vhPropertyInfo.mTvCity = (TextView) convertView.findViewById(R.id.tv_city);
vhPropertyInfo.mTvDistrict = (TextView) convertView.findViewById(R.id.tv_district);
vhPropertyInfo.mTvPostalCode = (TextView) convertView.findViewById(R.id.tv_postal_code);
vhPropertyInfo.mTvStreet = (TextView) convertView.findViewById(R.id.tv_street);
vhPropertyInfo.mTvSubStreet = (TextView) convertView.findViewById(R.id.tv_sub_street);
convertView.setTag(vhPropertyInfo);
}
}
vhPropertyInfo.mTvName.setText("House Lannister");
vhPropertyInfo.mTvCountry.setText("Westeros");
vhPropertyInfo.mTvCity.setText("Kings Landing");
vhPropertyInfo.mTvDistrict.setText("West Side");
vhPropertyInfo.mTvPostalCode.setText("12345");
vhPropertyInfo.mTvStreet.setText("123 Kings Lane");
vhPropertyInfo.mTvSubStreet.setText("Hut 23");
return convertView;
}
else {
ViewHolderGroup vhGroup;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_property_group, null);
vhGroup = new ViewHolderGroup();
vhGroup.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
convertView.setTag(vhGroup);
}
else {
if (convertView.getTag() instanceof ViewHolderGroup) {
vhGroup = (ViewHolderGroup) convertView.getTag();
}
else {
convertView = mInflater.inflate(R.layout.row_property_group, null);
vhGroup = new ViewHolderGroup();
vhGroup.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
convertView.setTag(vhGroup);
}
}
// Group 0 is property info so have to subtract one position
vhGroup.mTvName.setText(getGroup(groupPosition));
return convertView;
}
}
I found a more elegant way to do my solution. It is to do the convertView.getTag() checking the instanceof the ViewHolder before the check if convertView == null.
#Override
public View getGroupView(int groupPosition, boolean isExpandable, View convertView, ViewGroup parent) {
if (groupPosition == 0) {
ViewHolderPropertyInfo vhPropertyInfo;
if (convertView != null && !(convertView.getTag() instanceof ViewHolderPropertyInfo)) {
convertView = null;
}
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_property_info, null);
vhPropertyInfo = new ViewHolderPropertyInfo();
vhPropertyInfo.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
vhPropertyInfo.mTvCountry = (TextView) convertView.findViewById(R.id.tv_country);
vhPropertyInfo.mTvCity = (TextView) convertView.findViewById(R.id.tv_city);
vhPropertyInfo.mTvDistrict = (TextView) convertView.findViewById(R.id.tv_district);
vhPropertyInfo.mTvPostalCode = (TextView) convertView.findViewById(R.id.tv_postal_code);
vhPropertyInfo.mTvStreet = (TextView) convertView.findViewById(R.id.tv_street);
vhPropertyInfo.mTvSubStreet = (TextView) convertView.findViewById(R.id.tv_sub_street);
convertView.setTag(vhPropertyInfo);
}
else {
vhPropertyInfo = (ViewHolderPropertyInfo) convertView.getTag();
}
vhPropertyInfo.mTvName.setText("House Lannister");
vhPropertyInfo.mTvCountry.setText("Westeros");
vhPropertyInfo.mTvCity.setText("Kings Landing");
vhPropertyInfo.mTvDistrict.setText("West Side");
vhPropertyInfo.mTvPostalCode.setText("12345");
vhPropertyInfo.mTvStreet.setText("123 Kings Lane");
vhPropertyInfo.mTvSubStreet.setText("Hut 23");
return convertView;
}
else {
ViewHolderGroup vhGroup;
if (convertView != null && !(convertView.getTag() instanceof ViewHolderGroup)) {
convertView = null;
}
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_property_group, null);
vhGroup = new ViewHolderGroup();
vhGroup.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
convertView.setTag(vhGroup);
}
else {
vhGroup = (ViewHolderGroup) convertView.getTag();
}
// Group 0 is property info so have to subtract one position
vhGroup.mTvName.setText(getGroup(groupPosition));
return convertView;
}
}
I have Listview with multiple views.Tried to set the selection on second item using following code.But the view is always null.Please help me.Due to character limit I can't attach the layouts used for the listview
View view = ListView.getChildAt(0);
int top = (view == null) ? 0 : view.getTop();
// restore list
ListView.setSelectionFromTop(ListView.getFirstVisiblePosition() + 1, top);
Adapter
#Override
public int getItemViewType(int position) {
if (Details.get(position).getView_type() == 1)
return ITEM_TYPE_ONE;
else if (Details.get(position).getView_type() == 0)
return ITEM_TYPE_THREE;
else
return ITEM_TYPE_TWO;
}
#Override
public int getViewTypeCount() {
return 3;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
vi = convertView;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case ITEM_TYPE_ONE:
vi = inflater.inflate(R.layout.feed_detail_list_item_selected,
null);
holder.name = (TextView) vi
.findViewById(R.id.list_item_name_txt);
holder.profilepic = (ImageView) vi
.findViewById(R.id.list_item_profile_img);
break;
case ITEM_TYPE_TWO:
vi = inflater.inflate(R.layout.listlastrow, null);
holder.name = (TextView) vi.findViewById(R.id.msg);
break;
case ITEM_TYPE_THREE:
vi = inflater
.inflate(R.layout.feed_details_parent_layout, null);
holder.name = (TextView) vi.findViewById(R.id.ownerName);
holder.profilepic = (ImageView) vi
.findViewById(R.id.thumb_image);
holder.VoiceBar = (SeekBar) vi
.findViewById(R.id.message_seekbar);
holder.voice_bar_layout = (LinearLayout) vi
.findViewById(R.id.seekbar_layout);
break;
}
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if (type == ITEM_TYPE_TWO) {
holder.name.setTypeface(tfNormal);
((LinearLayout) vi.findViewById(R.id.ll))
.setBackgroundColor(mContext.getResources().getColor(
R.color.white));
if (Details.size() == 2) {
holder.name.setVisibility(View.VISIBLE);
holder.name.setText(mContext.getResources().getString(
R.string.no_replies));
} else {
holder.name.setVisibility(View.GONE);
}
}
}
return vi;
}