在 LinearLayouts 中使用 ListViews 而不是全屏
我是 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 Iqbal
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您基本上必须自己用代码或 XML(推荐 XML)创建布局。
然后在该布局中创建一个 ListView。您可以创建按钮并将其添加到相同的布局中。由于您正在创建布局,因此您可以按照适合您需要的任何方式放置 ListView 和 Button。
然后,您创建对 ListView 的引用,以便可以为其设置适配器。
示例:
名为“my_layout.xml”的 XML 文件中的布局:
然后在代码中:
显然没有数据,但您可以这样做,但将适配器设置在我上面指出的位置。
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":
Then in code:
Obviously there's no data but you can do that but setting your adapter where I have indicated above.