Reputation: 2252
I have a custom Array list that shows data from my Sqlite database, My problem is when i click on the item (from the list view, except the 1st item) its gets the wrong data (from my db), but when i implemented a button (inside the ListAdapter) with the same code it worked just fine.
What is the difference between "setOnItemClickListener" and "setOnClickListener" inside 'Array list'? and how can i fix it?
Thank you
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;
}}
Upvotes: 0
Views: 531
Reputation: 28516
Instead of using temp
you should use position
argument passed through OnItemClickListener to get your data
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); }
});
Upvotes: 1