Android ListView Multiple Row Give Views values in getView method - android

In getView method i try to set textview text.I have two different type of object. If i use "position" in parameter it gives "IndexOutOfBoundsException" error normally. How can i set text each of them appropriately?
Context context;
ArrayList<OfficialKanal> officialKanals;
ArrayList<NormalKanal> normalKanals;
ArrayList<Object> kanallar = new ArrayList();
int OFFICIAL_KANAL = 0;
int NORMAL_KANAL= 1;
LayoutInflater lala;
public KanalAdapter(Context context , ArrayList<OfficialKanal> officiallar , ArrayList<NormalKanal> normaller){
this.context = context;
officialKanals = officiallar;
normalKanals = normaller;
for(int i = 0 ; i < officiallar.size() ; i++){
kanallar.add(officiallar.get(i));
}
for(int i = 0 ; i < normaller.size() ; i++){
kanallar.add(normaller.get(i));
}
Log.i("tago" , "tagtag");
lala = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return kanallar.size();
}
public Object getItem(int i) {
return kanallar.get(i);
}
public long getItemId(int i) {
return i;
}
public int getItemViewType(int position) {
Object item = getItem(position);
if(item instanceof NormalKanal){
return NORMAL_KANAL;
}else if (item instanceof OfficialKanal){
return OFFICIAL_KANAL;
}
return -1;
}
#Override
public int getViewTypeCount() {
return 2;
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
KanalHolder holder = null;
int pozisyon = 0;
Object currentKanal = getItem(position);
if(convertView==null) {
holder = new KanalHolder();
if (currentKanal instanceof OfficialKanal) {
convertView = lala.inflate(R.layout.officialkanal, null);
holder.image2 = (ImageView) convertView.findViewById(R.id.imageView5);
holder.tv3 = (TextView) convertView.findViewById(R.id.textView4);
holder.tv4 = (TextView) convertView.findViewById(R.id.textView8);
holder.buton2 = (Button) convertView.findViewById(R.id.button7);
holder.buton3 = (Button) convertView.findViewById(R.id.button8);
Log.i("tago", "tagtagatagtagtagatg");
}
if (currentKanal instanceof NormalKanal) {
convertView = lala.inflate(R.layout.normalkanal, null);
holder.image1 = (ImageView) convertView.findViewById(R.id.imageView5);
holder.tv1 = (TextView) convertView.findViewById(R.id.textView4);
holder.tv2 = (TextView) convertView.findViewById(R.id.textView8);
holder.buton1 = (Button) convertView.findViewById(R.id.button8);
Log.i("tago", "tagtagtag");
}
convertView.setTag(holder);
}else{
holder = (KanalHolder)convertView.getTag();
}
if(currentKanal instanceof OfficialKanal){
holder.tv3.setText(officialKanals.get(position).getKanaladi());
}
if(currentKanal instanceof NormalKanal){
holder.tv1.setText(normalKanals.get(position).getKanaladi());
}
return convertView;
}
static class KanalHolder{
public ImageView image1,image2;
public TextView tv1 , tv2,tv3,tv4;
public Button buton1,buton2,buton3;
}

issue is getCount getView will call according to size of kanallar
public int getCount() {
return kanallar.size();
}
but you getting value from officialKanals and normalKanals in getView
try like for experiment
if(currentKanal instanceof OfficialKanal){
holder.tv3.setText(kanallar.get(position).getKanaladi());
}
if(currentKanal instanceof NormalKanal){
holder.tv1.setText(kanallar.get(position).getKanaladi());
}

Related

Getting duplicate values in ListView android while scrolling

