基本适配器具有两种不同类型的行

时间:2013-09-10 08:13:51

标签: android listview android-adapter baseadapter custom-adapter

我已经创建了一个自定义适配器来填充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等中保留的内容等。请帮忙

非常感谢

1 个答案:

答案 0 :(得分:0)

setText()来电转移到if(convertView == null)部分之后。

public View getView(int position, View convertView, ViewGroup parent) {
    Holder holder;

    if(convertView == null){
        if(type == TYPE_SENDER){
            // inflate sender view
        }else{
            // inflate receiver view
        }

        holder = new Holder();
        convertView.setTag(holder);

        holder.textView = (TextView) convertView.findViewById(R.id.text);
    }else{
        holder = (Holder) convertView.getTag();
    }

    holder.textView.setText(getItem(position));

    return convertView;
}