Android 记录持有者自定义适配器的问题

Android 记录持有者自定义适配器的问题,android,Android,我有listview的listAdapter,我在自定义适配器中设置了record按钮。我已将代码放入holder的点击事件中。记录like holder.record.setBackgroundResource(R.drawable.record_green); 用于在用户触摸时设置该按钮的背景。但我的问题是,当我触摸特定列表项按钮时,该图像将应用于另一个列表项 以下是代码: holder.record.setOnClickListener(new View.OnClickListener

我有listview的listAdapter,我在自定义适配器中设置了record按钮。我已将代码放入
holder的点击事件中。记录
like

holder.record.setBackgroundResource(R.drawable.record_green); 
用于在用户触摸时设置该按钮的背景。但我的问题是,当我触摸特定列表项按钮时,该图像将应用于另一个列表项

以下是代码:

holder.record.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        if (isplaying2 == true) {

        } else
        { 
            if (b_Flag_Record_or_not == true) {
            } else {
                try {

                    holder.record.setBackgroundResource(R.drawable.record_green);
                    b_Flag_Record_or_not = true;
                    b_play_or_not = true;

                    flag_stop_position = position;
                    AuthHandler dataHandler = new AuthHandler();
                    AuthDataset dataset = dataHandler
                            .getParsednewJobdtl_DataSet();

                    System.out.println("dataset.getint1();"
                            + dataset.getint1());
                    // Login l = new Login();
                    String str_useid = RequestTo[position];
                    recorder = new AudioRecorder("/audiometer/shanesh"
                            + RequestId[position] + "-" + str_useid);
                    start_or_not = true;
                    recorder.start();
                    Toast.makeText(context, "Start Recording", Toast.LENGTH_LONG).show();
                    CountDownTimer countDowntimer = new CountDownTimer(
                            120000000, 1000) {
                        public void onTick(long millisUntilFinished) {
                        }

                        public void onFinish() {
                            try {
                                Toast.makeText(
                                        context,
                                        "Stop recording Automatically ",
                                        Toast.LENGTH_LONG).show();
                                recorder.stop();
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    };
                    countDowntimer.start();

                } catch (IOException e) {
                    Writer writer = new StringWriter();
                    PrintWriter printWriter = new PrintWriter(writer);
                    e.printStackTrace(printWriter);
                    String s = writer.toString();

                }catch (Exception e) {

                }
            }

        }
    }
});
更新:

