SverkerSbrg
SverkerSbrg

Reputation: 503

onItemClickListener gives no event in AlertDialog, works fine in DialogFragment

I've build a DialogFragment for showing a custom ListView and then picking an entry, which is working fine. Except that i'm unable to remove the title bar. I can remove the title with setStyle(....), but the white field persists. I read on http://developer.android.com/guide/topics/ui/dialogs.html and got to the conclusion that using an AlertDialog would be a solution. I converted my class and actually got it working but i get no event from the OnItemClickListener...

Here is the working DialogFragment:

package com.SverkerSbrg.Spendo.Transaction.InsertTransaction.CategoryPicker;

import com.SverkerSbrg.Spendo.R;
import com.SverkerSbrg.Spendo.Category.CategoryList.CategoryList;
import com.SverkerSbrg.Spendo.Category.CategoryList.CategoryListChild;
import com.SverkerSbrg.Spendo.Category.Datastructure.Category;
import com.SverkerSbrg.Spendo.general.interfaces.CategoryReceiver;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class CategoryPickerFragment extends DialogFragment implements OnItemClickListener{
private CategoryReceiver receiver;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.category_picker_fragment, container);
    CategoryList categoryList = (CategoryList) view.findViewById(R.id.clCategories);
    categoryList.setOnItemClickListener(this);


    this.getDialog().setTitle(this.getActivity().getResources().getString(R.string.CategoryPicker_DialogHeader));

    return view;
}
public void setCategoryReceiver(CategoryReceiver receiver){
    this.receiver = receiver;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Category category = ((CategoryListChild)view).getCategory();
    receiver.setCategory(category);
    this.dismiss();
}

}

And here is the AlertDialog:

package com.SverkerSbrg.Spendo.Tester;

import com.SverkerSbrg.Spendo.R;
import com.SverkerSbrg.Spendo.Category.CategoryList.CategoryList;
import com.SverkerSbrg.Spendo.Category.CategoryList.CategoryListChild;
import com.SverkerSbrg.Spendo.Category.Datastructure.Category;
import com.SverkerSbrg.Spendo.general.interfaces.CategoryReceiver;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class AlertDialogTest extends DialogFragment implements OnItemClickListener{
private CategoryReceiver receiver;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    View view = inflater.inflate(R.layout.category_picker_fragment, null);

    builder.setView(inflater.inflate(R.layout.category_picker_fragment, null));
    AlertDialog ad = builder.create();

    CategoryList categoryList = (CategoryList) view.findViewById(R.id.clCategories);
    categoryList.setOnItemClickListener(this);

    return ad;
}


public void setCategoryReceiver(CategoryReceiver receiver){
    this.receiver = receiver;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    System.out.println("adfafefawefaewfae");
    Category category = ((CategoryListChild)view).getCategory();
    receiver.setCategory(category);
    this.dismiss();
}
}

And finally here is the how i call them:

//      FragmentActivity a = (FragmentActivity) this.getContext();
//      AlertDialogTest adt = new AlertDialogTest();
//      adt.show(a.getSupportFragmentManager(), "CategoryPicker");
//      adt.setCategoryReceiver(this);

    CategoryPickerFragment cpfFragment = new CategoryPickerFragment();
    FragmentActivity a = (FragmentActivity) this.getContext();
    cpfFragment.show(a.getSupportFragmentManager(), "CategoryPicker");
    cpfFragment.setCategoryReceiver(this);

Help would be greatly appreciated :)

Upvotes: 0

Views: 1283

Answers (1)

Pragnani
Pragnani

Reputation: 20155

You are using two different view,

i.e categoryList you have set setOnItemClickListener is different from View you are using for alert dialog. inflater.inflate(R.layout.category_picker_fragment, null); this will create a copy of view.

You have set

  builder.setView(inflater.inflate(R.layout.category_picker_fragment, null));

but you are getting the category list from another view inflatted here

 View view = inflater.inflate(R.layout.category_picker_fragment, null);

So two different view. Try to use like this

View view = inflater.inflate(R.layout.category_picker_fragment, null);
    builder.setView(view );

This will fix your problem

Upvotes: 2

Related Questions