每当我添加一些内容时SQLiteDatabase,它ListView都没有显示,但如果我完全重新启动应用程序,它就会显示.这是我的主要.java:
package com.gantt.shoppinglist;
import android.app.Dialog;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class ShoppingList extends ListActivity {
SimpleCursorAdapter adapter = null;
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final DataHelper dataHelper = new DataHelper(this);
ListView lv = (ListView) findViewById(android.R.id.list);
final SQLiteDatabase db = dataHelper.selectAll();
final Cursor c = db.rawQuery("SELECT DISTINCT oid as …Run Code Online (Sandbox Code Playgroud) 我正在尝试刷新使用创建为SimpleCursorAdapter的ListAdapter的ListView.
这是我在onCreate中创建Cursor和ListAdapter的代码,它填充了ListView.
tCursor = db.getAllEntries();
ListAdapter adapter=new SimpleCursorAdapter(this,
R.layout.row, tCursor,
new String[] columns,
new int[] {R.id.rowid, R.id.date});
setListAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
然后,我在另一个方法中向db添加一些数据,但我无法弄清楚如何刷新ListView.stackoverflow和其他地方的类似问题提到使用notifyDataSetChanged()和requery(),但ListAdapter或SimpleCursorAdapter的方法都没有.
我已经从SDK页面修改了这个示例,以从手机中获取所有联系人组并显示它们.但是,我无法弄清楚如何单击其中一个,然后使用Groups._ID执行其他操作.有人可以教我如何在这个列表上获得一个点击/选择监听器吗?
MyListAdapter.java
package com.example.simplelist;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.Groups;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
public class MyListAdapter extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_activity_view);
Cursor mCursor = this.getContentResolver().query(Groups.CONTENT_URI, null, null, null, null);
startManagingCursor(mCursor);
ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item, mCursor, new String[] {Groups.NAME, Groups._ID}, new int[] {android.R.id.text1, android.R.id.text2});
setListAdapter(adapter);
}
}
Run Code Online (Sandbox Code Playgroud)
custom_list_activity_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false" android:choiceMode="singleChoice" android:clickable="true" />
<TextView android:id="@id/android:empty"
android:layout_width="fill_parent" …Run Code Online (Sandbox Code Playgroud) 我有一个Listview,我想用我的SQLite数据库填充信息,这似乎是最实用的解决方案.在我的调试器中它说它是由以下原因造成的:
IllegalArgumentException没有这样的列.Id不存在
这是我试图填充它的java文件:
data = new MyData(this);
ListView lv = (ListView) findViewById(R.id.list);
ListAdapter adapter = new SimpleCursorAdapter(
this,
R.layout.list,
data.selectData(),
new String[] {
"name",
"title"
},
new int[] {
R.id.name,
R.id.title
}
);
lv.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
R.layout.list xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="8dip"/>
<TextView android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
public Cursor selectData() {
return db.query("tbl_mydata", new String[] {"name", "abb" }, null, null, null, null, null);
}
Run Code Online (Sandbox Code Playgroud) 我提前道歉,因为我离开了笔记本电脑并且无法生成任何代码示例,但希望我能够清楚地回答我的问题.我有一个由sqlite db支持的ListView(通过SimpleCursorAdapter).我正在尝试实现功能,因此列表一次只显示db查询中的某些元素.我通过在onCreate中加载db中的所有数据来完成此操作,并在启动列表时使用首选的元素#覆盖SimpleCursorAdapter的getCount方法.我还有一个按钮,可以将更多元素加载到列表中.这是通过更新getCount应返回的计数来完成的.
这在添加更多listview行时工作正常,但是在单击按钮后,我看到列表中的内容出现了奇怪的行为.我看到重复的列表元素和不正确的元素内容,这些都是在我的bindview方法中设置的.我假设这与覆盖getCount方法有关(我确定你是一个更好的方法来执行"Load Next 25"功能),因为当我只显示整个db结果集时我没有看到这种行为并且不要覆盖该方法.
我想我的问题是,当你处理SimpleCursorAdapter时,可以覆盖getCount方法导致bindview方法中的有趣行为吗?使用支持ListView的db查询结果实现此类功能的最佳方法是什么?
在此先感谢...如果需要,我可以在明天回到笔记本电脑时提供代码片段.
我正在使用数据库,从中获取光标,然后使用simplecursoradapter填充列表视图.我似乎无法弄清楚为什么应用程序在创建一个新的simplecursoradapter时崩溃了.我的光标包含在单个对象中.我通过这个类中的数据变量引用它.
String[] columns = new String[] {"item", "quantity", "days"};
int[] to = new int[] { R.id.nametext, R.id.quantitytext, R.id.dayslefttext};
SimpleCursorAdapter sCA = new SimpleCursorAdapter(this, R.layout.itemrow, data.listCursor, columns, to);
listView1.setAdapter(sCA);
Run Code Online (Sandbox Code Playgroud)
simplecursoradapter对构造函数中传递给它的信息做了什么?格式错误的参数之一是什么?这是我用来查看是否有效的每一行的简单xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading Screen"
android:id="@+id/nametext"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading Screen"
android:id="@+id/quantitytext"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading Screen"
android:id="@+id/dayslefttext"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是我加载游标的方法:
listCursor = myDataBase.query(ITEMS_TABLE, null, "location" +" = ?", new String[]{IN_SPECIAL_LIST}, null, null, "item");
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚问题是什么.我通过一些调试来运行它,当II创建新的simplecursoradapter时会出现问题.
在我的应用程序的开始我显示一个对话框.在这个对话框中,我有2个微调器.如果我更改第一个微调器的条目,则应用程序会加载JSON文件并将其解析为数据库.然后,微调器从保存JSON文件的数据库中填充SimpleCursorAdapter.问题是,当我更改第一个微调器时,它总是加载上次更改微调器时保存的数据库.
这是来自onItemSelected方法的代码:
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
dialogs.dismiss();
}
};
Thread checkUpdate = new Thread() {
public void run() {
klassenListeAktualisieren((new Long(txtBerufID)).toString());
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
dbHelperKlasse = new KlassenlisteDbAdapter(myContext);
dbHelperKlasse.open();
Cursor cursor_Names = dbHelperKlasse.fetchAllOfThem();
startManagingCursor(cursor_Names);
String[] columns = new String[] { dbHelperKlasse.KEY_TITLE };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(myContext, android.R.layout.simple_spinner_item,cursor_Names, columns, to);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
klassenSpinner.setAdapter(mAdapter);
Run Code Online (Sandbox Code Playgroud)
如果您需要了解其他任何内容,请告诉我(代码等).
我正在制作一个我使用数据库的android食谱应用程序.在数据库中有一个名为""images"的列,其中我存储了我在drawable文件夹中存储的食谱图片文件的名称.现在我想用食谱制作一个列表,显示:1)标题配方2)一个简短的描述和3)配方的图像为了做到这一点,我使用Simplecursoradaptor.
我的问题是我无法显示图像.
我想从"images"列中读取文件名,然后在我的imageview中设置图像(R.id.imageview1)
这是我的代码,直到现在:
public class RecipesMainActivity extends Activity
{
public static final String ROW_ID = "row_id"; //Intent extra key
private ListView esodaListView; // the ListActivity's ListView
private SimpleCursorAdapter esodaAdapter; // adapter for ListView
DatabaseConnector databaseConnector = new DatabaseConnector(RecipesMainActivity.this);
// called when the activity is first created
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipes_main);
esodaListView = (ListView)findViewById(R.id.esodaList);
esodaListView.setOnItemClickListener(viewEsodaListener);
databaseConnector.open();
// map each esoda to a TextView in the ListView layout
// The desired …Run Code Online (Sandbox Code Playgroud) 在我的代码中,现在,为了正确刷新列表视图,我必须重新获取我的数据库信息并重新创建SimpleCursorAdapter.
例如,我在listview中有一个按钮.单击此按钮时,它将从数据库中删除listview项的条目.所以我想要发生的是将项目从列表视图中删除,而不必重新创建适配器.
我已经尝试将我的全局更改SimpleCursorAdapter为BaseAdapater(因为它扩展SimpleCursorAdapater并允许使用该notifyDataSetChanged()函数),但它仍然无效.
这是我现在使用的代码(它确实有效):
global声明代码和onCreate():
private RoutinesDataSource datasource;
private SimpleCursorAdapter dataAdapter;
private boolean isEditing = false;
private Toast toast_deleted;
private String[] columns = new String[] { MySQLiteHelper.COLUMN_NAME };
private int[] to;
@SuppressLint("ShowToast")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_routines);
toast_deleted = Toast.makeText(this, "", Toast.LENGTH_SHORT);
datasource = new RoutinesDataSource(this);
datasource.open();
Cursor cursor = datasource.fetchAllRoutines();
to = new int[] { R.id.listitem_routine_name };
dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_routine, cursor, …Run Code Online (Sandbox Code Playgroud) 我有一个搜索视图,其中包含由MatrixCursor填充的建议(因为我已经有一个字符串数组).但是我想知道用户正在选择哪个项目.到目前为止,我只能获得用户点击建议列表的位置:
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionClick(int position) {
String selectedItem = (String)mAdapter.getItem(position);
Log.e("search view", selectedItem);
return true;
}
Run Code Online (Sandbox Code Playgroud)
但是我有一个错误:android.database.MatrixCursor无法转换为java.lang.String,我不知道如何解决它.非常感谢任何帮助.
android android-arrayadapter simplecursoradapter matrixcursor searchview
android ×10
listview ×6
listadapter ×3
sqlite ×2
baseadapter ×1
cursor ×1
imageview ×1
matrixcursor ×1
onselect ×1
refresh ×1
searchview ×1