0

I'm having trouble with the setListAdapter(). It tells me to just create the method because it doesn't know anything about it. I'm, for now, just trying to get a list to populate and I don't even know what this code is doing.

public class PassScreen extends Activity {
TextView selection;
ArrayList<String> items = new ArrayList<String>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.passwordscreen);
    selection=(TextView)findViewById(R.id.selection);

    try { 
        InputStream in=getResources().openRawResource(R.raw.words);
        DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(in,null);
        NodeList words=doc.getElementsByTagName("word");
        for (int i =0;i<words.getLength();i++){
            items.add(((Element)words.item(i)).getAttribute("value"));
        }
        in.close();
    }
    catch (Throwable t){
        Toast.makeText(this, "Exception: " + t.toString(), 2000).show();
    }

            setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            items));

}
public void onListItemClick(ListView parent, View v, int position, long id){
    selection.setText(items.get(position).toString());
}
}

As you can see I have and xml file that I'm using. That looks just like what the book looks like but then again I copy and pasted the setListAdapter() so I guess that's not all that helpful.

If you could also show me what the setListAdapter() is doing that would be great. I can't seen to understand what google is talking about.

Here is the xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

<ListView
  android:id="@android:id/list"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:drawSelectorOnTop="false"/>

</LinearLayout>

And some of the words xml:

<words>
<word value="lorem"/>
<word value="ipsom"/>
<word value="dolor"/>
</words>

Also can you explain what's going on? I don't understand the setListAdapter() at all. Or just point to Googles white paper. I can't find it. It's not like I know how to look that stuff up anyway.

2
  • Well I figured out what was wrong with my code. I just needed to extend ListActivity. However their is still no list. I'll have to keep searching on this but if you can see something I can't and correct me that would be great. That only took me an hour to find out. And right after posted.
    – Funlamb
    May 26, 2011 at 6:40
  • Found the problem. I needed to add an orientation to my layout. ARGH!
    – Funlamb
    May 30, 2011 at 2:14

3 Answers 3

3

setListAdapter() isn't a method you can call on an Activity (it is available on ListActivity but you're not using that). You're going to have to add a ListView into your layout (/res/layout/passwordscreen) and then find it and call setAdapter on that.

e.g.

ListView lv = (ListView) findViewById(R.id.mylistview);
lv.setAdapter(.....);
5
  • Yeah I actually got that right after I posted. I looked for 1 hour yesterday trying to figure it out. Then I was trying for almost 30 minutes today. So I just posted it. I fixed that but I don't have a list that shows up. I don't get errors so I can run the program but I don't get a list. Thanks for your fast reply.
    – Funlamb
    May 26, 2011 at 6:51
  • Congratulations one your one millionth download i just cheeked out your blag.
    – Funlamb
    May 26, 2011 at 6:57
  • Thanks, BatteryWidget is now 4.2million downloads, that blog post was a while back. Also, if you want to paste some of the layout xml here I'll check it out, maybe your listview doesn't have any space on the screen. Can you tick my previous response as well? thanks.
    – GeekYouUp
    May 26, 2011 at 7:02
  • Updated. I can't seem to tick your post up. I need 15 reputation. How do I get some reputation?
    – Funlamb
    May 26, 2011 at 7:19
  • there is a tick sign near vote counter,tick it ..it'll become green and it shows that you've accepted this answer May 26, 2011 at 7:29
3

Hey That's because of base class, your base class should be:

public class yourClassName extends ListActivity{

Not class yourClassName extends Activity{

0

When you call setListAdapter this must extend ListActivity probably you class just extends Activity.

I saw it on some other post and it works for me!!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.