我创建了一个自定义适配器来填充 ListView 扩展基本适配器。
在我的布局中,每一行都有相同的视图(一个RelativeLayout
,另一个TextView
)。但是行布局可能会有所不同,就像在左侧的一个,我已经展示了RelativeLayout
。在其他行布局上,RelativeLayout
在右侧,TextView
在两个中间。我已经成功地实现了这一点(根据某些标准划分行的模块)。
我的问题在这里:TextView
两种类型的行布局都需要一些来自 json 的文本。当我滚动整个ListView
文本时,文本是随机的。不知道那里发生了什么。有时是可行的,但每当我滚动该方法时,ListView
该方法都setText()
无法正常工作。请建议。这是我的基本适配器类。
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;
}
}
我知道问题出在getView()
方法上。在这里我需要一些自定义,但无法解决,无法解决 setText 和 setTag 的位置以及我需要在 switch case 中保留的内容等。请帮助
提前感谢