我想要的是,當名稱旁邊的+按鈕被點擊時,聯繫人將被添加到列表調用listDemandAccepted中。如何獲取嵌入在alertDialog中的listView中的按鈕?
首先,我創建了一個簡單的佈局與一個ListView,然後我將商品使用Arrayadapter(帶有一個TextView一個的ImageButton)ListView中。
以下是在單擊按鈕時開始的活動中的代碼。
public void displayContact() {
listeDemandAccepted = new ArrayList<Contact>();
if (listeDemand.size() != 0) {
final AlertDialog.Builder builderSingle = new AlertDialog.Builder(ContactListActivity.this);
final View v1 = (LinearLayout)getLayoutInflater().inflate(R.layout.liste_add_accepted_view, null);
final ContactRequestAdapter arrayRequestAdapter = new ContactRequestAdapter(ContactListActivity.this, listeDemand);
builderSingle.setIcon(R.drawable.user_groupe);
builderSingle.setTitle("Updating list");
builderSingle.setCancelable(true);
builderSingle.setView(v1);
TextView textView = (TextView)v1.findViewById(R.id.name_liste);
textView.setText("New demand by :");
ListView listView = (ListView)v1.findViewById(R.id.liste_demand_add);
listView.setAdapter(arrayRequestAdapter);
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Contact contact = arrayRequestAdapter.getItem(which);
listeDemandAccepted.add(contact);
}
};
builderSingle.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(builderSingle.getContext(), "Updating", Toast.LENGTH_LONG).show();
ArrayList<Contact> listeAll = listeAccepted;
listeAll.addAll(listeDemandAccepted);
listeContact.addAll(listeAll);
}
});
builderSingle.show();
}
}
我嘗試了幾種方法,但到目前爲止沒有成功。並沒有什麼指令後發生的事情:listeDemandAccepted.add(contact);
這裏是contactRequestAdapter代碼(我忘了):
public class ContactRequestAdapter extends ArrayAdapter<Contact> {
public ContactRequestAdapter(Context ctxt, ArrayList<Contact> contacts) {
super(ctxt,0,contacts);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
Contact contact = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.liste_demand_adapter, parent, false);
}
// Lookup view for data population
TextView checkBox = (TextView) convertView.findViewById(R.id.accept_user_checkbox);
// Populate the data into the template view using the data object
checkBox.setText(contact.getName());
// Return the completed view to render on screen
return convertView;
}
}`
你有關於如何幫助我的任何想法?
謝謝
這是我嘗試過的,但不知道如何到達加號按鈕! – Hinko
@Hinko Android設計原則不鼓勵包含重複動作的用戶界面,例如您已定義的用戶界面 - 列表中實際上不需要在每個列表項中都有+按鈕 - 它可能只是一個列表,並且可以添加到其中一個列表中你的商業模式 - 這只是我建議的一部分。 – takrishna