Android:滚动ListView后,onItemClick()调用false项

时间:2013-02-27 07:38:23

标签: android listview scroll

这是我的ListView:

enter image description here

滚动此列表视图后,单击例如“隐藏”,调用“收藏夹”操作(和类似的错误 - 如果我按“压缩”,则会调用其他内容)。

这只会在滚动后发生。从不滚动ListView。

这是我的适配器:

package com.landa.adapter;

import java.util.ArrayList;

import com.example.fileexplorermanager.R;
import com.landa.dialog.OperationsDialogFragment;

public class OperationsAdapter extends ArrayAdapter<String> {
  private final Context context;
  private final ArrayList<String> data;

  public OperationsAdapter(Context context, ArrayList<String> values) {
    super(context, R.layout.operations_list_item, values);
    this.context = context;
    this.data = values;
  }

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

    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //View rowView = inflater.inflate(R.layout.operations_list, parent, false);
    View rowView = inflater.inflate(R.layout.operations_list_item, parent, false);

    TextView textView = (TextView) rowView.findViewById(R.id.operation_name);

    String temp = data.get(position);
    String op_name = temp;
    textView.setText(op_name);

    ImageView imageView = (ImageView) rowView.findViewById(R.id.operation_image);

    return rowView;

  }
  public int getCount() {
      return data.size();
  }

}

这是对话框(ListView在Dialog中):

public class OperationsDialogFragment extends DialogFragment {

// operation names
public static final String OP_CUT = "Cut";
public static final String OP_COPY = "Copy";
public static final String OP_RENAME = "Rename";
public static final String OP_DELETE = "Delete";
public static final String OP_SELECT_ALL = "Select all";
public static final String OP_SELECT_INVERSE = "Select inverse";
public static final String OP_CREATE_SHORTCUT = "Create shortcut";
public static final String OP_FAVORITE = "Favorite";
public static final String OP_HIDE = "Hide";
public static final String OP_COMPRESS = "Compress";
public static final String OP_SET_AS_HOME = "Set as home";
public static final String OP_PROPERTIES = "Properties";

ArrayList<String> operationsInfo = new ArrayList<String>();

private OperationsHandler opHandler;
private File f;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    opHandler = OperationsHandler.getInstance();

    final String operation_type = getArguments().getString("operation_type");
    final String file_absolute_path = getArguments().getString("file_absolute_path");

    if(file_absolute_path != null)
        this.f = new File(file_absolute_path);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    ArrayList<String> operationsList;
    if (operation_type.equals("single_file")) {
        setSingleFileDialogTitle(builder);
        operationsList = getSingleFileOperationsList();
    } else if (operation_type.equals("multiple_files")) {
        setDefaultDialogTitle(builder);
        operationsList = getMultipleFilesOperationsList();
    } else {
        setDefaultDialogTitle(builder);
        operationsList = getDefaultOperationsList();
    }

    OperationsAdapter adap = new OperationsAdapter(getActivity(),
            operationsList);

    // upon operation click, execute operation
    builder.setAdapter(adap, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            String op_name = getOperationName(((AlertDialog) dialog)
                    .getListView().getChildAt(whichButton));
            if (operation_type.equals("single_file")) {
                executeSingleFileOperation(op_name);
            } else {
                executeMultipleFilesOperation(op_name);
            }
        }
    });

    return builder.create();
}

可能出现什么问题?

1 个答案:

答案 0 :(得分:0)

通过获取onClick ListView项而不是position ListView,为Dialog项实施OnClick。我认为这就是问题所在。