如何在android中禁用一行listview?

时间:2016-07-05 12:42:08

标签: android listview

我有一个ListView,它的每一行都由TextView和Button组成。

单击按钮时,会显示一个警告对话框,询问“您确定要禁用此项目吗?”,如果有人选择“是”按钮,TextView和按钮将被禁用。

我的代码如上。因此,TextView和Button被正确禁用,但当我向下和向上滚动ListView时,这些项目将再次启用。

我怎么能解决这个问题?

这是我的代码:

private class QuestionAdapter extends ArrayAdapter<String> {
    public QuestionAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
        super(context, resource, textViewResourceId, strings);
    }

    @SuppressLint("ViewHolder") @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View row = inflater.inflate(R.layout.list_layout_edit_sample_question, parent, false);

        TextView txt_question = (TextView) row.findViewById(R.id.txt_question);
        txt_question.setText(qText[position]);

        final int pos = position;

        Button btn_delete = (Button) row.findViewById(R.id.btn_delete);
        btn_delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                DeleteDialog(context, pos, row);
            }
        });

        return row;
    }
}

private void DeleteDialog(final Context context, final int pos, final View row) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    alertDialog.setMessage("are you sure you want to disable this item?");

    // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            ((TextView) row.findViewById(R.id.txt_question)).setEnabled(false);
            ((Button) row.findViewById(R.id.btn_delete)).setEnabled(false);
        }
    });

    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}

4 个答案:

答案 0 :(得分:0)

使用ViewHolder

static class ViewHolder {
TextView textview;
Button button;
boolean disabled;
}

在您的阵列适配器

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(<your layout id>, null);
        holder = new ViewHolder();
        TextView txt_question = (TextView) row.findViewById(R.id.txt_question);

       holder.textview = txt_question;
    final int pos = position;

    Button btn_delete = (Button) row.findViewById(R.id.btn_delete);

        holder.disabled = false;
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }
    final View row = convertView;
    //Set views here
    holder.textview.setText(qText[position]);
     holder.button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {

            DeleteDialog(context, pos, row);
        }
    });
    //Check for disabled 
    if(disabled){
        //disable the views
    }
    else{
       //enable the views.
    }

    return convertView;
}

并在viewHolder中设置禁用

private void DeleteDialog(final Context context, final int pos, final View row) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setMessage("are you sure you want to disable this item?");

// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        ViewHolder holder = (ViewHolder) row.getTag();
        holder.disabled = true;
        ((TextView) row.findViewById(R.id.txt_question)).setEnabled(false);
        ((Button) row.findViewById(R.id.btn_delete)).setEnabled(false);
    }
});

// Setting Negative "NO" Button
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});

// Showing Alert Message
alertDialog.show();

}

答案 1 :(得分:0)

您只需使用:

  

listview.getChildAt(childPosition).setEnabled(假);

<强> 实施例

if(listview.getChildAt(childPosition).isEnabled())
{
    listview.getChildAt(childPosition).setEnabled(false);
}

答案 2 :(得分:0)

利用isEnabled method of BaseAdapter。覆盖它并为启用的索引保留一个布尔数组。

boolean[] isEnabled; //initialize somewhere, everything to true

private class QuestionAdapter extends ArrayAdapter<String> {
    public QuestionAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
        super(context, resource, textViewResourceId, strings);
    }

    @Override
    public boolean isEnabled(int position) {
        return isEnabled[position];
    }
}

private void DeleteDialog(final Context context, final int pos, final View row) {

    // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            isEnabled[position] = false;
            questionAdapter.notifyDataSetChanged();
        }
    });

}

答案 3 :(得分:0)

实际上,当你滚动它时,每次膨胀新视图时你的ListView都不会保留旧视图。你可以使用holder模式来解决这个问题,但真正的问题仍然存在,这与你的数据绑定技术有关。

使用某些数据模型(POJO),它应该具有状态(启用/禁用)并使用该状态来启用或禁用您的视图。从对话框更新模型状态,并通过调用notifydatasetchanged。

刷新适配器