我有一个自定义数组列表,显示来自我的Sqlite数据库的数据,我的问题是当我点击项目时(从列表视图中,除了第一项)它获取错误的数据(来自我的数据库),但是当我使用相同的代码实现了一个按钮(在ListAdapter内)。
“数组列表”中的“setOnItemClickListener”和“setOnClickListener”有什么区别?我该如何解决?
谢谢
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) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.viewteam);
totalrecords = (TextView) findViewById(R.id.totalrecords1);
listview = (ListView) findViewById(R.id.listview1);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
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;
}}
答案 0 :(得分:1)
您应该使用通过OnItemClickListener传递的temp
参数来获取数据,而不是使用position
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); }
});