1

I have an array in my resources that I want to display in a ListView but I'm failing miserably so maybe someone can tell what I'm doing wrong and what should I do to accomplish this. Here is the code I' using:

In my XML resource:

<?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>

In my Activity:

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);
    }
}

I know that the issue is because string[] is not int but how can I fix that? That's how I will fill every row of my list.

Edit: LogCat added:

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)
8
  • Hmm, is there a caused by line or two not included here in your logcat? Commented May 4, 2012 at 0:23
  • I included the complete logcat that eclipse show me. Commented May 4, 2012 at 0:32
  • when I hover over the log cat, where my package name is it gives me line 456 and line 467, thats where the populate call and form are. Commented May 4, 2012 at 0:36
  • plus this on top of the log cat 05-03 17:39:00.126: W/dalvikvm(320): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 05-03 17:39:00.146: E/AndroidRuntime(320): FATAL EXCEPTION: main 05-03 17:39:00.146: E/AndroidRuntime(320): java.lang.NullPointerException Commented May 4, 2012 at 0:40
  • Could you post your esatdoitem layout? Commented May 4, 2012 at 0:45

1 Answer 1

1

Have you tried:

holder.populateFrom(Estados[position]);

Addition

Is this:

    esname = (TextView) row.findViewById(R.id.ename);

Supposed to be:

    esname = (TextView) row.findViewById(R.id.esname);
Sign up to request clarification or add additional context in comments.

6 Comments

@ Sam, thanks for helping, now is not giving me the error in the main Activity, but when I run it it crashes right in that line.
What is the logcat? Please include relevant code from your eHolder class if necessary.
eholder is just like I posted in the original post and I just added the log cat
Ha, I didn't see it there! Do you have plans to make this more complex? Otherwise I could suggest an approach using built-in methods. Give me a moment on the errors.
No this list is just going to contain 32 items fix, because those states or estados will never change.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.