Android - SetOnItemClickListener不起作用

时间:2016-03-01 19:53:53

标签: android listview onclicklistener android-checkbox

我知道这里有很多像我这样的问题,但我不知道为什么没有一个问题适合我。

我的目标:我有一个带有ListView的AlertDialog,每行都有一个复选框,我可以选择一些项目,我希望创建一个包含所选元素的ArrayList。

因为这个原因,我正在调用SetOnclickListener,我在方法中放了一个Log,但什么也没做。

我尝试了几乎无处不在的可聚焦和可点击,但我的日志没有出现。

这里是我的提醒对话

private void callAdditionalDialog() {
    LayoutInflater layoutInflater = LayoutInflater.from(ConfigProductActivity.this);
    final View additionalView = layoutInflater.inflate(R.layout.dialog_additional, null);
    additionalView.setFocusable(true);

    // set the custom dialog components - text, buttons, accountants
    TextView titleDialog = (TextView) additionalView.findViewById(R.id.title_additional);
    titleDialog.setTypeface(boldFont);
    Button buttonAccept = (Button) additionalView.findViewById(R.id.button_accept);
    buttonAccept.setTypeface(boldFont);
    Button buttonCancel = (Button) additionalView.findViewById(R.id.button_cancel);
    buttonCancel.setTypeface(boldFont);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ConfigProductActivity.this);
    alertDialogBuilder.setView(additionalView);
    final AlertDialog alertD = alertDialogBuilder.create();
    alertD.setCanceledOnTouchOutside(false);
    //Fill object of additional
    final ListView additionalListView = (ListView) additionalView.findViewById(R.id.list_additional);
    TextView additionalNotFound = (TextView) additionalView.findViewById(R.id.additional_not_found);
    if (!withoutModifiers){
        additionalAdapter = new AdditionalAdapter(ConfigProductActivity.this, additionalList);
        additionalListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        additionalListView.setAdapter(additionalAdapter);
        final ArrayList<ModifierEntity> modifierList = new ArrayList<ModifierEntity>();
        additionalListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent,
                                    View view, int position, long id) {
                Object modifier = additionalListView.getAdapter().getItem(position).toString();
                Log.d(TAG, "SOMETHIIIIING");
            }
        });
    }
    else{
        additionalListView.setVisibility(View.GONE);
        additionalNotFound.setVisibility(View.VISIBLE);
        additionalNotFound.setTypeface(font);
        buttonCancel.setVisibility(View.GONE);
    }
    //End of fill object of additional
    buttonCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            additionalBox.setEnabled(true);
            alertD.dismiss();
        }
    });

    buttonAccept.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //additional.setText(additionalAdapter.getCount());
            additionalBox.setEnabled(true);
            alertD.dismiss();
        }
    });
    alertD.show();
}

这是我的适配器:

public class AdditionalAdapter extends ArrayAdapter {
private static String TAG = AdditionalAdapter.class.getName();
private List<ModifierEntity> originalData = null;
private ConfigProductActivity activity;
private Typeface font;
private Typeface boldFont;
private static ModifierEntity modifier;

public AdditionalAdapter (ConfigProductActivity activity, List<ModifierEntity> listArray){
    super(activity, R.layout.additional_item);
    this.activity = activity;
    this.originalData = listArray ;
    font = Typeface.createFromAsset(activity.getAssets(),"HelveticaNeueThn.ttf");
    boldFont = Typeface.createFromAsset(activity.getAssets(), "avgardm.ttf");
}

public static class Row
{
    public TextView labelName;
    public TextView labelPrice;
    public CheckBox check;
}

@Override
public int getCount() {
    return originalData.size();
}

@Override
public ModifierEntity getItem(int position) {
    return originalData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

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


    final int colorFont = activity.getResources().getColor(R.color.brown_tataki);

    final Row holder;
    View rowView = convertView;
    // reuse views
    if (convertView == null) {
        holder = new Row();
        LayoutInflater inflater = LayoutInflater.from(activity);
        rowView = inflater.inflate(R.layout.additional_item, null);
        rowView.setClickable(true);
        //rowView.setFocusable(false);
        // configure view holder


        holder.labelName = (TextView) rowView.findViewById(R.id.additional_name);
        holder.labelPrice = (TextView) rowView.findViewById(R.id.additional_price);
        holder.check = (CheckBox) rowView.findViewById(R.id.additional_check);
        rowView.setTag(holder);

    }
    else {
        holder = (Row) convertView.getTag();
       // rowView.setClickable(true);
    }
    final ModifierEntity itm = originalData.get(position);
    holder.labelName.setText(itm.getModifier_name());
    holder.labelName.setTypeface(font);
    holder.labelName.setTextColor(colorFont);

    holder.labelPrice.setText(GlobalParameters.CURRENCY.concat(String.valueOf(itm.getModifier_cost())));
    holder.labelPrice.setTypeface(boldFont);
    holder.labelPrice.setTextColor(colorFont);
    return rowView;
}

}

这里是我的对话

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:background="@color/white">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/orange_tataki"
    android:text="@string/additional_title"
    android:textColor="@color/white"
    android:textSize="20sp"
    android:gravity="center"
    android:id="@+id/title_additional"
    android:padding="5dp"/>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/title_additional"
    android:gravity="center"
    android:background="@color/white"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list_additional"
        android:divider="@color/brown_tataki"
        android:dividerHeight="0.5dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/list_additional"
        android:gravity="center"
        android:padding="10dp"
        android:visibility="gone"
        android:textColor="@color/brown_tataki"
        android:id="@+id/additional_not_found"
        android:text="@string/additional_not_found"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/buttons"
        android:layout_marginBottom="10dp"
        android:layout_below="@+id/additional_not_found"
        android:gravity="center"
        android:layout_marginTop="20dp">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:id="@+id/button_cancel"
            android:background="@color/green_tataki"
            android:text="@string/button_cancel"
            android:textSize="15sp"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button_accept"
            android:padding="10dp"
            android:background="@color/green_tataki"
            android:text="@string/button_accept"
            android:textSize="15sp"
            android:layout_marginLeft="15dp"
            />

    </LinearLayout>
</RelativeLayout>

这是我的item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
>

<TextView
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:text="QUESO"
    android:singleLine="true"
    android:padding="10dp"
    android:textColor="@color/brown_tataki"
    android:id="@+id/additional_name"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/additional_price"
    android:padding="10dp"
    android:text="Bs. 500"
    android:textColor="@color/brown_tataki"
    android:layout_toRightOf="@id/additional_name"
    android:layout_marginLeft="10dp"
    />

<CheckBox
    android:id="@+id/additional_check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="8dp" />

1 个答案:

答案 0 :(得分:1)

使用接口我可以解决我的问题,这个link为我提供了解决方案,并且在我的活动中创建了一个接口和实现。

像这样:

1.-界面

public interface MyListener {
    void folderClicked();
}

2.-在活动中实现界面

public class ActivityA extends Activity implements MyListener 

3.-你必须自动覆盖方法文件夹点击它将如下所示:

@Override
protected void folderClicked() {
// Do your stuff here.
}

4.-使用如下构造函数将活动侦听器发送到适配器:

MyAdpater adapter = new MyAdpater(ActivityA.this);

5.-你的适配器类你的代码应该是这样的:

public class TimeLineAdapter extends BaseAdapter {
private MyListener mListener;
public TimeLineAdapter(MyListener listener) {
    super();
    mListener = listener;
}

holder.iconImage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
     mListener.onFolderClicked()
  //code to do stuff when the image is clicked
}