0

我已经构建了一个 DialogFragment 用于显示自定义 ListView 然后选择一个条目,它工作正常。除了我无法删除标题栏。我可以使用 setStyle(....) 删除标题,但白色字段仍然存在。我在http://developer.android.com/guide/topics/ui/dialogs.html上阅读并得出结论,使用 AlertDialog 将是一个解决方案。我转换了我的课程,实际上让它工作了,但我没有从 OnItemClickListener 得到任何事件......

这是工作的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();
}

}

这是警报对话框:

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

最后是我如何称呼它们:

//      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);

帮助将不胜感激:)

4

1 回答 1

2

您正在使用两种不同的视图,

categoryList您设置setOnItemClickListener的视图与您用于警报对话框的视图不同。 inflater.inflate(R.layout.category_picker_fragment, null);这将创建视图的副本。

你已经设置

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

但是您正在从此处夸大的另一个视图中获取类别列表

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

所以两种不同的看法。尝试像这样使用

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

这将解决您的问题

于 2013-04-21T15:06:48.583 回答