[英]ArrayList strange behavior
我正在做一個應用程序,它從xml中獲取數據,並放置在數據庫中,並在圖片的gridview中顯示此數據(圖片的名稱和url)。 我添加了一個onitemclicklistener來獲取url(從ArrayList),並轉移到另一個活動,因此我可以將url轉換為位圖並顯示在imageview中。
//cursor that returns all items from databse to a arraylist
final Cursor cursor = entry.getAllRows();
//arraylist
names = new ArrayList<Post>();
cursor.moveToFirst();
//adding all database values to the arraylist
while (cursor.isAfterLast() == false) {
names.add(new Post(cursor.getString(cursor.getColumnIndex("name")), cursor.getString(cursor.getColumnIndex("link"))));
cursor.moveToNext();
}
現在是onitemclicklistsner代碼:
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
position++;
Post text = names.get(position);
Log.i("POSITION", "Exeption:"+position + " " + text);
//i.putExtra("id", text);
//startActivity(i);
}
});
我的位置正確,但問題是我無法從該位置獲取相應的網址。 它不是顯示鏈接,而是顯示鏈接:com.example.partedoxml.Post@42b191d8問題是我怎么不能從arraylist中獲得單個項目的位置?
你的問題是
names.get(position);
您想要做的是獲取Post對象而不是名稱
因此在您的Post Class中將有一個以Name命名的文件
得到這樣的名字
在post類中,為名稱創建getter和setter ,然后在您的偵聽器中編寫
String name =names.get(position).getName();
要么
在您的上課時將名字公開
public String name;
然后在你的聽眾
String name =names.get(position).name;
或你在做什么
Post text = names.get(position);
Log.i("POSITION", "Exeption:"+position + " " + text.name);
您正在按索引獲得一個帖子(索引是您在position變量中指定的)。
您需要在Post類中實現public String toString(){}
方法,以查看Post對象中的字段值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.