Marlaurita
Marlaurita

Reputation: 585

Android - SetOnItemClickListener doesn't work

I know here we have a lot of questions like mine, but I don't know why none works for me.

My objective: I have an AlertDialog with a ListView with a check box in each row, I can select some of the items, and I wish to make an ArrayList with the elements selected.

For that reason, I'm calling the SetOnclickListener, I put a Log inside the method, but does nothing.

I tried with focusable and clickable almost everywhere, but my Log doesn't appear.

Here My alert Dialog

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();
}

Here my adapter:

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;
}

}

Here My Dialog

<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>

And Here my 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" />

Upvotes: 1

Views: 338

Answers (1)

Marlaurita
Marlaurita

Reputation: 585

Using interfaces I can solve my problem, this link provided me the solution, and bassically consist in create an interface and implement in my activity.

Like this:

1.- Interface

public interface MyListener {
    void folderClicked();
}

2.- Implements the interface in the activity

public class ActivityA extends Activity implements MyListener 

3.- You will have to auto override the method folderClicked it will look like this:

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

4.- Send the activity listener to the adapter with constructor like this:

MyAdpater adapter = new MyAdpater(ActivityA.this);

5.- Your adapter class your code should be like this:

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
}

Upvotes: 1

Related Questions