Android:SQLiteDatabase
我创建了一个SQLiteDatabase,它保存用户输入的信息。用户输入自己想要通过facebook与他人分享的食谱数据。此用户数据存储在listview中,然后显示在listview中。这在大多数情况下都可以正常工作,但是当滚动listview以观察更多条目时,会出现空指针。我将3种类型的文本信息存储到此数据库中,但我只希望listview显示行的entryID,并在单击项目时显示其余信息 错误消息: 这是它所指的适配器类:Android:SQLiteDatabase,android,listview,android-sqlite,android-arrayadapter,android-viewholder,Android,Listview,Android Sqlite,Android Arrayadapter,Android Viewholder,我创建了一个SQLiteDatabase,它保存用户输入的信息。用户输入自己想要通过facebook与他人分享的食谱数据。此用户数据存储在listview中,然后显示在listview中。这在大多数情况下都可以正常工作,但是当滚动listview以观察更多条目时,会出现空指针。我将3种类型的文本信息存储到此数据库中,但我只希望listview显示行的entryID,并在单击项目时显示其余信息 错误消息: 这是它所指的适配器类: import android.content.Context; i
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class RecipeAdapter extends ArrayAdapter {
List list = new ArrayList();
public RecipeAdapter(Context context, int resource) {
super(context, resource);
}
public void add(Recipe object) {
list.add(object);
super.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecipeHolder recipeHolder;
if(row == null){
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.display_entry_row,parent,false);
recipeHolder = new RecipeHolder();
recipeHolder.tx_id = (TextView)row.findViewById(R.id.t_id);
}
else{
recipeHolder = (RecipeHolder) row.getTag();
}
Recipe recipe = (Recipe)getItem(position);
recipeHolder.tx_id.setText(recipe.getId_entry().toString());
return row;
}
static class RecipeHolder{
TextView tx_id;
}
}
这是它所指的一条线:
recipeHolder.tx_id.setText(recipe.getId_entry().toString());
具有getter和setter的配方任务:
public class Recipe {
private String id_entry;
public Recipe(String id_entry){
this.setId_entry(id_entry);
}
public String getId_entry() {
return id_entry;
}
public void setId_entry(String id_entry) {
this.id_entry = id_entry;
}
}
如果需要,我将为数据库本身执行后台任务:
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.widget.ListView;
import android.widget.Toast;
public class BackgroundTask extends AsyncTask<String, Recipe, String> {
Context ctx;
RecipeAdapter mRecipeAdapter;
Activity activity;
ListView listView;
BackgroundTask(Context ctx){
this.ctx = ctx;
activity = (Activity)ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String method = params[0];
DatabaseOperations dbOperations = new DatabaseOperations(ctx);
//adds information into database
if (method.equals("add_information")){
String entry = params[1];
String ingred = params [2];
String direc = params [3];
SQLiteDatabase db = dbOperations.getWritableDatabase();
dbOperations.putInformation(db, entry, ingred, direc);
return "One Row Inserted";
}
//gets information from database and places inside listview
else if (method.equals("get_info")){
listView = (ListView) activity.findViewById(R.id.listVrecipe);
SQLiteDatabase db = dbOperations.getReadableDatabase();
Cursor cursor = dbOperations.getInformation(db);
mRecipeAdapter = new RecipeAdapter(ctx,R.layout.display_entry_row);
String entry;
//loops through all row information
while (cursor.moveToNext()){
//grabs entry id
entry = cursor.getString(cursor.getColumnIndex(TableData.TableInfo.EntryID));
Recipe recipe = new Recipe(entry);
publishProgress(recipe);
}
return "get_info";
}
return null;
}
@Override
protected void onProgressUpdate(Recipe... values) {
mRecipeAdapter.add(values[0]);
}
@Override
protected void onPostExecute(String result) {
//after execution update listview with new entries
if(result.equals("get_info")){
listView.setAdapter(mRecipeAdapter);
}
else{
Toast.makeText(ctx,result, Toast.LENGTH_LONG).show();
}
}
}
根据您所附的图像,它表示您正在adapters getView方法中获得NullPointerException
在我看来,有两种可能性
一,。RecipeHold为null,因为您没有使用row.setTagRecipeHold将标记设置为RecipeHold
二,。可能是配方对象为空
当您使用ArrayAdapter时,其最佳做法是如下所示:
更新RecipeAdapter,如下所示:
希望这会有所帮助~可能是在RecipePater的getView方法中,如果使用else子句,则无法获取TextView的id。尝试添加recipeHolder.tx_id=TextViewrow.findViewByIdR.id.t_id;在Else子句中,或者交替移动它,以便在两种情况下都使用它,例如,在Else子句之后但在TextView的文本设置之前。我没有看到行。setTagrecipeHolder。谢谢,是我没有设置标记。这解决了问题。我只是添加了行。setTagrecipeHolder;在RecipeAdapter中的else对我起作用之后,这不是一个解决方案吗?是的,这是一个解决方案。此外,我刚刚添加了使用ArrayAdapter时的最佳实践。谢谢,非常有用
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class RecipeAdapter extends ArrayAdapter<Recipe> {
public RecipeAdapter(Context context, int resource, ArrayList<Recipe> list) {
super(context, resource, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RecipeHolder recipeHolder;
if(convertView == null){
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.display_entry_row,parent, false);
recipeHolder = new RecipeHolder();
recipeHolder.tx_id = (TextView) convertView.findViewById(R.id.t_id);
convertView.setTag(recipeHolder);
}
else{
recipeHolder = (RecipeHolder) convertView.getTag();
}
Recipe recipe = (Recipe) getItem(position);
recipeHolder.tx_id.setText(recipe.getId_entry().toString());
return row;
}
static class RecipeHolder {
TextView tx_id;
}
}
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.widget.ListView;
import android.widget.Toast;
public class BackgroundTask extends AsyncTask<String, Recipe, String> {
Context ctx;
RecipeAdapter mRecipeAdapter;
Activity activity;
ListView listView;
List<Recipe> listRecipe;
BackgroundTask(Context ctx){
this.ctx = ctx;
activity = (Activity)ctx;
listRecipe = new ArrayList<Recipe>();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String method = params[0];
DatabaseOperations dbOperations = new DatabaseOperations(ctx);
//adds information into database
if (method.equals("add_information")){
String entry = params[1];
String ingred = params [2];
String direc = params [3];
SQLiteDatabase db = dbOperations.getWritableDatabase();
dbOperations.putInformation(db, entry, ingred, direc);
return "One Row Inserted";
}
//gets information from database and places inside listview
else if (method.equals("get_info")){
listView = (ListView) activity.findViewById(R.id.listVrecipe);
SQLiteDatabase db = dbOperations.getReadableDatabase();
Cursor cursor = dbOperations.getInformation(db);
// Adapter
mRecipeAdapter = new RecipeAdapter(ctx, R.layout.display_entry_row, listRecipe);
String entry;
//loops through all row information
while (cursor.moveToNext()){
//grabs entry id
entry = cursor.getString(cursor.getColumnIndex(TableData.TableInfo.EntryID));
Recipe recipe = new Recipe(entry);
// Add recipe to list
listRecipe.add(recipe);
mRecipeAdapter.notifyDataSetChanged();
//publishProgress(recipe);
}
return "get_info";
}
return null;
}
@Override
protected void onProgressUpdate(Recipe... values) {
//mRecipeAdapter.add(values[0]);
}
@Override
protected void onPostExecute(String result) {
//after execution update listview with new entries
if(result.equals("get_info")){
listView.setAdapter(mRecipeAdapter);
} else {
Toast.makeText(ctx,result, Toast.LENGTH_LONG).show();
}
}
}