ListView没有填充

时间:2011-04-28 23:40:20

标签: android listview adapter

我创建了一个适配器来填充我的自定义listView,当在模拟器上运行时,活动为空。 Plz的帮助。我确信我错过了一些东西,因为我不熟悉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;
    }  
}

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 个答案:

答案 0 :(得分:1)

google notepad tutorials也应该帮助你IIRC他们应该使用传递给列表视图的游标

答案 1 :(得分:0)

SimpleCursorAdapter不需要扩展即可工作。将您的db逻辑从getView中取出并使用它构造一个实际指向db结果的游标。然后将该游标传递给SimpleCursorAdapter构造函数。事实上,我认为getView实际上并没有被调用。

尝试首先使用此示例http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/,然后对其进行编辑以执行您需要的操作。

如果你想做一些更复杂的事情(比如你自己设置各种文本视图,就像你在getView中所做的那样),请查看CursorAdapter。

答案 2 :(得分:0)

可以找到正确的答案HERE以及代码。