我在尝试创建ImageAdapter时遇到了一些问题。说实话,我不确定我是最好的方式。我的/assets
文件夹中有20张图片。我有一个String
格式的文件名数组。我只是想在我的一个对话框条目中创建一个下拉列表(Spinner
),供人们选择其中一个图像(不是按名称,而是按实际图像)。我提供了与我在下面尝试做的相关的所有代码。任何帮助将不胜感激。稍后我将需要知道为不同的ListAdapter
选择了哪个图像我必须显示数据。
编辑:修正了第一个错误。使用错误的findViewById。当我尝试按下拉下来时它现在崩溃了。我对ArrayAdapter
LogCat Below
package cs.ucsb.cs185.dimberman.lifeRPG;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
public class ImageAdapter extends ArrayAdapter<String> {
int layoutResourceId;
public static final String CONTENT_URI = "content://cs.ucsb.cs185.dimberman.lifeRPG.assets.";
public ImageAdapter(Context context, int layoutResourceId, String[] icons) {
super(context, layoutResourceId, icons);
this.layoutResourceId = layoutResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ItemHolder holder = null;
if(row == null) {
LayoutInflater inflater = ((Activity)parent.getContext()).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ItemHolder();
holder.image = (ImageView)row.findViewById(R.id.list_image);
row.setTag(holder);
} else {
holder = (ItemHolder)row.getTag();
}
final String s = getItem(position);
holder.image.setImageURI(Uri.parse(CONTENT_URI + s));
return row;
}
static class ItemHolder{
ImageView image;
}
}
Dialog Builder
private String [] icons = {
"airplane.png", "bang.png", "drive_copy.png", "email.png", "heart.png", "kingOfWorld.png",
"leafy_outdoors.png", "miscellaneous_blob.png", "music.png", "outdoors.png", "paperclip.png",
"pencil.png", "person.png", "pet.png", "radioactive.png", "shop.png", "star.png", "talk.png",
"telephone.png", "thumbtack.png"
};
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(R.string.add_activity);
final View v2 = this.getLayoutInflater().inflate(R.layout.add_button_layout, null);
ImageAdapter adapter2 = new ImageAdapter(this, R.layout.image_drop_down_view, icons);
Spinner imageList = (Spinner) findViewById(R.id.addActivityImageText);
imageList.setAdapter(adapter2);
// ...
简单图像项的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:contentDescription=""
/>
</LinearLayout>
用于Alert Builder的XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="6dp" >
<LinearLayout
android:id="@+id/row0ForDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="16">
<TextView
android:id="@+id/addActivityImageLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingLeft="8dp"
android:text="@string/add_activity_image"
android:gravity="left"
android:layout_weight="5"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id ="@+id/addActivityImageText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingRight="8dp"
android:layout_weight="11"
android:inputType="text" />
</LinearLayout>
</LinearLayout>
LogCat崩溃
06-05 17:21:27.346: E/AndroidRuntime(1843): FATAL EXCEPTION: main
06-05 17:21:27.346: E/AndroidRuntime(1843): java.lang.IllegalStateException: Could not execute method of the activity
06-05 17:21:27.346: E/AndroidRuntime(1843): at android.view.View$1.onClick(View.java:3597)
06-05 17:21:27.346: E/AndroidRuntime(1843): at android.view.View.performClick(View.java:4202)
06-05 17:21:27.346: E/AndroidRuntime(1843): at android.view.View$PerformClick.run(View.java:17340)
06-05 17:21:27.346: E/AndroidRuntime(1843): at android.os.Handler.handleCallback(Handler.java:725)
06-05 17:21:27.346: E/AndroidRuntime(1843): at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 17:21:27.346: E/AndroidRuntime(1843): at android.os.Looper.loop(Looper.java:137)
06-05 17:21:27.346: E/AndroidRuntime(1843): at android.app.ActivityThread.main(ActivityThread.java:5039)
06-05 17:21:27.346: E/AndroidRuntime(1843): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 17:21:27.346: E/AndroidRuntime(1843): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 17:21:27.346: E/AndroidRuntime(1843): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-05 17:21:27.346: E/AndroidRuntime(1843): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-05 17:21:27.346: E/AndroidRuntime(1843): at dalvik.system.NativeStart.main(Native Method)
06-05 17:21:27.346: E/AndroidRuntime(1843): Caused by: java.lang.reflect.InvocationTargetException
06-05 17:21:27.346: E/AndroidRuntime(1843): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 17:21:27.346: E/AndroidRuntime(1843): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 17:21:27.346: E/AndroidRuntime(1843): at android.view.View$1.onClick(View.java:3592)
06-05 17:21:27.346: E/AndroidRuntime(1843): ... 11 more
06-05 17:21:27.346: E/AndroidRuntime(1843): Caused by: java.lang.NullPointerException
06-05 17:21:27.346: E/AndroidRuntime(1843): at cs.ucsb.cs185.dimberman.lifeRPG.LifeRpgMain.listAdd(LifeRpgMain.java:159)
06-05 17:21:27.346: E/AndroidRuntime(1843): ... 14 more
06-05 17:26:27.660: E/Trace(1875): error opening trace file: No such file or directory (2)
编辑:LogCat第二个错误
06-05 17:35:35.719: E/AndroidRuntime(1949): FATAL EXCEPTION: main
06-05 17:35:35.719: E/AndroidRuntime(1949): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
06-05 17:35:35.719: E/AndroidRuntime(1949): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
06-05 17:35:35.719: E/AndroidRuntime(1949): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:415)
06-05 17:35:35.719: E/AndroidRuntime(1949): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:737)
06-05 17:35:35.719: E/AndroidRuntime(1949): at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:733)
06-05 17:35:35.719: E/AndroidRuntime(1949): at android.widget.Spinner.measureContentWidth(Spinner.java:681)
06-05 17:35:35.719: E/AndroidRuntime(1949): at android.widget.Spinner$DropdownPopup.show(Spinner.java:959)
06-05 17:35:35.719: E/AndroidRuntime(1949): at android.widget.Spinner.performClick(Spinner.java:609)
06-05 17:35:35.719: E/AndroidRuntime(1949): at android.view.View$PerformClick.run(View.java:17340)
06-05 17:35:35.719: E/AndroidRuntime(1949): at android.os.Handler.handleCallback(Handler.java:725)
06-05 17:35:35.719: E/AndroidRuntime(1949): at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 17:35:35.719: E/AndroidRuntime(1949): at android.os.Looper.loop(Looper.java:137)
06-05 17:35:35.719: E/AndroidRuntime(1949): at android.app.ActivityThread.main(ActivityThread.java:5039)
06-05 17:35:35.719: E/AndroidRuntime(1949): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 17:35:35.719: E/AndroidRuntime(1949): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 17:35:35.719: E/AndroidRuntime(1949): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-05 17:35:35.719: E/AndroidRuntime(1949): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-05 17:35:35.719: E/AndroidRuntime(1949): at dalvik.system.NativeStart.main(Native Method)
06-05 17:35:35.719: E/AndroidRuntime(1949): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
06-05 17:35:35.719: E/AndroidRuntime(1949): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
06-05 17:35:35.719: E/AndroidRuntime(1949): ... 16 more
答案 0 :(得分:0)
如果Spinner位于DialogBox
布局内(在add_button_layout
中),则将其初始化为:
Spinner imageList = (Spinner)v2. findViewById(R.id.addActivityImageText);