У меня есть ListActivity в Android Project. Его код:
public class ListActivityApp extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
map = new HashMap<String, String>();
map.put("title", "my note");
map.put("img", String.valueOf(R.drawable.paper));
listItem.add(map);
map = new HashMap<String, String>();
map.put("title", "second");
map.put("img", String.valueOf(R.drawable.paper));
listItem.add(map);
map = new HashMap<String, String>();
map.put("title", "cos tam");
map.put("img", String.valueOf(R.drawable.paper));
listItem.add(map);
SimpleAdapter myAdapter = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.list,
new String[] {"img", "title"}, new int[] {R.id.img, R.id.title});
setListAdapter(myAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String selection = getListAdapter().getItem(position).toString();
Toast.makeText(this, selection, 0).show();
}}
Мой list.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Empty"
/>
И мой item.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
</LinearLayout>
Первая проблема (разрешенная), которая у меня возникла: в эмуляторе я вижу три TextView с текстом «Пусто», но когда я щелкаю по каждому из них, я вижу Toast с правильным «заголовком». Что я делаю не так? В другом проекте это работает правильно. - Я решил это. В этом:
SimpleAdapter myAdapter = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.list, new String[] {"img", "title"}, new int[] {R.id.img, R.id.title});
Мне следует в качестве третьего аргумента использовать R.layout.item, а не R.layout.list.
Вторая проблема: как я могу получить Объект моего элемента списка в методе onListItemClick (..)? У меня есть, конечно, должность и идентификатор.
l.getItemAtPosition (положение)
getListAdapter (). GetItem (положение)
Эти методы возвращают все элементы (в моем случае img и title). Как я могу, например, изменить текст в TextView или сделать мой ImageView невидимым в onListItemClick (..)?