/*imports and packages*/
public class ShowBookmarks extends ListActivity
{
private ArrayList<String> booknames;
private ArrayList<String> bookmarknames;
private ListView myListView;
private bookmarkListAdapter mybookmarkListAdapter;
String path="none";
public static final String FILENAME_EXTRA = "FILENAME_EXTRA";
public static final String PAGE_EXTRA = "PAGE_EXTRA";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
myListView = getListView(); // get the built-in ListView
listBookmarkFiles();
mybookmarkListAdapter = new bookmarkListAdapter(this, booknames,bookmarknames);
myListView.setAdapter(mybookmarkListAdapter);
}
private static class ViewHolder
{
public TextView textView_name;
public TextView textView_bookmark;
}
public void listBookmarkFiles()
{
String bookdir="nothing";
String bookmarkNames="nothing";
booknames = new ArrayList<String>();
bookmarknames = new ArrayList<String>();
SQLiteDatabase db;
db=openOrCreateDatabase("epub.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
try
{
Cursor cur = db.query("BookMark",null, null, null,null,null,null);
cur.moveToFirst();
while(cur.isAfterLast()==false)
{
bookdir = cur.getString(1);
int p = bookdir.lastIndexOf("/");
path=bookdir.substring(0,p);
bookdir = bookdir.substring(p);
bookmarkNames=cur.getString(3);
booknames.add(bookdir);
bookmarknames.add(bookmarkNames);
cur.moveToNext();
}
cur.moveToFirst();
cur.close();
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
db.close();
}
}
public class bookmarkListAdapter extends ArrayAdapter<String>
{
private List<String> fileDir;
private List<String> mybooks;
private List<String> mybookmarks;
private LayoutInflater myInflater;
public bookmarkListAdapter(Context context, List<String> bookz, List<String> bookmarkz)
{
super(context, -1, bookz);
this.mybooks = bookz;
this.mybookmarks = bookmarkz;
myInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getBookmarkView(int position, View convertView, ViewGroup parent)
{
ViewHolder bookmarkviewHolder; // holds references to current item's GUI
//ViewHolder viewHolder1;
// if convertView is null, inflate GUI and create ViewHolder;
// otherwise, get existing ViewHolder
if (convertView == null)
{
//Toast.makeText(ListEpubActivity.this,"" , Toast.LENGTH_SHORT).show();
convertView = myInflater.inflate(R.layout.bookmarks, null);
System.out.println("inside convert view");
bookmarkviewHolder = new ViewHolder();
bookmarkviewHolder.textView_name = (TextView) convertView.findViewById(R.id.bookname);
bookmarkviewHolder.textView_bookmark=(TextView) convertView.findViewById(R.id.bookmarkname);
convertView.setTag(bookmarkviewHolder); // store as View's tag
}
else
{
bookmarkviewHolder = (ViewHolder) convertView.getTag();
//viewHolder1 = (ViewHolder) convertView.getTag();
}
// Populate the list item (view) with the comic's details
String bookName = mybooks.get(position);
String realName = mybookmarks.get(position);
bookmarkviewHolder.textView_name.setText(bookName);
bookmarkviewHolder.textView_name.setTag(bookName);
bookmarkviewHolder.textView_bookmark.setText(realName);
bookmarkviewHolder.textView_bookmark.setTag(realName);
return convertView;
}
}
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String bookmarkAddress="none";
String fileName =booknames.get(position);
String fileChapterAddress= bookmarknames.get(position);
fileName=path+"/"+fileName;
SQLiteDatabase db;
db=openOrCreateDatabase("epub.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
try
{
String selectQueryAdd = "SELECT lastaddress FROM BookMark WHERE bookdir=? AND addresname=?";
Cursor c = db.rawQuery(selectQueryAdd, new String[] { fileName,fileChapterAddress });
if (c.moveToFirst())
{
bookmarkAddress = c.getString(c.getColumnIndex("lastaddress"));
}
}
catch (Exception e)
{
System.out.println(e);
}
finally
{
db.close();
}
System.out.println(bookmarkAddress);
Toast.makeText(ShowBookmarks.this, fileName+"address"+bookmarkAddress, Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.putExtra(FILENAME_EXTRA, fileName);
intent.putExtra(PAGE_EXTRA, 0);
setResult(RESULT_OK, intent);
finish();
}
}
logcat说
03-07 17:50:45.281: E/AndroidRuntime(10103): FATAL EXCEPTION: main
03-07 17:50:45.281: E/AndroidRuntime(10103): android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.content.res.Resources.getValue(Resources.java:1018)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2105)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.content.res.Resources.getLayout(Resources.java:857)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.widget.ListView.makeAndAddView(ListView.java:1772)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.widget.ListView.fillDown(ListView.java:672)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.widget.ListView.fillFromTop(ListView.java:732)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.widget.ListView.layoutChildren(ListView.java:1625)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.widget.AbsListView.onLayout(AbsListView.java:1863)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.view.View.layout(View.java:11278)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.view.ViewGroup.layout(ViewGroup.java:4224)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.view.View.layout(View.java:11278)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.view.ViewGroup.layout(ViewGroup.java:4224)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.view.View.layout(View.java:11278)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.view.ViewGroup.layout(ViewGroup.java:4224)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.view.View.layout(View.java:11278)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.view.ViewGroup.layout(ViewGroup.java:4224)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.os.Looper.loop(Looper.java:137)
03-07 17:50:45.281: E/AndroidRuntime(10103): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-07 17:50:45.281: E/AndroidRuntime(10103): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 17:50:45.281: E/AndroidRuntime(10103): at java.lang.reflect.Method.invoke(Method.java:511)
03-07 17:50:45.281: E/AndroidRuntime(10103): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
03-07 17:50:45.281: E/AndroidRuntime(10103): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
03-07 17:50:45.281: E/AndroidRuntime(10103): at dalvik.system.NativeStart.main(Native Method)
我在这里做错了什么?请帮助,我正在尝试将我从数据库中获取的值添加到listview。
答案 0 :(得分:0)
从
改变mybookmarkListAdapter = new bookmarkListAdapter(this, booknames,bookmarknames);
到
mybookmarkListAdapter = new bookmarkListAdapter(this, R.layout.bookmarks,booknames,bookmarknames);\
public bookmarkListAdapter(Context context, List<String> bookz, List<String> bookmarkz)
{
super(context, -1, bookz);
this.mybooks = bookz;
this.mybookmarks = bookmarkz;
myInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
到
int resid //
public bookmarkListAdapter(Context context, int resID ,List<String> bookz, List<String> bookmarkz)
{
super(context, resID , bookz,bookmarkz);
this.mybooks = bookz;
this.mybookmarks = bookmarkz;
this.resid = resID ;
myInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
答案 1 :(得分:-2)
我刚刚将代码更改为此内容并且每件事情都很好
/*<imports and packages>*/
public class ShowBookmarks extends Activity
{
public static final String FILENAME_EXTRA = "FILENAME_EXTRA";
public static final String PAGE_EXTRA = "PAGE_EXTRA";
String pathofbook="none";
String dirFile="none";
String directoryfile="none";
String addrFile="none";
String bookmarkAddress="none";
String lid;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bookmarks);
ArrayList<SearchBookmarksResults> searchResults = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.ListView01);
//pathofbook= getIntent().getExtras().getString("bookpathfile");
lv1.setAdapter(new MyNewCustomBaseAdapter(this, searchResults));
lv1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Object o = lv1.getItemAtPosition(position);
SearchBookmarksResults fullObject = (SearchBookmarksResults)o;
Toast.makeText(ShowBookmarks.this, "You have chosen: " + " " + fullObject.getCid(), Toast.LENGTH_SHORT).show();
lid=fullObject.getCid();
String pathofbook= getIntent().getExtras().getString("bookmarkFile");
//dirFile=fullObject.getCname();
SQLiteDatabase db;
db = openOrCreateDatabase("epub.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
try
{
String selectQueryAdd = "SELECT lastaddress,bookdir FROM BookMark WHERE lid=?";
Cursor c = db.rawQuery(selectQueryAdd, new String[] { lid });
if (c.moveToFirst())
{
bookmarkAddress = c.getString(c.getColumnIndex("lastaddress"));
dirFile=c.getString(c.getColumnIndex("bookdir"));
}
c.close();
}
catch (Exception e)
{
System.out.println(e);
}
finally
{
db.close();
}
Intent intent=new Intent(ShowBookmarks.this,MainActivity.class);
intent.putExtra(FILENAME_EXTRA, dirFile);
intent.putExtra(PAGE_EXTRA, bookmarkAddress);
setResult(RESULT_OK, intent);
finish();
}
});
}
private ArrayList<SearchBookmarksResults> GetSearchResults()
{
String id="none";
ArrayList<SearchBookmarksResults> results = new ArrayList<SearchBookmarksResults>();
pathofbook= getIntent().getExtras().getString("bookpathfile");
System.out.println(pathofbook);
SQLiteDatabase db;
db = openOrCreateDatabase("epub.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
//TextView t11=new TextView(this);
try
{
Cursor cur1 = db.query("BookMark",null, null, null,null,null,null);
cur1.moveToFirst();
if(cur1.isAfterLast() == false)
{
id=cur1.getString(0);
}
cur1.close();
}
catch(Exception e)
{
System.out.println(e);
}
if(id==null)
{
new AlertDialog.Builder(this)
.setTitle("INFO")
.setMessage("No Bookmarks To Display")
.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.show();
}
else
{
try
{
Cursor cur = db.query("BookMark",null, null, null,null,null,null);
cur.moveToFirst();
while(cur.isAfterLast()==false)
{
if(cur.getString(1).equals(pathofbook))
{
SearchBookmarksResults sr1 = new SearchBookmarksResults();
sr1.setCid(cur.getString(0));
sr1.setCname(cur.getString(3));
results.add(sr1);
}
cur.moveToNext();
}
cur.moveToFirst();
cur.close();
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
db.close();
}
}
return results;
}
}