ListView не заполнен - PullRequest
       7

ListView не заполнен

0 голосов
/ 29 апреля 2011

Я создал Адаптер для заполнения моего пользовательского списка, и при запуске на эмуляторе действие остается пустым. Пожалуйста, помогите. Я уверен, что что-то упустил, потому что я новичок в Java и Android. Некоторые фрагменты кода для исправления и указатели будут оценены. Thnx!

Моя активность:

public class List_AC3 extends ListActivity {

/**
 * -- Called when the activity is first created
 * ===================================================================
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.list_view2);

    displayResultList();
}

private void displayResultList() {
    Cursor databaseCursor = null;
    DomainAdapter databaseListAdapter = new DomainAdapter(this, R.layout.list_item, databaseCursor, 
            new String[] {"label", "title", "description"}, 
            new int[] { R.id.label, R.id.listTitle, R.id.caption });
            databaseListAdapter.notifyDataSetChanged();
            setListAdapter(databaseListAdapter);
}
}

Мой адаптер:

public class DomainAdapter extends SimpleCursorAdapter{

private LayoutInflater mInflater;
String extStorageDirectory;

public DomainAdapter(Context context, int layout, Cursor c, String[] from,
        int[] to) {
    super(context, layout, c, from, to);
        mInflater = LayoutInflater.from(context);
}

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);

        holder = new ViewHolder();
        holder.text1 = (TextView) convertView.findViewById(R.id.label);
        holder.text2 = (TextView) convertView.findViewById(R.id.listTitle);
        holder.text3 = (TextView) convertView.findViewById(R.id.caption);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File dbfile = new File(extStorageDirectory+ "/Aero-Technologies/flyDroid/dB/flyDroid.db");
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
    Cursor data = db.rawQuery("SELECT * FROM AC_list", null);

    data.moveToPosition(position);

    int label_index = data.getColumnIndex("label"); 
    String label = data.getString(label_index);

    int title_index = data.getColumnIndex("title"); 
    String title = data.getString(title_index);

    int description_index = data.getColumnIndex("description"); 
    String description = data.getString(description_index);

    holder.text1.setText(label);
    holder.text2.setText(title);
    holder.text3.setText(description);

    return convertView;
    }

    static class ViewHolder {
        TextView text1;
        TextView text2;
        TextView text3;
    }  
}

The list_view2.xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" 
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="30dip" 
    android:padding="4dip"
    android:background="@drawable/gradient" >

    <ImageButton
        android:id="@+id/homeBtn"
        android:src="@drawable/ic_menu_icon"
        android:layout_width="wrap_content" 
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:background="@null" />

    <TextView
        android:id="@+id/titleBarTitle"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content" 
        android:layout_height="match_parent"
        android:textSize="18sp" />

    <ImageButton
        android:id="@+id/toolBtn"
        android:src="@drawable/ic_menu_list"
        android:layout_width="wrap_content" 
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:background="@null" />

</RelativeLayout>

<ListView 
   android:id="@id/android:list" 
   android:layout_height="wrap_content"
   android:layout_width="fill_parent" />

</LinearLayout>

И мой list_item.xml:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/acItem"
style="@style/listItem" >

<TextView
    android:id="@+id/label"
    style="@style/listAcronym" />

<TextView
    android:id="@+id/listTitle"
    style="@style/listTitle" />

<TextView
    android:id="@+id/caption"
    style="@style/listDiscription"/>        

<ImageView
    style="@style/listNextIcon" />   

</RelativeLayout>

Ответы [ 3 ]

1 голос
/ 29 апреля 2011
0 голосов
/ 30 апреля 2011

Правильный ответ можно найти ЗДЕСЬ вместе с кодом.

0 голосов
/ 29 апреля 2011

SimpleCursorAdapter не требует расширения для работы. Возьмите свою логику БД из getView и используйте ее для создания курсора, который фактически указывает на результат БД. Затем передайте этот курсор в конструктор SimpleCursorAdapter. На самом деле, я не думаю, что getView фактически вызывается где-либо.

Попробуйте сначала заставить этот пример работать http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/, а затем отредактируйте его, чтобы сделать то, что вам нужно.

Если вы хотите сделать что-то более сложное (например, настроить различные текстовые представления самостоятельно, как вы пытаетесь сделать в getView), посмотрите CursorAdapter.

...