我的资源中有一个数组,我希望在ListView
中显示,但我失败了,所以也许有人能说出我做错了什么,我该怎么做才能做到这一点。这是我使用的代码:
在我的XML资源中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">myApp</string>
<string-array name="EstadosArray">
<item >Estado1</item>
<item >Estado2</item>
<item >Estado3</item>
<item >Estado4</item>
<item >Estado5</item>
<item >Estado6</item>
</string-array>
</resources>
在我的活动中:
public class myMain extends Activity{
String[] Estados;
eAdapter ea=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Estados = getResources().getStringArray(R.array.EstadosArray);
ea = new eAdapter();
ListView EstadosList = (ListView) findViewById(R.id.estadosList);
EstadosList.setAdapter(ea);
}
class eAdapter extends ArrayAdapter<String> {
eAdapter() {
super(myMain.this, android.R.layout.simple_list_item_1, Estados);
}
public View getView(int position, View convertView, ViewGroup parent) {
eHolder holder;
if (convertView == null) {
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.esatdoitem, null);
holder = new eHolder(convertView);
convertView.setTag(holder);
} else {
holder = (eHolder) convertView.getTag();
}
holder.populateFrom(Estados.get(position)); // HERE IS WHERE I HAVE THE issue is, I DON'T KNOW HOW TO GET THE POSITION, IS GIVING ME AN ERROR (CANNOT INVOKE GET(INT) ON THE ARRAY TYPE STRING[])
return (convertView);
}
}
class eHolder{
public TextView esname=null;
eHolder(View row) {
esname = (TextView) row.findViewById(R.id.ename);
}
void populateFrom(String est) {
esname.setText(est);
}
}
我知道问题是因为string []不是int但我该如何解决?这就是我填写列表中每一行的方式。
修改:LogCat
已添加:
05-03 17:09:46.290: E/AndroidRuntime(271): at com.zvzej.jlgproapps.mexicoguia.myMain$eHolder.populateFrom(myMain.java:468)
05-03 17:09:46.290: E/AndroidRuntime(271): at com.zvzej.jlgproapps.mexicoguia.myMain$eAdapter.getView(myMain.java:456)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.widget.ListView.onMeasure(ListView.java:1109)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.view.View.measure(View.java:8171)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:563)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:378)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.view.View.measure(View.java:8171)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.view.View.measure(View.java:8171)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.view.View.measure(View.java:8171)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.view.View.measure(View.java:8171)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.view.View.measure(View.java:8171)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.os.Looper.loop(Looper.java:123)
05-03 17:09:46.290: E/AndroidRuntime(271): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-03 17:09:46.290: E/AndroidRuntime(271): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 17:09:46.290: E/AndroidRuntime(271): at java.lang.reflect.Method.invoke(Method.java:521)
05-03 17:09:46.290: E/AndroidRuntime(271): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-03 17:09:46.290: E/AndroidRuntime(271): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-03 17:09:46.290: E/AndroidRuntime(271): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
你试过了吗?
holder.populateFrom(Estados[position]);
<强>加成强>
这是:
esname = (TextView) row.findViewById(R.id.ename);
原定于:
esname = (TextView) row.findViewById(R.id.esname);