[英]How to access ArrayList from custom adapter class, when the ArrayList is stored in a class that extends Application
[英]Access ArrayList from Custom Adapter
我想做的是從客戶適配器訪問ListView的主要活動中的ArrayList。
我有一個可單擊的列表視圖,該列表視圖是在客戶適配器(onclick所在的位置)內完成的。 當用戶單擊它時,他們將輸入標題(文本),該標題將更新主活動中的arraylist(字符串)。
我已經閱讀了有關此主題的其他一些問題,但是我對如何使該工作迷失了方向。
我認為不需要發布代碼段,因為它只是基本的arraylist(string)和用於listview的自定義適配器。 如果需要代碼,我可以發布。 謝謝!
這里是適配器代碼的一部分:
public class PhotoAdapter extends ArrayAdapter<Photo> {
private final ArrayList<Photo> objects;
public PhotoAdapter(Context context, int textViewResourceId,
ArrayList<Photo> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Photo i = objects.get(position);
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.listview_row, null);
v.setClickable(true);
v.setFocusable(true);
}
這是來自主要活動的代碼
public ArrayList<Photo> m_photos = new ArrayList<Photo>();
public ArrayList<String> m_photo_captions = new ArrayList<String>();
private PhotoAdapter m_adapter;
this.list1 = (ListView) findViewById(R.id.lstPhotos);
m_adapter = new PhotoAdapter(this, R.layout.listview_row, m_photos);
LayoutInflater infalter = getLayoutInflater();
list1.setAdapter(m_adapter);
if (Intent.ACTION_SEND_MULTIPLE.equals(action)
&& intentGallery.hasExtra(Intent.EXTRA_STREAM)) {
ArrayList<Parcelable> list = intentGallery
.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
for (Parcelable p : list) {
Uri uri = (Uri) p;
imagePath = getPath(uri);
m_photos.add(new Photo(imagePath, ""));
m_photo_locations.add(imagePath);
m_photo_captions.add("");
m_adapter.notifyDataSetChanged();
}
}
onclick偵聽器
list1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Do whatever you want with m_photo_captions here
Log.v("listview", "listview item clicked");
appErrorAlert("test", "test");
}
});
您可以將要訪問的ArrayList定義為全局實例變量,這樣就可以從自定義適配器內部訪問它。
如果您為問題提供了代碼段,這也將有所幫助。
既然您已經發布了代碼,我將建議使用不同於Mohamed A. Karim的方法。
您試圖在Adapter
的整個行上設置一個簡單的OnClickListener
,但是訪問您的Activity成員。 為什么OnItemClickListener
在已經可以訪問所需列表的ListView
設置OnItemClickListener
?
list1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Do whatever you want with m_photo_captions here
}
});
接下來,刪除所有將在touch事件到達ListView
之前將其攔截的事件。 您還可以使getView()
變得更小,更快,如下所示:
public class PhotoAdapter extends ArrayAdapter<Photo> {
LayoutInflater mInflater;
public PhotoAdapter(Context context, int textViewResourceId,
ArrayList<Photo> objects) {
super(context, textViewResourceId, objects);
mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Photo i = get(position);
View v = convertView;
if (v == null) {
v = mInflater.inflate(R.layout.listview_row, null);
// Initialize your ViewHolder here!
}
// Update your TextViews, ImageViews, etc here
...
}
}
希望有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.