android “setOnItemClickListener” and “setOnClickListener” 方法有什么不同

文章标签: android,android-listview
2014-12-16 9:32:54     人阅读    

andorid 开发问题:
我有一个自定义的Array 列表,展示从 Sqlite 数据库中查询出来的数据,我现在发现一个问题。
当我点击里列表中的其中一条数据,它从数据库中获得的数据是错误的,但是,当我通实现一个按钮,并使用同样的代码是,运行的很正常!!!
我想知道 setOnItemClickListener 和 setOnClickListener 2个方法在  Array 列表中有什么不同?

下面是我的代码

public class ViewTeam  extends Activity {

private ListView listview;

TextView totalrecords;
DBHelper db;
public ArrayList<TeamModel> _teamlist = new ArrayList<TeamModel>();

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewteam);

    totalrecords = (TextView) findViewById(R.id.totalrecords1);
    listview = (ListView) findViewById(R.id.listview1);
}

@Override
protected void onResume() {
4
    super.onResume();

    _teamlist.clear();

    db = new DBHelper(getApplicationContext());
    db.getWritableDatabase();
    ArrayList<TeamModel> team_list = db.getTeams();

    for (int i = 0; i < team_list.size(); i++) {

        String tname = team_list.get(i).getTeamname();

        System.out.println("tname>>>>>" + tname);
        String topponent = team_list.get(i).getTeamopponent();
        String tdate = team_list.get(i).getTeamdate();

        TeamModel _TeamModel = new TeamModel();

        //_TeamModel.setIdno(tidno);
        _TeamModel.setTeamname(tname);
        _TeamModel.setTeamopponent(topponent);
        _TeamModel.setTeamdate(tdate);

        _teamlist.add(_TeamModel);
    }
    totalrecords.setText("Total Records :-" + _teamlist.size());
    listview.setAdapter(new ListAdapter(this));
    db.close();
}


private class ListAdapter extends BaseAdapter {
    LayoutInflater inflater;
    ViewHolder viewHolder;
    private android.widget.ListAdapter adapter;

    public ListAdapter(Context context) {
        // TODO Auto-generated constructor stub
        inflater = LayoutInflater.from(context);
    }

    @Override


    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.listviewrow, null);
            viewHolder = new ViewHolder();

            viewHolder.txt_name = (TextView) convertView
 
                    .findViewById(R.id.txtdisplayname);
            viewHolder.txt_opponent = (TextView) convertView
 
                    .findViewById(R.id.txtdisplayopponent);
            viewHolder.txt_date = (TextView) convertView
 
                    .findViewById(R.id.txtdisplaydate);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.txt_name.setText(_teamlist.get(position)
                .getTeamname().trim());
        viewHolder.txt_opponent.setText(_teamlist.get(position)
                .getTeamopponent().trim());
        viewHolder.txt_date.setText(_teamlist.get(position).
                getTeamdate().trim());

        final int temp = position;
        (convertView.findViewById(R.id.btn_update))
        .setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {

                String _teamname = String.valueOf(_teamlist
 
                        .get(temp).getTeamname());
                String _teamopponent = _teamlist.get(temp)
                        .getTeamopponent();
                String _teamdate = _teamlist.get(temp)
                        .getTeamdate();

                Intent intent = new Intent(ViewTeam.this,
                        AddReminder.class);

                Bundle bundle = new Bundle();
                bundle.putString("name1", _teamname);
                bundle.putString("opponent1", _teamopponent);
                bundle.putString("date1", _teamdate);
                intent.putExtras(bundle);
                startActivity(intent);

            }
        });

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
            {
                String _teamname = String.valueOf(_teamlist
 
                        .get(temp).getTeamname());
                String _teamopponent = _teamlist.get(temp)
                        .getTeamopponent();
                String _teamdate = _teamlist.get(temp)
                        .getTeamdate();

                Intent intent = new Intent(ViewTeam.this,
                        AddReminder.class);

                Bundle bundle = new Bundle();
                bundle.putString("name1", _teamname);
                bundle.putString("opponent1", _teamopponent);
                bundle.putString("date1", _teamdate);
                intent.putExtras(bundle);
                startActivity(intent);              }
        });

        return convertView;
                }

    private class ViewHolder {
        TextView txt_name;
        TextView txt_opponent;
        TextView txt_date;

    }}

处理方法

你需要在指定位置传递参数到 OnItemClickListener 方法中,不能使用temp,
如下:

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
String _teamname = String.valueOf(_teamlist

.get(position).getTeamname());
String _teamopponent = _teamlist.get(position)
.getTeamopponent();
String _teamdate = _teamlist.get(position)
.getTeamdate();

Intent intent = new Intent(ViewTeam.this,
AddReminder.class);

Bundle bundle = new Bundle();
bundle.putString("name1", _teamname);
bundle.putString("opponent1", _teamopponent);
bundle.putString("date1", _teamdate);
intent.putExtras(bundle);
startActivity(intent); }
});

上面的 android listview的setOnItemClickListener 方法,使用传参数才是正确的 ,把你的代码按照上面的方式改动就可以了


原文地址:http://www.itmmd.com/201412/322.html
该文章由 萌萌的IT人 整理发布,转载须标明出处。

andorid 开发,如何在divider中间添加一个TextView   上一篇
下一篇  ArrayAdapter getView 返回 NullPointerException -
精彩回复
我要追加问题,请求站长解决!
姓名:       

《程序员app》专门为程序员量身定做!