Funlamb
Funlamb

Reputation: 625

setlistadapter problem I don't know how this works at all

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.

Upvotes: 0

Views: 4351

Answers (3)

GeekYouUp
GeekYouUp

Reputation: 1651

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(.....);

Upvotes: 3

user3879613
user3879613

Reputation: 1

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

Upvotes: 0

Shripad Bhat
Shripad Bhat

Reputation: 31

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

public class yourClassName extends ListActivity{

Not class yourClassName extends Activity{

Upvotes: 3

Related Questions