0

我尝试从 ListView 中获取选定的项目,但我没有从我的听众那里得到任何回应

我的日志中没有收到来自此代码的任何呼叫:

public void onItemSelected(AdapterView<?> spinnerReference,
        View rowReference, int arg2, long arg3) {

        chooseExercise = itemsList.getSelectedItem().toString();
        Log.i("choosed", chooseExercise);
}

我的适配器:

public void setItemList(String muscle)
{

if (muscle.equals("All"))
    items = data.getAllExercies();
else
    items = data.getMuscleEx(muscle);

ArrayAdapter<CharSequence> exerciseAdapter;

if (items == null || items.length < 1)
    items = new String[] {};

exerciseAdapter = new ArrayAdapter<CharSequence>(this,
        android.R.layout.simple_spinner_item, items)
{

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

        String text = items[position];

        TextView listItem = new TextView(AddWorkOutPage.this);
        listItem.setBackground(getResources().getDrawable(R.drawable.shape_box));
        listItem.setTextSize(18);
        listItem.setText(text);
        listItem.setPadding(10, 20, 0, 20);;
        listItem.setTextColor(Color.BLACK);

        return listItem;
    }
};

// Specify the layout to use when the list of choices appears

// Apply the adapter to the spinner
searchItemField.setAdapter(exerciseAdapter);
itemsList.setAdapter(exerciseAdapter);

}

我的列表视图对象代码:

public void addExerciseDialog()
     {       
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Choose exercise");

            View prefView = View.inflate(this, R.layout.choose_from_list, null);    

            searchItemField = (AutoCompleteTextView) prefView.findViewById(R.id.searchItemField);
            itemsList = (ListView) prefView.findViewById(R.id.itemsList);

            searchItemField.setDropDownHeight(0);

            searchItemField.addTextChangedListener(this);

            setItemList(wantedMuscle);

            itemsList.setOnItemClickListener(this);

            builder.setPositiveButton("Choose selected", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) 
                {
                    Log.i("exercise choose", chooseExercise);
                    dialog.dismiss();
                }
            });

            builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) 
                {
                    dialog.dismiss();
                }
            });

            builder.setNeutralButton("filter", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) 
                {
                    createDialog();
                }
            });

            builder.setView(prefView);

            builder.show();
     }

我的对话框 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/White"
    android:descendantFocusability="beforeDescendants"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@android:drawable/ic_menu_search" />

    <ListView
        android:id="@+id/itemsList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/searchItemField"
        android:descendantFocusability="beforeDescendants"
        android:layout_marginTop="5sp" >

    </ListView>

    <AutoCompleteTextView
        android:id="@+id/searchItemField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"       
        android:layout_toLeftOf="@+id/imageView1"
        android:ems="10" >

        <requestFocus />
    </AutoCompleteTextView>

</RelativeLayout>
4

1 回答 1

1

你到底在使用什么监听器?OnItemClickListener有一个方法onItemClick ()而不是您的 onItemSelected()。我不确定你的是否也可以,但这有效:

ListView listview = (ListView) v.findViewById(R.id.itemsList);
listview.setAdapter(...);
listview.setOnItemClickListener(new MyOnItemClickListener());

并将 OnItemClickListener 作为内部类:

private class MyOnItemClickListener implements OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        //Object o = parent.getItemAtPosition(position);

    }
}
于 2013-08-17T20:57:03.163 回答