Listview Repeating Items - android

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;
}

Related

in listview android items repeating

public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService("layout_inflater");
if (convertView != null) {
return convertView;
}
list = new View(this.mContext);
list = inflater.inflate(R.layout.rowsset, null);
imageView = (ImageView) list.findViewById(R.id.pic1);
textView1 = (TextView) list.findViewById(R.id.det2);
textView2 = (TextView) list.findViewById(R.id.kite1);
textView1.setText(this.web[position]);
textView2.setText(this.web1[position]);
imageView.setImageResource(this.Imageid[position]);
return list;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.rowsset, parent, false);
imageView = (ImageView) convertView.findViewById(R.id.pic1);
textView1 = (TextView) convertView.findViewById(R.id.det2);
textView2 = (TextView) convertView.findViewById(R.id.kite1);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
viewHolder.textView1.setText(this.web[position]);
viewHolder.textView2.setText(this.web1[position]);
viewHolder.imageView.setImageResource(this.Imageid[position]);
// Return the completed view to render on screen
return convertView;
}
// Please this line mention in your adapter's constructor
inflater = LayoutInflater.from(context);
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.rowsset, null);
imageView = (ImageView) list.findViewById(R.id.pic1);
textView1 = (TextView) list.findViewById(R.id.det2);
textView2 = (TextView) list.findViewById(R.id.kite1);
textView1.setText(web[position]);
textView2.setText(web1[position]);
imageView.setImageResource(Imageid[position]);
return convertView;
}
Hope this will work...

Custom listview throwing a null pointer exception

I am creating a custom ListView in android. The problem am facing is in my adapter code. It throws a NullPointerException even though everything else seems in order. Sample code below:
public class CustomAdapter extends BaseAdapter {
private Context ctx;
private List<LearnLangs> langsList;
private LayoutInflater layoutInflater;
public CustomAdapter(Context ctx, List<LearnLangs> langsList) {
this.ctx = ctx;
this.langsList = langsList;
}
#Override
public int getCount() {
return langsList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public ImageView img;
public TextView title;
public TextView sub;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LearnLangs langs = langsList.get(position);
if (convertView == null) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.custom_list, parent, false);
holder = new ViewHolder();
//error occurs on this line below
holder.img = (ImageView) convertView.findViewById(R.id.list_img);
holder.title = (TextView) convertView.findViewById(R.id.list_title);
holder.sub = (TextView) convertView.findViewById(R.id.list_subT);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
holder.img.setImageResource(langs.getImgId());
holder.title.setText(langs.getNameLang());
holder.sub.setText(langs.getnTuts());
}
return convertView;
}
}
Exception thrown:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.example.joey.sunshine.CustomAdapter.getView(CustomAdapter.java:62)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1673)
at android.widget.AbsListView.onLayout(AbsListView.java:2151)
at android.view.View.layout(View.java:15761)
What seems to be the problem?
You are getting exception because you are not using view which you are inflating at all. Change your code of getview() method as per below :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View mView = convertView;
ViewHolder holder;
LearnLangs langs = langsList.get(position);
if (mView == null) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = layoutInflater.inflate(R.layout.custom_list, parent, false);
holder = new ViewHolder();
//error occurs on this line below
holder.img = (ImageView) mView.findViewById(R.id.list_img);
holder.title = (TextView) mView.findViewById(R.id.list_title);
holder.sub = (TextView) mView.findViewById(R.id.list_subT);
mView.setTag(holder);
}
else
{
holder = (ViewHolder) mView.getTag();
holder.img.setImageResource(langs.getImgId());
holder.title.setText(langs.getNameLang());
holder.sub.setText(langs.getnTuts());
}
return mView;
}
Just change this line :
layoutInflater.inflate(R.layout.custom_list, parent, false);
to this:
convertView = layoutInflater.inflate(R.layout.custom_list, parent, false);
EDIT
public AdapterConstructor() {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //move this line inside the constructor
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LearnLangs langs = langsList.get(position);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.custom_list, parent, false);
holder = new ViewHolder();
//error occurs on this line below
holder.img = (ImageView) convertView.findViewById(R.id.list_img);
holder.title = (TextView) convertView.findViewById(R.id.list_title);
holder.sub = (TextView) convertView.findViewById(R.id.list_subT);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.img.setImageResource(langs.getImgId());
holder.title.setText(langs.getNameLang());
holder.sub.setText(langs.getnTuts());
return convertView;
}
Just change your getView() method by below :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LearnLangs langs = langsList.get(position);
if (convertView == null) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_list, parent, false);
holder = new ViewHolder();
//error occurs on this line below
holder.img = (ImageView) convertView.findViewById(R.id.list_img);
holder.title = (TextView) convertView.findViewById(R.id.list_title);
holder.sub = (TextView) convertView.findViewById(R.id.list_subT);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.img.setImageResource(langs.getImgId());
holder.title.setText(langs.getNameLang());
holder.sub.setText(langs.getnTuts());
return convertView;
}