Whenever I scroll down, it shows me duplicate values.I have used setTag() method to avoid duplicate values but still duplicate values are there. Please provide help. I have added my Adapter class as follows :
CustomAdapter.java
#Override
public int getCount() {
return searchVedBeanList.size();
}
#Override
public Object getItem(int position) {
if (searchVedBeanList != null && searchVedBeanList.size() != 0 && searchVedBeanList.size() > position) {
return searchVedBeanList.get(position);
} else {
return null;
}
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressWarnings("deprecation")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final VedBean vedBean = (VedBean) getItem(position);
if (convertView == null) {
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new ViewHolder();
convertView = li.inflate(R.layout.search_ved_list_item, parent, false);
holder.mantraSansTextView = (TextView) convertView.findViewById(R.id.mantraSansTextView);
holder.vedTextView = (TextView) convertView.findViewById(R.id.vedTextView);
holder.commentratorTextView = (TextView) convertView.findViewById(R.id.commentratorTextView);
holder.rishiDevtaLinearLayout = (LinearLayout) convertView.findViewById(R.id.rishiDevtaLinearLayout);
holder.rishiTextView = (TextView) convertView.findViewById(R.id.rishiTextView);
holder.devtaTextView = (TextView) convertView.findViewById(R.id.devtaTextView);
// initialize Atharvaved components
holder.atharvavedVedLinearLayoutSearch = (LinearLayout) convertView.findViewById(R.id.atharvavedVedLinearLayoutSearch);
holder.kandTextViewAtharvaSearch = (TextView) convertView.findViewById(R.id.kandTextViewAtharvaSearch);
holder.suktaTextViewAtharvaSearch = (TextView) convertView.findViewById(R.id.suktaTextViewAtharvaSearch);
holder.mantraNumTextViewAtharvaSearch = (TextView) convertView.findViewById(R.id.mantraNumTextViewAtharvaSearch);
// initialize Yajurved components
holder.yajurVedLinearLayoutSearch = (LinearLayout) convertView.findViewById(R.id.yajurVedLinearLayoutSearch);
holder.adhyayTextViewYajurSearch = (TextView) convertView.findViewById(R.id.adhyayTextViewYajurSearch);
holder.mantraNumTextViewYajurSearch = (TextView) convertView.findViewById(R.id.mantraNumTextViewYajurSearch);
// initialize Samved components
holder.samVedLinearLayoutSearch = (LinearLayout) convertView.findViewById(R.id.samVedLinearLayoutSearch);
holder.archikTextViewSamSearch = (TextView) convertView.findViewById(R.id.archikTextViewSamSearch);
holder.adhyayTextViewSamSearch = (TextView) convertView.findViewById(R.id.adhyayTextViewSamSearch);
holder.dashatiTextViewSamSearch = (TextView) convertView.findViewById(R.id.dashatiTextViewSamSearch);
holder.mantraNumTextViewSamSearch = (TextView) convertView.findViewById(R.id.mantraNumTextViewSamSearch);
// initialize Rigved components
holder.rigVedLinearLayoutSearch = (LinearLayout) convertView.findViewById(R.id.rigVedLinearLayoutSearch);
holder.mandalTextViewRigSearch = (TextView) convertView.findViewById(R.id.mandalTextViewRigSearch);
holder.suktaTextViewRigSearch = (TextView) convertView.findViewById(R.id.suktaTextViewRigSearch);
holder.mantraNumTextViewRigSearch = (TextView) convertView.findViewById(R.id.mantraNumTextViewRigSearch);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
char charArr[] = vedBean.getMantraSans().toCharArray();
for (int i = 0; i < charArr.length; i++) {
holder.mantraSansTextView.setText(holder.mantraSansTextView.getText().toString() + charArr[i]);
}
String vedStr = "<b>"+UtilityConstant.STR_VED+" : "+"</b>"+UtilityConstant.getVedValueFromId(vedBean.getVedId());
holder.vedTextView.setText(Html.fromHtml(vedStr));
String commentatorStr = "<b>"+UtilityConstant.STR_COMMENTATOR+" : "+"</b>"+UtilityConstant.getCommentratorValueFromId(vedBean.getCommentratorId());
holder.commentratorTextView.setText(Html.fromHtml(commentatorStr));
holder.rishiTextView.setVisibility(View.GONE);
return convertView;
}

how to created a variable numer elements in listview android

I'm trying to make a listView that display a question witt a variable numbers of checkBoxs. I'm using a class that extends BaseAdapter. My problem come when the list need to scroll and then get rows repeated. I know where is my error but I cant get a solution. I need to extract part of the code out of the metod getView, like I did in title of the question but I cant make for the rest, Please help.
private class AdaptadorListaPreguntas extends BaseAdapter{
ArrayList<String> array;
public AdaptadorListaPreguntas(ArrayList<String> lista){
array=lista;
}
#Override
public int getCount() {
return array.size();
}
#Override
public Object getItem(int position) {
return array.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
HolderListaPreguntas holder;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
String frase = array.get(position);
if (convertView == null)
{
holder = new HolderListaPreguntas();
convertView = inflater.inflate(R.layout.lista_multiple_eleccion, parent, false);
convertView.setTag(holder);
//Count how many checkbox i need
for (int i=0 ; i<frase.length(); i++){
if (frase.charAt(i) == '\n'){
nCheckBox++;
}
}
//Rest one, need for the format of the string
nCheckBox--;
for(int i=0; i<nCheckBox; i++){
CheckBox cb = new CheckBox(getApplicationContext(),null,android.R.attr.checkboxStyle);
cb.setTextColor(Color.BLACK);
cb.setButtonDrawable(R.drawable.checkbox_multiple_eleccion);
//Put checkbox in to layout
holder.layout = (LinearLayout) convertView.findViewById(R.id.checkboxmultiple);
holder.layout.addView(cb);
}
//reset for next loop
nCheckBox = 0;
}
else{
holder = (HolderListaPreguntas) convertView.getTag();
}
holder.tituloPregunta = (TextView) convertView.findViewById(R.id.titulopreguntamultple);
holder.id = position;
holder.layout = (LinearLayout) convertView.findViewById(R.id.checkboxmultiple);
//Get text for each CheckBox
int indice = frase.indexOf("-");
String titulo = frase.substring(frase.indexOf(")")+1, frase.indexOf("-", indice+1));
holder.tituloPregunta.setText(titulo);
//Put all elements of layout in array
ArrayList<View> respuestas = holder.layout.getTouchables();
//Format of the text (only need "question") -Question;/;(id:n)(vof)
int indPriPreInit = frase.indexOf("-", frase.indexOf("-")+1);
int indPriPreFin= frase.indexOf(";/;");
//put text in the checkbox
for (View cb : respuestas){
if(cb instanceof CheckBox){
indPriPreInit = frase.indexOf("-", indPriPreInit);
indPriPreFin = frase.indexOf(";/;", indPriPreFin);
if (indPriPreInit > -1 && indPriPreFin > -1){
String textoCheckBox = (String) frase.subSequence(indPriPreInit, indPriPreFin);
indPriPreInit = indPriPreInit+1;
indPriPreFin = indPriPreFin+1;
//Relleno el texto del checkbox
((CheckBox) cb).setText(textoCheckBox);
}
}
}
return convertView;
}
}
public class AdaptadorListaPreguntas extends BaseAdapter implements CompoundButton.OnCheckedChangeListener {
private final ArrayList<String> array;
private final List<Set<Integer>> checked;
public AdaptadorListaPreguntas(ArrayList<String> lista){
array=lista;
checked = new ArrayList<Set<Integer>>(lista.size());
}
#Override
public int getCount() {
return array.size();
}
#Override
public Object getItem(int position) {
return array.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
HolderListaPreguntas holder;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
String frase = array.get(position);
int checkboxCount = frase.split("\n").length-1;
if (convertView == null) {
holder = new HolderListaPreguntas();
convertView = inflater.inflate(R.layout.lista_multiple_eleccion, parent, false);
holder.layout = (LinearLayout) convertView.findViewById(R.id.checkboxmultiple);
convertView.setTag(holder);
}
else{
holder = (HolderListaPreguntas) convertView.getTag();
}
Set<Integer> current;
try {
current = checked.get(position);
} catch(NullPointerException e) {
current = new HashSet<Integer>(checkboxCount);
checked.set(position, current);
}
List<CheckBox> respuestas = new ArrayList<CheckBox>(checkboxCount);
holder.layout.removeAllViews();
for(int i=0; i < checkboxCount; i++) {
CheckBox cb = new CheckBox(getApplicationContext(),null,android.R.attr.checkboxStyle);
cb.setTextColor(Color.BLACK);
cb.setButtonDrawable(R.drawable.checkbox_multiple_eleccion);
respuestas.add(cb);
cb.setTag(position);
if (current.contains(position))
cb.setChecked(true);
holder.layout.addView(cb);
}
holder.tituloPregunta = (TextView) convertView.findViewById(R.id.titulopreguntamultple);
holder.id = position;
holder.layout = (LinearLayout) convertView.findViewById(R.id.checkboxmultiple);
//Get text for each CheckBox
int indice = frase.indexOf("-");
String titulo = frase.substring(frase.indexOf(")")+1, frase.indexOf("-", indice+1));
holder.tituloPregunta.setText(titulo);
//Format of the text (only need "question") -Question;/;(id:n)(vof)
int indPriPreInit = frase.indexOf("-", frase.indexOf("-")+1);
int indPriPreFin= frase.indexOf(";/;");
//put text in the checkbox
for (CheckBox cb : respuestas){
indPriPreInit = frase.indexOf("-", indPriPreInit);
indPriPreFin = frase.indexOf(";/;", indPriPreFin);
if (indPriPreInit > -1 && indPriPreFin > -1){
String textoCheckBox = (String) frase.subSequence(indPriPreInit, indPriPreFin);
indPriPreInit = indPriPreInit+1;
indPriPreFin = indPriPreFin+1;
//Relleno el texto del checkbox
cb.setText(textoCheckBox);
}
}
return convertView;
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int position = (Integer)buttonView.getTag();
ViewGroup vg = (ViewGroup)buttonView.getParent();
List<View> touchables = vg.getTouchables();
int index = touchables.indexOf(buttonView);
if (isChecked) {
checked.get(position).add(index);
} else {
checked.get(position).remove(index);
}
}
}
Finally i decided to use a scrollView instead a listView, the reason is the listView use a recycle system that i dont want to use and its the problem!
I found more info of How Work a ListView in: Link
Thanks all for the help.

How to create listview for displaying sender receiver messages using baseadapter?

when we scroll down or up getview problem?
getview not called properly?
at background images are loading from respective url?
layout is different for sender and receiver..
public class NewReadMessageAdapter extends BaseAdapter {
private Context context;
private LayoutInflater vi;
public ImageLoader imageLoader;
private Typeface tf;
private class Image {
String pic_url = new String();
Bitmap thumb;
String messege = new String();
int id;
String time = new String();
String sender_name = new String();
public boolean sender_user;
}
private Image[] images;
ArrayList<Messages> messages = new ArrayList<Messages>();
private TextView txt_msg;
private TextView txt_name;
private TextView txt_time;
Button btn_loc;
private ImageView img;
private ArrayList<Messages> mData = new ArrayList<Messages>();
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
private static final int TYPE_RECEIVER = 0;
private static final int TYPE_SENDER = 1;
public NewReadMessageAdapter(Context context,
ReadMessagePostParser messageParser, String sender_id,
String reciever_id) {
this.context = context;
tf = Typeface.createFromAsset(context.getAssets(), "fonts/tahoma.ttf");
messages = messageParser.getMessages();
imageLoader = new ImageLoader(context.getApplicationContext());
images = new Image[messages.size()];
for (int i = 0, j = messages.size(); i < j; i++) {
images[i] = new Image();
images[i].messege = messages.get(i).getMessage_text();
images[i].time = messages.get(i).getTime();
if (messages.get(i).getMessage_from().equals("YES")) {
images[i].sender_name = messageParser.sender_name;
images[i].pic_url = messageParser.sender_pic_url;
images[i].sender_user = true;
images[i].id = Integer.parseInt(sender_id);
} else {
images[i].sender_user = false;
images[i].id = Integer.parseInt(reciever_id);
images[i].sender_name = messageParser.reciever_name;
images[i].pic_url = messageParser.receiver_pic_url;
}
}
for (int i = 0; i < images.length; i++) {
System.out.println(images[i].pic_url + " " + images[i].sender_name
+ " " + images[i].messege);
}
vi = LayoutInflater.from(context);
imageLoader = new ImageLoader(context.getApplicationContext());
}
public void addItem(final Messages item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final Messages item) {
mData.add(item);
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getCount() {
return mData.size();
}
#Override
public Object getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SENDER : TYPE_RECEIVER;
}
#Override
public View getView( int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
switch (type) {
case TYPE_SENDER:
System.out.println("Sender");
holder = new ViewHolder();
convertView = vi.inflate(R.layout.list_row_layout_sender, null);
holder.txt_msg = (TextView) convertView
.findViewById(R.id.txt_msg);
holder.txt_name = (TextView) convertView
.findViewById(R.id.txt_name);
holder.txt_time = (TextView) convertView
.findViewById(R.id.txt_time);
holder.img = (ImageView) convertView
.findViewById(R.id.user_img);
holder.btn_loc = (Button) convertView
.findViewById(R.id.btn_loc);
convertView.setTag(holder);
break;
case TYPE_RECEIVER:
System.out.println("Receiver");
convertView = vi.inflate(R.layout.list_row_layout_receiver, null);
holder = new ViewHolder();
holder.txt_msg = (TextView) convertView
.findViewById(R.id.txt_msg);
holder.txt_name = (TextView) convertView
.findViewById(R.id.txt_name);
holder.txt_time = (TextView) convertView
.findViewById(R.id.txt_time);
holder.img = (ImageView) convertView
.findViewById(R.id.user_img);
holder.btn_loc = (Button) convertView
.findViewById(R.id.btn_loc);
convertView.setTag(holder);
break;
}
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.btn_loc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out
.println("-------------- btn_loc ------------------- Sender");
Developer.callIntent_userlocation(context, "18.50929796 ",
"73.79045371");
}
});
holder.txt_name.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("----Name----");
}
});
holder.img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("----Image----");
}
});
holder.txt_name.setText(mData.get(position).message_from);
holder.txt_time.setText(mData.get(position).time);
holder.txt_msg.setText(mData.get(position).message_text);
if (position != 0) {
cached = images[position - 1];
if (cached.sender_user == flag) {
holder.img.setVisibility(ImageView.INVISIBLE);
holder.txt_name.setVisibility(TextView.INVISIBLE);
} else
imageLoader.DisplayImage(images[position].pic_url.trim()
.replace(" ", "%20"), ReadMessageActivityNew.activity,
holder.img);
} else
imageLoader.DisplayImage(
images[position].pic_url.trim().replace(" ", "%20"),
ReadMessageActivityNew.activity, holder.img);
return convertView;
}
static class ViewHolder {
TextView txt_msg;
TextView txt_name;
TextView txt_time;
ImageView img;
Button btn_loc;
}
}
It is better to reuse convertView only when both the layouts are the same. As the layouts are different here, when you reuse a convertView used by sender for receiver, then the view may not be as expected.

