Отображение элементов списка в ListView - PullRequest
0 голосов
/ 16 октября 2010

У меня есть следующий main.xml:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
    <ListView
    android:id="@+id/listview"
    android:layout_width="315px"
    android:layout_height="379px"
    android:layout_x="2px"
    android:layout_y="50px"
    >
    </ListView>
    <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Select device:"
    android:textSize="17sp"
    android:layout_x="-2px"
    android:layout_y="1px"
    >
    </TextView>
    <Button
    android:id="@+id/refresh"
    android:layout_width="109px"
    android:layout_height="wrap_content"
    android:text="Refresh"
    android:layout_x="209px"
    android:layout_y="7px"
    >
    </Button>
</AbsoluteLayout>

Основной класс ничего не делает, кроме рисования макета.

У меня есть следующий класс и макет для элементов списка:

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

package com.android.bluetoothp2p;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class BTListView extends ListActivity {
    @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();
        }
      });
    }

Страны - это просто массив со списком стран, с некоторыми фиктивными значениями.

Теперь, как мне сделать так, чтобы элементы списка класса BTListView помещались в ListView (с @ + id / listview) основного класса?

РЕДАКТИРОВАТЬ: Теперь мой список работает, но он больше не кликабелен. Это код:

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(), "lolwat", Toast.LENGTH_SHORT).show();
        }
      });

1 Ответ

2 голосов
/ 16 октября 2010

Вы должны указать идентификатор @android: id / list для вашего ListView в макете - подробности см. http://developer.android.com/reference/android/app/ListActivity.html.Этот идентификатор используется классом ListActivity для поиска экземпляра ListView.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...