我有自定义适配器,因为我有一个ImageButton
并且想要设置onClickListener
。
问题是,当我点击位置0中的元素时,它返回位置3中的元素;我不明白为什么..
这是自定义适配器:
public class CustomAdapter extends BaseAdapter {
private final Activity context;
private LayoutInflater mInflater;
private ArrayList<HashMap<String, Object>> mlist;
private String mNome;
private String mCognome;
private String mTitolo;
private String mInfo;
ViewHolder viewHolder;
View view;
ArrayList<Integer> map = new ArrayList<>();
HashMap<String, Object> e;
public CustomAdapter(Activity context,ArrayList<HashMap<String, Object>> mlist){
this.context = context;
this.mlist=mlist;
mInflater = context.getLayoutInflater();
}
public class ViewHolder {
public ImageButton mImageButton;
public TextView mTitleView;
public TextView mNomeView;
public TextView mCognomeView;
public TextView mInfoView;
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public Object getItem(int position) {
Log.d("POSITION GET",Integer.toString(position));
if (mlist != null && position >= 0 && position < getCount()) {
Log.d("POSITION GET",Integer.toString(position));
return mlist.get(position);
}
return null;
}
@Override
public long getItemId(int position) {
if (mlist != null && position >= 0 && position < getCount()) {
return Integer.parseInt((String)mlist.get(position).get("id"));
}
return position;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
view = convertView;
e = (HashMap<String, Object>) getItem(position);
if (view == null) {
view = mInflater.inflate(R.layout.item_list_avvisi, parent, false);
viewHolder = new ViewHolder();
viewHolder.mImageButton = (ImageButton) view.findViewById(R.id.imageButton);
viewHolder.mCognomeView = (TextView) view.findViewById(R.id.cognome);
viewHolder.mNomeView = (TextView) view.findViewById(R.id.nome);
viewHolder.mTitleView = (TextView) view.findViewById(R.id.titolo);
viewHolder.mInfoView = (TextView) view.findViewById(R.id.info);
viewHolder.mImageButton.setClickable(true);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.mImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String nome = e.get("Nome").toString();
Log.d("TAG", nome);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, (String) "Avviso Scuola: " + nome + " " +
mCognome + "\n" +
mTitolo + "\n" +
mInfo
);
shareIntent.setType("text/plain");
context.startActivity(shareIntent);
}
});
viewHolder.mCognomeView.setText(e.get("Cognome").toString());
viewHolder.mNomeView.setText(e.get("Nome").toString());
viewHolder.mTitleView.setText(e.get("Titolo").toString());
viewHolder.mInfoView.setText(e.get("Info").toString());
return view;
}
在片段中我有:
private ArrayList<HashMap<String, Object>> mlist = new ArrayList<HashMap<String, Object>>();
...
for (int i = 1; i < array.length(); i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
Gson gson = new Gson();
Avv mAvv = gson.fromJson(String.valueOf(array.getJSONObject(i)), Avv.class);
map.put("id", String.valueOf(i));
map.put("Name", mAvv.getNome());
map.put("Surname", mAvv.getCognome());
map.put("Title", mAvv.getTitolo());
map.put("Info", mAvv.getTesto());
mlist.add(map);
}
...
CustomAdapter mAdapter = new CustomAdapter(getActivity(),mlist);
list.setAdapter(mAdapter);
答案 0 :(得分:2)
shareIntent.putExtra(Intent.EXTRA_TEXT, (String) "Avviso " + tx.getText().toString());
您正在使用从回收的视图中使用的数据。永远都不要这样做。而是从ArrayList
获取数据mlist.get(postion)....
您必须将final
声明添加到该位置,才能在onClick
方法
public View getView(final int position, View convertView, ViewGroup parent)
{