Android Icon on listView Custom Adaptor with separators

I want to include a play icon on a list view when ever user clicks on a row.
I am using a custom adapter (Lazy Load with separators)... I had issue with separator being overlapped with other rows so i implemented getViewTypeCount() method to resolve it.
Now when i am including a play icon on onItemClickListener of the list view, icon is getting added fine but its overlapping with other rows(except separator)...
Heres my code :
OnItemCLickListener :
listChannels.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if (oldView != null) {
oldView.setDrawingCacheEnabled(true);
ImageView icon = (ImageView) oldView.findViewById(R.id.imageViewPlay);
icon.setImageBitmap(null);
icon.setVisibility(ImageView.GONE);
icon.invalidate();
}
oldView = arg1;
selectedItem = arg2;
ImageView icon = (ImageView) arg1.findViewById(R.id.imageViewPlay);
icon.setImageResource(R.drawable.play_small);
icon.setVisibility(ImageView.VISIBLE);
Intent intent = new Intent(StationList2.this,
ServiceLauncher.class);
intent.putExtra("objectPassed", feeds.get(arg2));
startActivity(intent);
}
});
My Custom Adapter :
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList<BeanChannelList> mData = new ArrayList<BeanChannelList>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public MyCustomAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public void addItem(final BeanChannelList beanChannelList) {
mData.add(beanChannelList);
notifyDataSetChanged();
}
public void addSeparatorItem(final BeanChannelList beanChannelList) {
mData.add(beanChannelList);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR
: TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
return mData.get(position).getStrTitle();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater
.inflate(R.layout.listitem_row, null);
holder.textView = (TextView) convertView
.findViewById(R.id.textView1);
holder.textview2 = (TextView) convertView
.findViewById(R.id.textView2);
holder.image = (ImageView) convertView
.findViewById(R.id.imageView1);
holder.imagePlay = (ImageView) convertView
.findViewById(R.id.imageViewPlay);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item2, null);
holder.textView = (TextView) convertView
.findViewById(R.id.textView1);
holder.textview2 = (TextView) convertView
.findViewById(R.id.textView2);
holder.image = (ImageView) convertView
.findViewById(R.id.imageView1);
holder.imagePlay = (ImageView) convertView
.findViewById(R.id.imageViewPlay);
convertView.setClickable(false);
convertView.setFocusable(false);
convertView.setFocusableInTouchMode(false);
convertView.setEnabled(false);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
if (mData.get(position).getStrChannelNo().equals("")) {
holder.textView.setText(mData.get(position)
.getStrChannelNo()
+ " "
+ mData.get(position).getStrTitle());
}
else {
holder.textView.setText(mData.get(position)
.getStrChannelNo()
+ ": "
+ mData.get(position).getStrTitle());
}
if (selectedItem == position) {
holder.imagePlay.setImageResource(R.drawable.play_small);
holder.imagePlay.setVisibility(ImageView.VISIBLE);
}
holder.textview2.setText(mData.get(position).getStrLocation());
imageLoader.DisplayImage(mData.get(position).getStrImage()
.toString(), activity, holder.image);
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
}
I guess problem is in ViewHolder. Once I had also same kind of problem. Do not use View Holder.