I m getting error in getView() in android?

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

Android listview ImageButton onclick error

I use an imagebutton in a listview and everything was working fine. But when i tried to change the background resource on the imagebutton on its click i got my app crashed. here is my code.
public View getView(final int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
// mCheckStates = new SparseBooleanArray(name1.size());
mInflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.textView1);
holder.tv1 = (TextView) convertView
.findViewById(R.id.textView2);
holder.b = (ImageButton) convertView
.findViewById(R.id.btn_invite);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(name1.get(position));
holder.tv1.setText(phno1.get(position));
holder.b.setTag(position);
holder.b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
RelativeLayout rl = (RelativeLayout)v.getParent();
holder.b = (ImageButton)rl.getChildAt(0);
holder.b.setBackgroundResource(R.drawable.ic_invited);
});
return convertView;
}
This following would be right flow-
public View getView(final int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
// mCheckStates = new SparseBooleanArray(name1.size());
mInflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.textView1);
holder.tv1 = (TextView) convertView
.findViewById(R.id.textView2);
holder.b = (ImageButton) convertView
.findViewById(R.id.btn_invite);
holder.tv.setText(name1.get(position));
holder.tv1.setText(phno1.get(position));
holder.b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
RelativeLayout rl = (RelativeLayout)v.getParent();
holder.b = (ImageButton)rl.getChildAt(0); // if your error is layout can't be casted to image Button, then in this line may be you are getting an unexpected layout view instead of a ImageButton
holder.b.setBackgroundResource(R.drawable.ic_invited);
});
holder.b.setTag(position);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}

View Holder And custom list view items getting shuffled while scrolling

I know there is a lot of similar questions out there but i cant find the mistake i am making,if anybody could help.
I am trying to display the values in an array list like a chat format but the array list items are getting misplaced on scrolling
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getActivity()).inflate(
R.layout.chat_row, parent, false);
holder = new ViewHolder();
holder.messageLeft = (TextView) convertView
.findViewById(R.id.leftBox);
holder.messageRight = (TextView) convertView
.findViewById(R.id.rightBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try { if (snd_id.get(position).equalsIgnoreCase(appPref.getData("Uid"))) {
holder.messageLeft.setText(snd_name.get(position)+":"+"\n"+chat.get(position));
holder.messageLeft.setVisibility(View.VISIBLE);
holder.messageRight.setVisibility(View.GONE);
} else {
holder.messageRight.setText(snd_name.get(position)+":"+"\n"+chat.get(position));
holder.messageRight.setVisibility(View.VISIBLE);
holder.messageLeft.setVisibility(View.GONE);
}
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
here is the view holder class
private static class ViewHolder {
public TextView messageLeft;
public TextView messageRight;
}
Just include holder.messageRight.setVisibility(View.GONE); and holder.messageLeft.setVisibility(View.GONE); in your if-else statement.
try {
if (snd_id.get(position).equalsIgnoreCase(appPref.getData("Uid"))) {
holder.messageLeft.setText(snd_name.get(position)+":"+"\n"+chat.get(position));
holder.messageLeft.setVisibility(View.VISIBLE);
holder.messageRight.setVisibility(View.GONE);
} else {
holder.messageRight.setText(snd_name.get(position)+":"+"\n"+chat.get(position));
holder.messageRight.setVisibility(View.VISIBLE);
holder.messageLeft.setVisibility(View.GONE);
}
Let me know whether it works for you or not.
You are pefroming findViewById every time..ViewHolder is not designed like that..Change your if-else condition like this and try..
if (convertView == null) {
convertView = LayoutInflater.from(getActivity()).inflate(
R.layout.chat_row, parent, false);
holder = new ViewHolder();
holder.messageLeft = (TextView) convertView
.findViewById(R.id.leftBox);
holder.messageRight = (TextView) convertView
.findViewById(R.id.rightBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
And remove
convertView.setTag(holder);
at the bottom
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getActivity()).inflate(
R.layout.chat_row, parent, false);
holder = new ViewHolder();
holder.messageLeft = (TextView) convertView
.findViewById(R.id.leftBox);
holder.messageRight = (TextView) convertView
.findViewById(R.id.rightBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
if (snd_id.get(position).equalsIgnoreCase(appPref.getData("Uid"))) {
holder.messageLeft.setText(snd_name.get(position)+":"+"\n"+chat.get(position));
holder.messageLeft.setVisibility(View.VISIBLE);
} else {
holder.messageRight.setText(snd_name.get(position)+":"+"\n"+chat.get(position));
holder.messageRight.setVisibility(View.VISIBLE);
}
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
This is the efficient and correct approach to use a viewholder

Categories

Resources