我的问题是这样的:我想在我的应用程序中进行活动,你可以搜索一个人更新一个人或同时删除他 现在搜索部分已完成,我将其显示在列表视图中 现在我想要从列表视图中选择一个人你可以填写那些字段并更新我的问题是从列表视图中获取所选项目的信息列表视图包含人名id和电子邮件和电话号码这里是代码
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>
自定义适配器类
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;
}
}
最后是我希望在
中完成所有工作的课程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) {
}
});
}
}
答案 0 :(得分:1)
做这样的事情。
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) {
}
});
答案 1 :(得分:1)
您需要将人员列表(List<Person>
)作为全局变量。
另一个名为selectedPersonId的int变量。
然后,您需要捕获列表视图中所选项目的索引。
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedPersonId = position;
}
}
最后,当您单击modifierButton时,您可以从全局列表中获取正确的人。
modifierButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Person selectedPerson = personList.get(selectedPersonId);
//You can to implement the Parceable interface in your Person class to pass the selected person to another activity.
}
}
);