Update textview in gallery

I am trying to update the textview in my gallery after it has been created using my updateCaption method, however, only the first textview in the gallery gets updated. When I print out my textview, they all have the same ID like android.widget.TextView#40942d60.
public class ImageAdapter extends BaseAdapter {
private static LayoutInflater inflater = null;
private int[] image;
private TextView caption;
public ImageAdapter(Context context) {
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
image = new int[4];
for (int i = 0; i < image.length; i++)
{
image[i] = R.drawable.image;
}
}
public void updateCaption(String text, int position)
{
System.out.println(position + ": text");
caption.setText(text);
System.out.println(caption);
}
public int getCount() {
return image.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView text, dssnum, scnum;
public ImageView image;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View vi = convertView;
ViewHolder holder;
if (convertView == null)
{
vi = inflater.inflate(R.layout.caption, null);
holder = new ViewHolder();
caption = holder.text = (TextView) vi.findViewById(R.id.textView1);
holder.scnum = (TextView) vi.findViewById(R.id.scnum);
holder.dssnum = (TextView) vi.findViewById(R.id.dss_num);
holder.image = (ImageView) vi.findViewById(R.id.image);
vi.setTag(holder);
}
else
holder = (ViewHolder) vi.getTag();
holder.dssnum.setText("Unavailable");
holder.image.setImageResource(image[position]);
holder.text.setText("Not available.");
return vi;
}
}
Does anyone know what I am doing wrong?
Added: how I am using updateCaption
for (int j = 0; j < listOfList.get(i).size(); j++)
{
galleryImageAdapter.updateCaption("test " + j, j);
}
I think this is due to this line...
caption = holder.text = (TextView) vi.findViewById(R.id.textView1);
once all convertView is created, your caption will always point to the textview of the last convertView.

Categories

Resources