public class ListViewAdapter extends BaseAdapter {

public static Activity context;

String title[];
String description[];
String RequestId[];
String Folderpath[];
String RequestTo[];

boolean b_Flag_Record_or_not = false;
boolean b_upload_or_not = false;
boolean start_or_not = false;
boolean start_play = false;
boolean upload_or_not = false;
boolean b_play_or_not = false;
boolean isplaying2 = false;

Thread welcomeThread;
int glob_position;
MediaPlayer mPlayer2;
int flag_stop_position;
AudioRecorder recorder;
AnimationDrawable frameAnimation, frameAnimation_play;
private static String mFileName = null;
private MediaRecorder mRecorder = null;
private MediaPlayer mPlayer = null;
Recording login = new Recording();
ViewHolder holder;
MediaPlayer MP_completeRequest = new MediaPlayer();

public ListViewAdapter(Activity context, String[] title,
        String[] description, String[] req_id, String[] FolderPath,
        String[] Arr_RequestTo) {
    super();
    this.context = context;
    this.title = title;
    this.description = description;
    this.RequestId = req_id;
    this.Folderpath = FolderPath;
    this.RequestTo = Arr_RequestTo;
}

public int getCount() {
    // TODO Auto-generated method stub
    return title.length;
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

private class ViewHolder {
    TextView txtViewTitle;
    TextView txtViewDescription;
    Button record, stop, play, upload;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    LayoutInflater inflater = context.getLayoutInflater();
    glob_position = position;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.listitem_row, null);
        holder = new ViewHolder();
        holder.txtViewTitle = (TextView) convertView
                .findViewById(R.id.textView1);
        holder.txtViewDescription = (TextView) convertView
                .findViewById(R.id.textView2);
        holder.record = (Button) convertView.findViewById(R.id.record);

        holder.stop = (Button) convertView.findViewById(R.id.stop);
        holder.play = (Button) convertView.findViewById(R.id.play1);
        holder.upload = (Button) convertView
                .findViewById(R.id.audio_upload);
        /* set button image */



        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    try {

        Typeface face = Typeface.createFromAsset(context.getAssets(),
                "fonts/tahoma.ttf");
        holder.txtViewTitle.setTypeface(face);

        holder.txtViewTitle.setText(title[position]);
        holder.txtViewDescription.setText(description[position]);


    } catch (ArrayIndexOutOfBoundsException e) {
        e.printStackTrace();
    }

请参阅Holder用于在adapterview中重用视图,因此,如果一个屏幕可以一次显示5个项目,则第6个项目将重用项目1的视图,因此Holder

使用ListView.onItemClickListener而不是holder.record.setOnClickListener(),或者将视图背景设置为getView方法,我正在getView方法中编辑您的代码:

    public class ListViewAdapter extends BaseAdapter {

public static Activity context;

String title[];
String description[];
String RequestId[];
String Folderpath[];
String RequestTo[];

boolean b_Flag_Record_or_not = false;
boolean b_upload_or_not = false;
boolean start_or_not = false;
boolean start_play = false;
boolean upload_or_not = false;
boolean b_play_or_not = false;
boolean isplaying2 = false;

Thread welcomeThread;
int glob_position;
MediaPlayer mPlayer2;
int flag_stop_position;
AudioRecorder recorder;
AnimationDrawable frameAnimation, frameAnimation_play;
private static String mFileName = null;
private MediaRecorder mRecorder = null;
private MediaPlayer mPlayer = null;
Recording login = new Recording();
ViewHolder holder;
MediaPlayer MP_completeRequest = new MediaPlayer();

public ListViewAdapter(Activity context, String[] title,
        String[] description, String[] req_id, String[] FolderPath,
        String[] Arr_RequestTo) {
    super();
    this.context = context;
    this.title = title;
    this.description = description;
    this.RequestId = req_id;
    this.Folderpath = FolderPath;
    this.RequestTo = Arr_RequestTo;
}

public int getCount() {
    // TODO Auto-generated method stub
    return title.length;
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

private class ViewHolder {
    TextView txtViewTitle;
    TextView txtViewDescription;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    LayoutInflater inflater = context.getLayoutInflater();
    glob_position = position;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.listitem_row, null);
        holder = new ViewHolder();
        holder.txtViewTitle = (TextView) convertView
                .findViewById(R.id.textView1);
        holder.txtViewDescription = (TextView) convertView
                .findViewById(R.id.textView2);

        /* set button image */



        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    try {

        Typeface face = Typeface.createFromAsset(context.getAssets(),
                "fonts/tahoma.ttf");
        holder.txtViewTitle.setTypeface(face);

        holder.txtViewTitle.setText(title[position]);
        holder.txtViewDescription.setText(description[position]);


    } catch (ArrayIndexOutOfBoundsException e) {
        e.printStackTrace();
    }

    Button record = (Button) convertView.findViewById(R.id.record);

    Button stop = (Button) convertView.findViewById(R.id.stop);
    Button play = (Button) convertView.findViewById(R.id.play1);
    Button upload = (Button) convertView
                .findViewById(R.id.audio_upload);
    record.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        if (isplaying2 == true) {

        } else
        { 
            if (b_Flag_Record_or_not == true) {
            } else {
                try {

                    record.setBackgroundResource(R.drawable.record_green);
                    b_Flag_Record_or_not = true;
                    b_play_or_not = true;

                    flag_stop_position = position;
                    AuthHandler dataHandler = new AuthHandler();
                    AuthDataset dataset = dataHandler
                            .getParsednewJobdtl_DataSet();

                    System.out.println("dataset.getint1();"
                            + dataset.getint1());
                    // Login l = new Login();
                    String str_useid = RequestTo[position];
                    recorder = new AudioRecorder("/audiometer/shanesh"
                            + RequestId[position] + "-" + str_useid);
                    start_or_not = true;
                    recorder.start();
                    Toast.makeText(context, "Start Recording", Toast.LENGTH_LONG).show();
                    CountDownTimer countDowntimer = new CountDownTimer(
                            120000000, 1000) {
                        public void onTick(long millisUntilFinished) {
                        }

                        public void onFinish() {
                            try {
                                Toast.makeText(
                                        context,
                                        "Stop recording Automatically ",
                                        Toast.LENGTH_LONG).show();
                                recorder.stop();
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    };
                    countDowntimer.start();

                } catch (IOException e) {
                    Writer writer = new StringWriter();
                    PrintWriter printWriter = new PrintWriter(writer);
                    e.printStackTrace(printWriter);
                    String s = writer.toString();

                }catch (Exception e) {

                }
            }

        }
    }
});

粘贴getView方法的完整代码。有什么变化,我仍然没有找到…我比较了整个代码,但没有发现变化…请帮助mestill解决问题。