OnItemClickListener(更改Drawables)

时间:2014-03-09 02:27:58

标签: android android-button onitemclicklistener

我有一个具有自定义适配器/列表视图的警报对话框。当用户点击列表视图中的项目时,我希望能够检索他们的选择(可绘制),然后将其设置为用于生成alertdialog的按钮中的drawable。

在ListView中选择任何内容的那一刻,我一直在崩溃。我相信这是因为我在ArrayList rowItems的每个位置存储了多个项目。

如何在onItemClickListener中仅引用所选listview项的drawable?

我希望能够将drawable设置为父项活动中可绘制的按钮。

public class PicturePickerFragment extends DialogFragment {

ListView listView;
ArrayList<RowItem> rowItems = new ArrayList<RowItem>();
// attach adapter to a list view
// A String[] array that will hold the names of the items.
public static final String[] descriptions = { "Baby", "Baking",
        "Barbershop", "Camping", "Conference Call", "Funeral", "Gambling",
        "Gardening", "Halloweeen", "Medicine", "Meeting", "Poker",
        "Christmas", "Wedding" };
public static final Integer[] images = { R.drawable.baby,
        R.drawable.baking, R.drawable.barbershop, R.drawable.camping,
        R.drawable.conferencecall, R.drawable.funeral, R.drawable.gambling,
        R.drawable.gardening, R.drawable.halloween, R.drawable.medicine,
        R.drawable.meeting, R.drawable.poker, R.drawable.santa,
        R.drawable.wedding };

Button btn_pic;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View convertView = (View) inflater.inflate(R.layout.dialog_view, null);

    // defining the alertdialog
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(convertView);
    builder.setTitle(R.string.event_type);

    // builder.setPositiveButton(R.string.select_picture,
    // new DialogInterface.OnClickListener() {
    // @Override
    // public void onClick(DialogInterface dialog, int which) {
    // call the method on the parent activity when
    // user click the positive button

    // }
    // });

    // populating the array list
    for (int i = 0; i < descriptions.length; i++) {
        RowItem item = new RowItem(images[i], descriptions[i]);
        rowItems.add(item);
    }

    // defining listview and using array adapter
    listView = (ListView) convertView.findViewById(R.id.listViewFragment2);
    DrawableAdapter adapter = new DrawableAdapter(getActivity(), rowItems);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, final View view,
                int position, long id) {

            final Integer item = (Integer) parent
                    .getItemAtPosition(position);
        }

    });

    adapter.addAll(rowItems);

    return builder.create();
}

ADAPTER

public class DrawableAdapter extends ArrayAdapter<RowItem>{

public DrawableAdapter(Context context, List<RowItem> objects) {
    super(context, R.layout.dialog_detail);
    // TODO Auto-generated constructor stub
}

/* private view holder class */
private class ViewHolder {
    ImageView imageView;
    TextView txtDesc;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    RowItem item = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder viewHolder;
    if (convertView == null) {
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.dialog_detail, null);
        viewHolder.imageView = (ImageView) convertView
                .findViewById(R.id.imageView1);
        viewHolder.txtDesc = (TextView) convertView
                .findViewById(R.id.textView1);

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    // Populate the data into the template view using the data object
    viewHolder.txtDesc.setText(item.getDesc());
    viewHolder.imageView.setImageResource(item.getImageId());
    // Return the completed view to render on screen
    return convertView;


}

}

logcat的

03-09 23:27:17.285: E/AndroidRuntime(24674): FATAL EXCEPTION: main
03-09 23:27:17.285: E/AndroidRuntime(24674): java.lang.ClassCastException: com.example.datetracker.RowItem cannot be cast to java.lang.Integer
03-09 23:27:17.285: E/AndroidRuntime(24674):    at com.example.datetracker.PicturePickerFragment$1.onItemClick(PicturePickerFragment.java:74)
03-09 23:27:17.285: E/AndroidRuntime(24674):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-09 23:27:17.285: E/AndroidRuntime(24674):    at android.widget.AbsListView.performItemClick(AbsListView.java:1129)
03-09 23:27:17.285: E/AndroidRuntime(24674):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2782)
03-09 23:27:17.285: E/AndroidRuntime(24674):    at android.widget.AbsListView$1.run(AbsListView.java:3612)
03-09 23:27:17.285: E/AndroidRuntime(24674):    at android.os.Handler.handleCallback(Handler.java:725)
03-09 23:27:17.285: E/AndroidRuntime(24674):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-09 23:27:17.285: E/AndroidRuntime(24674):    at android.os.Looper.loop(Looper.java:137)
03-09 23:27:17.285: E/AndroidRuntime(24674):    at android.app.ActivityThread.main(ActivityThread.java:5168)
03-09 23:27:17.285: E/AndroidRuntime(24674):    at java.lang.reflect.Method.invokeNative(Native Method)
03-09 23:27:17.285: E/AndroidRuntime(24674):    at java.lang.reflect.Method.invoke(Method.java:511)
03-09 23:27:17.285: E/AndroidRuntime(24674):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
03-09 23:27:17.285: E/AndroidRuntime(24674):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
03-09 23:27:17.285: E/AndroidRuntime(24674):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

您可以直接从onClick

引用您的图像数组
@Override
public void onItemClick(AdapterView<?> parent, final View view,
        int position, long id) {
    final int item = images[position];
    btn_pic.setBackgroundResource(item);
}