问题描述
我遇到的麻烦setListAdapter()。它告诉我刚刚创建的方法,因为它不知道这件事。我,现在,只是想获得一个列表来填充,我甚至不知道这是什么code在做什么。
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());
}
}
正如你可以看到我已经和我使用的XML文件。这看起来就像什么书的样子,但后来我又复制并粘贴setListAdapter(),所以我想这是不是所有的帮助。
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.
如果你也能告诉我是什么setListAdapter()做这将是巨大的。我也没见过了解什么谷歌正在谈论。
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.
下面是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>
和一些词的xml:
<words>
<word value="lorem"/>
<word value="ipsom"/>
<word value="dolor"/>
</words>
另外你能解释一下这是怎么回事?我根本不明白setListAdapter()。或者只是指向谷歌搜索了白皮书。我找不到它。这不是就像我知道怎么看的东西了呢。
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.
推荐答案
setListAdapter()
是不是你可以在一个活动调用一个方法
(它可在 ListActivity
但你没有使用)。你将需要添加一个的ListView
到您的布局(/ RES /布局/ passwordscreen),然后找到它并调用 setAdapter
这一点。
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.
例如
ListView lv = (ListView) findViewById(R.id.mylistview);
lv.setAdapter(.....);
这篇关于我不知道setlistadapter的问题是如何在所有工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!