开发者

Changing Java ListView to XML

Alright, so I followed a tutorial on the Android website, and I g开发者_运维百科ot a ListView going in my application. But, the example they had did everything in Java basically. How could I transform the following code to XML?

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));

ListView lv = getListView();
lv.setTextFilterEnabled(true);

lv.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // When clicked, show a toast with the TextView text
    Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
  }
});

I know I can't make the click listener XML, I just put that there for example.


You don't really need to change anything to XML in that particular piece of code. ListActivity will use a full-screen ListView if there is no call to setContentView(). Here is an example of a ListActivity with a custom layout, if that is what you are interested in.

Also, please get rid of getApplicationContext(). Just use this to reference the Activity, which itself is a Context.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