在 LinearLayouts 中使用 ListViews 而不是全屏

发布于 2024-12-01 20:36:23 字数 2293 浏览 3 评论 0原文

我是 Android 新手,我希望添加主题按钮仅显示一次,而不是在每个项目上显示。我怎样才能使用 SimpleCursorAdapter 做到这

一点 Waneya Iqbal我希望添加主题按钮仅显示一次,而不是在每个项目上显示

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

public class PopulatedSubject extends 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;
}   

布局

如下:

布局图片为:

在此输入图片描述

I am new to android and I want the add subject button to be displayed only once and not on each item. How can i do this using SimpleCursorAdapter

Regards
Waneya IqbalI want the add subject button to be displayed only once and not on each item

The code is as follows( see the showSubjectOnList() method ):

public class PopulatingSubject extends ListActivity {

public static String subjectName;
private SoftCopyDatabase subjects;

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;
}   

}

The layout is as follows :

The picture of layout is:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梓梦 2024-12-08 20:36:23

您基本上必须自己用代码或 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
        }
    });
    }
}

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

You have to basically create a Layout yourself either in code or in XML (XML is recommended).
Then create a ListView within that Layout. You can create your button and add it to the same layout. Since you are creating your layout you can position the ListView and the Button in whatever way suits your needs.

You then create a reference to your ListView so that you can set an adapter to it.

Example:

Layout in XML file named "my_layout.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>

Then in code:

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
        }
    });
    }
}

Obviously there's no data but you can do that but setting your adapter where I have indicated above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文