amir barksdale amir barksdale - 6 months ago 18
Android Question

get item slelcted from llistview with multiple data in it from android activity

hi my problem is this: i want to make activity in my app where u able to search for a person update a person or delete him at the same time
now the search part is done and i display it in a list view
now i want when selecting a person from a listview u can fill those field and update the person my problem is getting the information from the slected item from the list view the list view contains the person name id and email and phone number here is the code

the layout for the listView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#76ba7e"
android:layout_height="75dp">
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:id="@+id/nom_id"
android:layout_alignParentLeft="true"
android:text="nom"
android:gravity="center"
android:textAppearance="?android:textAppearanceLarge"
/>
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:id="@+id/prenom_id"
android:layout_toRightOf="@+id/nom_id"
android:text="Prenom"
android:gravity="center"
android:textAppearance="?android:textAppearanceLarge"
/>
<TextView
android:layout_width="300dp"
android:layout_height="match_parent"
android:id="@+id/email_id"
android:layout_toRightOf="@+id/prenom_id"
android:text="Email"
android:gravity="center"
android:textAppearance="?android:textAppearanceLarge"
/>
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:id="@+id/numero_tele_id"
android:layout_toRightOf="@+id/email_id"
android:text="Numero telephone"
android:gravity="center"
android:textAppearance="?android:textAppearanceLarge"
/>
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:id="@+id/person_id"
android:layout_toRightOf="@+id/numero_tele_id"
android:text="Numero telephone"
android:gravity="center"
android:textAppearance="?android:textAppearanceLarge"
/>

</RelativeLayout>


the custom adapter class

public class MembreAdapter extends BaseAdapter{
private Context context;
private List<Membre> memb;

public MembreAdapter(Context context, List<Membre> memb) {
this.context = context;
this.memb = memb;
}

@Override
public int getCount() {
return memb.size();
}

@Override
public Object getItem(int position) {
return memb.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v= View.inflate(context,R.layout.display_member_row,null);
TextView tid=(TextView) v.findViewById(R.id.person_id);
TextView tnom = (TextView)v.findViewById(R.id.nom_id);
TextView tprenom = (TextView)v.findViewById(R.id.prenom_id);
TextView temail = (TextView)v.findViewById(R.id.email_id);
TextView tnumero= (TextView)v.findViewById(R.id.numero_tele_id);
tid.setText(memb.get(position).getId());

tnom.setText(memb.get(position).getNom());
tprenom.setText(memb.get(position).getPrenom());
temail.setText(memb.get(position).getEmail());
tnumero.setText(""+memb.get(position).getNumero());




return v;
}
}


and finally the class that i want to all the work in

public class RechercheMembre extends AppCompatActivity {
EditText email,nom,prenom;
ListView listView;
Button rechercheButton,modifierButton;
BaseDeDonee bdd; int numero;
MembreAdapter mema;List<Membre> mem ;
String nom1,prenom1,email1,p;
int idp ;
Spinner spinner ;ArrayAdapter<CharSequence> ada;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recherche_membre);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
bdd=new BaseDeDonee(this);
mem=new ArrayList<>();

email = (EditText)findViewById(R.id.email);
nom=(EditText)findViewById(R.id.nom);
prenom=(EditText)findViewById(R.id.prenom);
rechercheButton=(Button)findViewById(R.id.button14);
listView=(ListView)findViewById(R.id.listrechetche);
modifierButton=(Button)findViewById(R.id.button15);
spinner=(Spinner)findViewById(R.id.etat);
ada = ArrayAdapter.createFromResource(this,R.array.etat_de_personne,android.R.layout.simple_spinner_item);
ada.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(ada);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
p = parent.getItemAtPosition(position).toString();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});

rechercheButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(email.getText().toString()==null && nom.getText().toString()==null && prenom.getText().toString()==null){
Toast.makeText(RechercheMembre.this, "il faut saisir au moins un champs ", Toast.LENGTH_LONG).show();
}else{
Cursor c = bdd.FindMember(nom.getText().toString(), prenom.getText().toString(), email.getText().toString(),p);
while(c.moveToNext()){
nom1= c.getString(c.getColumnIndex("nom"));
prenom1=c.getString(c.getColumnIndex("prenom"));
email1=c.getString(c.getColumnIndex("profile"));
numero=c.getInt(c.getColumnIndex("numero_tel"));
idp=c.getInt(c.getColumnIndex("ID"));



mem.add(new Membre(nom1,prenom1,email1,numero,idp));
}



mema= new MembreAdapter(getApplicationContext(),mem);
listView.setAdapter(mema);


}
}
});
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView iddsz = view.findViewById(R.id.person_id);




modifierButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {


}
}
);

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});



}

}

Answer

Do something like this.

listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        TextView email = (TextView) view.findViewById(R.id.email_id);
        String email = email.getText().toString(); // This is your email what you select
        Log.i("Email", "" + email);

        TextView person  = view.findViewById(R.id.person_id); // This is your persion id what you select
        String person  = person .getText().toString();
        Log.i("Person", "" + person);

        modifierButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


                    }
                }
        );

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});