在LinearLayouts中使用ListViews而不是全屏

时间:2011-08-27 15:50:33

标签: android listview simplecursoradapter

我是Android的新手,我希望添加主题按钮只显示一次而不是每个项目。我如何使用SimpleCursorAdapter

执行此操作

此致 Waneya Iqbal I want the add subject button to be displayed only once and not on each item

代码如下(参见showSubjectOnList()方法):

公共类PopulatingSubject扩展了ListActivity {

public static String subjectName; 私人SoftCopyDatabase主题;

private static int[] subTO = { R.id.subject };
private static String[] subFROM = { SUBJECT };


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    subjects = new SoftCopyDatabase(this);

}

@Override
public void onStart() {
    super.onStart();

    try {

        showSubjectsOnList();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public void onStop() {
    super.onStop();
    if (subjects.getReadableDatabase().isOpen()) {
        subjects.close();
    }
}

public void onDestroy(){
    super.onDestroy();
    if (subjects.getReadableDatabase().isOpen()) {
        subjects.close();
    }
}

private void showSubjectsOnList() {

    String sql = "SELECT " + _ID + "," + SUBJECT + " FROM " + TABLE_NAME
            + " GROUP BY " + SUBJECT + ";";
    Cursor cursor = subjects.getReadableDatabase().rawQuery(sql, null);
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.item_subject, cursor, subFROM, subTO);
    setListAdapter(adapter);
    startManagingCursor(cursor);

}
@Override
protected void onListItemClick(ListView listView, View view, int position,
        long id) {
    super.onListItemClick(listView, view, position, id);
    subjectName=getSubjectName(Long.toString(id));
    startActivity(new Intent(this, PopulatingLectures.class));
}



private String getSubjectName(String ID) {

    Cursor cursor = subjects.getReadableDatabase().query(TABLE_NAME,
            new String[] { SUBJECT }, "_ID=?", new String[] { ID }, null,
            null, null);
    cursor.moveToFirst();
    int subjectIndex = cursor.getColumnIndex(SUBJECT);
    String subjectName = cursor.getString(subjectIndex);
    cursor.close();
    return subjectName;
}   

}

布局如下:

                                     

布局图为:

enter image description here

1 个答案:

答案 0 :(得分:0)

您必须自己创建一个布局,无论是代码还是XML(建议使用XML)。 然后在该布局中创建一个ListView。您可以创建按钮并将其添加到同一布局。由于您正在创建布局,因此您可以以任何适合您需要的方式定位ListView和Button。

然后创建对ListView的引用,以便为其设置适配器。

示例:

名为“my_layout.xml”的XML文件中的布局:

<LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Sign In" />

   <ListView
       android:id="@+id/my_listview"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" />

</LinearLayout>

然后在代码中:

public class MyActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);
        ListView listView = (ListView) findViewById(R.id.my_listview);
        listView.setAdapter(/*Put your adapter reference here*/);
        listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Handle list selections here
        }
    });
    }
}

显然没有数据,但你可以这样做但是将你的适配器设置在我上面指出的地方。