【问题标题】:Using ListViews in LinearLayouts instead of full Screen在 LinearLayouts 中使用 ListViews 而不是全屏
【发布时间】:2011-08-28 01:22:13
【问题描述】:

我是 android 新手,我希望添加主题按钮仅显示一次,而不是在每个项目上显示。我如何使用 SimpleCursorAdapter 来做到这一点

问候 瓦内亚·伊克巴尔

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

公共类 PopulatingSubject 扩展 ListActivity {

公共静态字符串主题名; 私有 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;
}   

}

布局如下:

布局图为:

【问题讨论】:

  • 你能举一个你想要完成的布局的例子吗?
  • 其实我想要实现的是在listActivity中添加一个按钮,而不是在列表中。如果我在我的 Activity 中添加一个扩展 ListActivity 的按钮,则该按钮会显示在 List 的每个项目中,因为 SimpleCursorAdapter 正在使用 XML 的 ID 填充列表。
  • 贴一些代码,一张图片,异常......
  • @pankaj 问题是我必须在我的 Activity 中添加一个扩展 ListActivity 的按钮。但是如果我添加按钮,按钮会显示在列表的每个项目中,但是我希望按钮只显示一次。为了避免这种情况,我尝试将 listView 放在单独的 LinearLayout 中,但是无法通过 SimpleCursorAdapter 填充 linearLayout(构造函数方法不支持 LinearLayouts,它支持 XML 布局)。现在如何添加按钮?
  • @Jack private void showSubjectsOnList() { String sql = "SELECT " + _ID + "," + SUBJECT + " FROM " + TABLE_NAME + " GROUP BY " + SUBJECT + ";";光标 cursor = subject.getReadableDatabase().rawQuery(sql, null); SimpleCursorAdapter 适配器 = new SimpleCursorAdapter(this, R.layout.item_subject, cursor, subFROM, subTO); setListAdapter(适配器);开始管理光标(光标); SimpleCursorAdapter 中使用的 R.layout.item 是一个 XML 布局,其中我有一个线性布局。在此 LinearLayout 上,我想填充 ListView,否则按钮将显示在所有项目中。怎么样?

标签: android listview simplecursoradapter


【解决方案1】:

您必须自己在代码或 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
        }
    });
    }
}

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

【讨论】:

  • @COdeAttack 我尝试这样做并在 my_laout.xml 中创建了一个布局,其 ID 为 listViewLinearLayout。然后我在简单光标适配器中使用了这个 id (listViewLinearLayout) 如下 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.id.listViewLinearLayout, cursor, subFROM, subTO);但是应用程序崩溃了,仅仅是因为 SimpleCursorAdapter 需要 R.layout.my_layout 而不是 my_layout 中的 linearLayout 的 id。我也尝试使用 listView 的 id(id 是 listView1),但这也没有用。
  • 啊,好吧,你已经尝试过了,这很好。如果您可以在上面的问题中粘贴一些相关的代码/xml,那么这将有助于我们找到问题或提出建议。
  • 如何在这里张贴图片? :s
  • 编辑您的原始问题,在工具栏中会有一个图标来上传图片。如果您不确定它是哪个图标,请依次将鼠标悬停在每个图标上并查看工具提示。
  • 重新阅读了您的评论;我认为您的问题可能是您正在使用 ListView ID。您应该将资源 ID 用于该 ListView 中每一行的布局。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 2012-09-16
  • 2022-10-19
  • 2013-07-29
相关资源
最近更新 更多