I have created a Custom adapter to fill ListView extending base adapter.
In my layout each row have same views (One RelativeLayout
, other TextView
) .But row layout may be different like in one on left hand side ,I have shown RelativeLayout
.On other row layout, RelativeLayout
is on right hand side, TextView
in middle in both. I have successfully implemented that (module to divide row base on some criteria).
My problem is here: The TextView
available on both type of row layout needs some text from json. when i scroll the whole ListView
the text works randomized. Don't know what is happening there. Some times is works but whenever i scroll the ListView
the setText()
method doesnt works correctly. Please Suggest. Here is my base adapter class.
public class AwesomeAdapter extends BaseAdapter {
private Activity activity;
Context context;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
String audiopath, audiopathrec;
String pathOfAudio, pathOfAudiorec;
String filenameectString;
private MediaPlayer mediaPlayer = null;
private String OUTPUT_FILE = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/" + "varun.m4a";
File outFile = new File(OUTPUT_FILE);
private static final int TYPE_SENDER = 0;
private static final int TYPE_RECEIVER = 1;
private static final int TYPE_MAX_COUNT = TYPE_RECEIVER + 1;
private ArrayList<String> mData = new ArrayList<String>();
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public AwesomeAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final String item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_RECEIVER : TYPE_SENDER;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return (String) mData.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
int type = getItemViewType(position);
// View vi = convertView;
context = parent.getContext();
if (convertView == null) {
holder = new ViewHolder();
HashMap<String, String> events = new HashMap<String, String>();
events = data.get(position);
audiopath = events.get(Conversation.TAG_AUDIOPATH);
switch (type) {
case TYPE_SENDER:
convertView = inflater.inflate(
R.layout.chatbubble_listrow_sender, null);
holder.textView = (TextView) convertView
.findViewById(R.id.date);
holder.relativeLayout = (RelativeLayout) convertView
.findViewById(R.id.audio_image);
holder.relativeLayout.setTag(audiopath);
holder.relativeLayout
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopPlayBack();
pathOfAudio = String.valueOf(v.getTag());
Toast.makeText(context, pathOfAudio,
Toast.LENGTH_SHORT).show();
Log.e("Audio Path name", "Audio Path ==>> "
+ audiopath);
new LoadChats().execute();
}
});
holder.textView.setText(audiopath);
break;
case TYPE_RECEIVER:
convertView = inflater.inflate(R.layout.chatbubbles_listrow,
null);
holder.textView = (TextView) convertView
.findViewById(R.id.date);
holder.relativeLayout = (RelativeLayout) convertView
.findViewById(R.id.audio_image);
holder.relativeLayout.setTag(audiopath);
holder.relativeLayout
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopPlayBack();
pathOfAudio = String.valueOf(v.getTag());
Toast.makeText(context, pathOfAudio,
Toast.LENGTH_SHORT).show();
Log.e("Audio Path name", "Audio Path ==>> "
+ audiopath);
new LoadChats().execute();
}
});
holder.textView.setText(audiopath);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//convertView.setTag(holder);
return convertView;
}
public static class ViewHolder {
public TextView textView;
public RelativeLayout relativeLayout;
}
}
I know problem is in getView()
method. Here I need some customization but unable to resolve that, unable to resolve where to setText and where to setTag and what i need to kept in switch case etc etc. Please help
Thanks in adavnce