老是获取到position的最后一个值

不管按哪个按钮,上一次老是获取到position的第一个值,显示"苹果",然后按照网上的解决办法改了之后,老是获取到position的最后一个值,显示"运行"
图片说明

 package com.example.exer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity  {
    private static final String TAG = "MainActivity";
    private List<Map<String, Object>> mdata;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        ListView mListView=getListView();
        mdata = data();
        mListView.setAdapter(new MyAdapter(this,mdata));
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> l, View v, int position, long id) {
    Log.i(TAG, (String)mdata.get(position).get("title"));
    Toast.makeText(MainActivity.this, (String)mdata.get(position).get("title"), 1000).show();
    }
        });
    }

    class MyAdapter extends BaseAdapter{
    private Context context;
    Hodler hodler =null;
    private LayoutInflater minflater;
    private ArrayList<Map<String,Object>> datalist;
    public MyAdapter(Context context,List<Map<String,Object>> datalist) {
        this.context=context;
        this.minflater = LayoutInflater.from(context);
        this.datalist=(ArrayList<Map<String,Object>>)datalist;
    }

    @Override
    public int getCount() {
    // TODO Auto-generated method stub
    return datalist.size();
    }

    @Override
    public Object getItem(int position) {
    // TODO Auto-generated method stub
    return datalist.get(position);
    }

    @Override
    public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView == null){
    hodler = new Hodler();
    convertView= minflater.inflate(R.layout.mylist, null);
    hodler.img = (ImageView) convertView.findViewById(R.id.img);
    hodler.title = (TextView) convertView.findViewById(R.id.title);
    hodler.info = (TextView) convertView.findViewById(R.id.info);
    hodler.button = (Button) convertView.findViewById(R.id.button);
    convertView.setTag(hodler);
    }else{
    hodler = (Hodler)convertView.getTag();
    }
    hodler.img.setBackgroundResource((Integer) mdata.get(position).get("img"));
    hodler.title.setText((CharSequence) mdata.get(position).get("title"));
    hodler.info.setText((CharSequence) mdata.get(position).get("info"));
    //hodler.button.setTag(position);
    hodler.positio=position;
    hodler.button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    //showdialog((int)hodler.button.getTag());  
        showdialog(hodler.positio);
    }
    });
    return convertView;
    }
    class Hodler {
        public ImageView img;
        public TextView title;
        public TextView info;
        public Button button;
        int positio;//增加一个变量记录每个item的值
        }
    }

    public void showdialog(int position) {
    new AlertDialog.Builder(this).setTitle("提示信息").setMessage("你点击的标题是:  " +(String)mdata.get(position).get("title"))
    .setPositiveButton("确定", new DialogInterface.OnClickListener(){

        @Override
        public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
        setResult(RESULT_OK);
        finish();
        }
     }).setNegativeButton("取消", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
    }).show();
    }



    public List<Map<String, Object>> data() {
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("title", "苹果");
    map.put("info", "apple");
    map.put("img", R.drawable.ic_launcher); 
    list.add(map);
    map = new HashMap<String, Object>();
    map.put("title", "书");
    map.put("info", "book");
    map.put("img", R.drawable.ic_launcher);
    list.add(map);
    map = new HashMap<String, Object>();
    map.put("title", "电脑");
    map.put("info", "computer");
    map.put("img", R.drawable.ic_launcher);
    list.add(map);
    map = new HashMap<String, Object>();
    map.put("title", "对话");
    map.put("info", "dialog");
    map.put("img", R.drawable.ic_launcher);
    list.add(map);
    map = new HashMap<String, Object>();
    map.put("title", "运行");
    map.put("info", "run");
    map.put("img", R.drawable.ic_launcher);
    list.add(map);
    return list;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

5个回答

引用:把 showdialog(hodler.positio); hodler.positio 改成 position ;直接用getView()方法传递过来的position

不要在方法的参数上加final,肯定报错啦

直接使用就可以,或者新建个 final int index=position; 使用index就可以了

YXTS122
YXTS122 只能新建个final int index=position,直接使用是不行的!!!!!
大约 3 年之前 回复

把 showdialog(hodler.positio); hodler.positio 改成 position ;直接用getView()方法传递过来的position

qq_35071078
下课睡觉 在getview方法的 position 前面加一个final就行了
大约 3 年之前 回复
YXTS122
YXTS122 你那样会报错的,没用,我早试过了,
大约 3 年之前 回复

我赞同上面的那位兄弟的方法,你说不行的话 可以尝试在showdialog(hodler.positio);方法中把position参数直接换成mdata.get(position)获取到的对象

你可以在创建一个List 2 ,在你List每次add之后你在用list2.addAll(list);

改好了
图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问