Ben Mustapha Sabrine Ben Mustapha Sabrine - 26 days ago 9
Android Question

get the value of item view clicked with android

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

<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
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;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fen_inscription_assurance_remorqueur);

//Intent
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@gmail.com","GAT"));
assuranceArray.add(new Assurance("STAR@gmail.com","STAR"));
assuranceArray.add(new Assurance("Comar@gmail.com","Comar"));
assuranceArray.add(new Assurance("Ctama@gmail.com","Ctama"));

//Récupération

list_assurance = (ListView)findViewById(R.id.list_assurance);
btn_confirmation = (Button)findViewById(R.id.btn_cfrm_as_rm);

//Adapter
MonAdapter adapter = new MonAdapter(this,assuranceArray);
list_assurance.setAdapter(adapter);
list_assurance.setOnItemClickListener(this);

//Ecouteurs

btn_confirmation.setOnClickListener(this);
}

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


}

@Override
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(R.id.txt_assurance);
checkbox = (CheckBox) layout.findViewById(R.id.checkbox_assurance);
if(v==checkbox)
{


Log.d("test",txt.getText().toString());

}
}}


shoud I use onClick or onItemClick ?

Answer

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:

@Override
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.