Ben Mustapha Sabrine Ben Mustapha Sabrine - 1 year ago 55
Android Question

get the value of item view clicked with android

I have a listview in android which has the following xml:

android:id="@+id/list_assurance" />

and the listItem in other xml file

I want to get the value of the item clicked in a listView.
I worked with the baseAdapter
this is my code:

public class InscriptionAssuranceRemorqueur extends AppCompatActivity implements AdapterView.OnItemClickListener, View.OnClickListener {

public static ArrayList<Assurance> assuranceArray = new ArrayList<Assurance>();
private ListView list_assurance;
private Button btn_confirmation;
private CheckBox checkbox;

private double largeur;
private double longueur;
private double poids;
private String nom;
private String mail;
private String tel;
private String mdp;
private TextView txt;

protected void onCreate(Bundle savedInstanceState) {

Intent x = this.getIntent();
nom = x.getExtras().getString("nomCompagnie");
mail = x.getExtras().getString("mail");
tel = x.getExtras().getString("tel");
mdp = x.getExtras().getString("mdp");
String largeur = x.getExtras().getString("largeur");
String longueur = x.getExtras().getString("longueur");
String poids = x.getExtras().getString("poids");
this.largeur = Double.parseDouble(largeur);
this.longueur = Double.parseDouble(longueur);
this.poids = Double.parseDouble(poids);

//remplir le tab des compagnies d'assurance
assuranceArray.add(new Assurance("","GAT"));
assuranceArray.add(new Assurance("","STAR"));
assuranceArray.add(new Assurance("","Comar"));
assuranceArray.add(new Assurance("","Ctama"));


list_assurance = (ListView)findViewById(;
btn_confirmation = (Button)findViewById(;

MonAdapter adapter = new MonAdapter(this,assuranceArray);



public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


public void onClick(View v) {
LinearLayout layout = new LinearLayout(this);
LayoutInflater inflater = LayoutInflater.from(this);
layout = (LinearLayout)inflater.inflate(R.layout.item_view_assurance,null);
txt = (TextView) layout.findViewById(;
checkbox = (CheckBox) layout.findViewById(;



shoud I use onClick or onItemClick ?

Answer Source

Acutally, position variable inside of onItemClick method should correspond to the position of the item clicked of the listView. If you want to take the value of the assuranceArray that is stored in clicked listview item then you need to use that position variable. For instance, your solution can look like:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    String email = assuranceArray.get(position).getEmail()

Here, getEmail() method is just my guess. You need to replace it with your own method specified inside of Assurance model class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download