Creating A List Application
I'm tr开发者_运维知识库ying to create a simple list application for my first app. I have a hello world application running successfully on my droidx2 but when I change the code for a list it gives me a bunch of errors in the code.
The XML:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
The .java file
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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();
}
});
}
This code might look familiar to you guys if you looked at a list example on the android development pages.
Do I need to import certain packages to get this to work?
EDIT: After installing the packages it fixed a lot of errors. I'm stilling getting an error here: setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
It doesn't like list_item. I fexed the COUNTRIES problem.
EDIT 2: I figured out the problem. I never created the list_item.xml. Once that is created then the error is gone.
I figured out the problem. I never created the list_item.xml. Once that is created then the error is gone.
精彩评论