2016-03-24 11 views
0

私が望むものは、名前の隣にある+ボタンをクリックすると、連絡先がlist call listDemandAcceptedに追加されるということです。alertDialogに埋め込まれたlistViewのボタンを取得する方法は?

Here is what I have now

まず、私は、私はArrayadapter(のTextViewとのImageButton)を使用してリストビューにアイテムを追加し、リストビューでシンプルなレイアウトを作成しました。

ボタンがクリックされたときに開始されるアクティビティのコードです。

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; 
} 

} `

あなたは私を助けるためにどのように上の任意のアイデアを持っていますか?

はあなたが検出して置くことができるようにlistview項目(あなたのケースで、それはプラスボタンです)にonClickListenerを定義しようとすることができ、

答えて

0

ありがとう「listeDemandAccepted.add(contact)を。」その方法ではalogInterface.OnClickListenerの代わりに。あなたの問題を解決するために

+0

これは私が試したことですが、プラスボタンに到達する方法を知らなかったのです! – Hinko

+0

@Hinko Androidデザインの原則は、あなたが定義したような反復アクションを含むUIを嫌っています。リストは実際にはすべてのリストアイテムに+ボタンを持つ必要はありません。あなたのビジネスモデル - それは私の提案の一部です。 – takrishna

0

、私はあなたのListViewにonItemClickListenerを追加することが必要であることを考えると、リスナーの内部で宣言され、最終的にはArrayListに自分の連絡先を追加、

arrayRequestAdaper.notifyDataSetChanged(); 
を呼び出すこと
を忘れないでください
関連する問題