我在android中有一个listview,它有以下xml:
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_assurance" />
和其他xml文件中的listItem
我想获取listView中单击项目的值。 我使用baseAdapter 这是我的代码:
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());
}
}}
我的项目视图xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="30dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text=""
android:paddingLeft="20dp"
android:textColor="@color/gris"
android:id="@+id/txt_assurance"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingRight="50dp"
android:id="@+id/checkbox_assurance"/>
</LinearLayout>
</LinearLayout>
我的适配器:
public class MonAdapter extends BaseAdapter {
private ArrayList<Assurance> assuranceArray;
private Context c;
private TextView txt;
public MonAdapter(Context c,ArrayList<Assurance> list) {
this.assuranceArray = list;
this.c=c;
}
@Override
public int getCount() {
return assuranceArray.size();
}
@Override
public Assurance getItem(int position) {
return assuranceArray.get(position);
}
@Override
public long getItemId(int position) {
return assuranceArray.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout layout = new LinearLayout(c);
LayoutInflater inflater = LayoutInflater.from(c);
layout = (LinearLayout)inflater.inflate(R.layout.item_view_assurance,null);
txt = (TextView) layout.findViewById(R.id.txt_assurance);
txt.setText((CharSequence)getItem(position).getNomCompagnie());
return layout;
}}
所以我有listView包含一个textView和一个复选框,我需要的是检查项目的文本!请你好吗
答案 0 :(得分:0)
实际上,position
方法内的onItemClick
变量应对应于listView单击项目的位置。如果要获取存储在单击的列表视图项中的assuranceArray
的值,则需要使用该position
变量。例如,您的解决方案可能如下所示:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String email = assuranceArray.get(position).getEmail()
}
在这里,getEmail()
方法只是我的猜测。您需要使用Assurance
模型类中指定的自己的方法替换它。
修改强>
如果您想在listView项目中添加CheckBox
,然后想知道它是否已被选中,您可以使用以下代码:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String email = assuranceArray.get(position).getEmail();
boolean isChecked = ( (CheckBox) view.findViewById(R.id.checkbox_assurance)).isChecked();
}
但是,您需要记住,每当您向listView项添加一些clickable
视图元素时,它都会使listItem 不可点击。 Checkbox
就是其中一种观点。要解决此问题,您需要将以下属性包含在listItem.xml
的父布局中:
android:descendantFocusability="blocksDescendants"