我想创建一个包含图标和字符串的自定义微调器,但是从资源数组中获取值
问题是当我运行应用程序时它不会启动。但是当我在活动中声明数组时,这并没有发生
通过声明我的意思是:String[] vcode = {"d1","d2","d3","d4","d5"};
我已将这些变量移至oncreate
,但该图标未显示在我的应用程序中。为什么呢?
以下是我迄今为止所做的并且失败了。请告诉我哪里出错了
RES /值/ array.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="d_icon">
<item>@drawable/d1</item>
<item>@drawable/d2</item>
<item>@drawable/d3</item>
<item>@drawable/d4</item>
<item>@drawable/d5</item>
</integer-array>
<string-array name="d_code">
<item>d1</item>
<item>d2</item>
<item>d3</item>
<item>d4</item>
<item>d5</item>
</string-array>
<string-array name="d_condition">
<item>1</item>
<item>1</item>
<item>1</item>
<item>0</item>
<item>1</item>
</string-array>
</resources>
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Hello"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
custom_spinner_items.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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="@dimen/activity_horizontal_margin"
android:text="Demo"
android:textColor="#000" />
</LinearLayout>
MainActivity.java:
package com.training.androidcustomspinner;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Spinner;
public class MainActivity extends Activity {
//String[] vcode = {"d1","d2","d3","d4","d5"};
//int vicon[] = {R.drawable.d1, R.drawable.d2, R.drawable.d3, R.drawable.d4, R.drawable.d5};
String[] vcode = getResources().getStringArray(R.array.d_code);
int vicon[] = getResources().getIntArray(R.array.d_icon);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
CustomAdapter customAdapter=new CustomAdapter(getApplicationContext(),vicon,vcode);
mySpinner.setAdapter(customAdapter);
}
}
CustomAdapter.java
package com.training.androidcustomspinner;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
Context context;
int vicon[];
String[] vcode;
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] vicon, String[] vcode) {
this.context = applicationContext;
this.vicon = vicon;
this.vcode = vcode;
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return vicon.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_items, null);
ImageView icon = (ImageView) view.findViewById(R.id.imageView);
TextView names = (TextView) view.findViewById(R.id.textView);
icon.setImageResource(vicon[i]);
names.setText(vcode[i]);
return view;
}
}
答案 0 :(得分:0)
我的适配器扩展了ArrayAdapter并实现了getDropDownView,它适用于我。喜欢关注
cumsum