如何获取alertDialog中嵌入的listView中的按钮?

时间:2016-03-24 10:00:01

标签: android android-arrayadapter alertdialog

我想要的是,当点击名称旁边的+按钮时,联系人会被添加到列表调用listDemandAccepted。

Here is what I have now

首先,我使用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;
}

}`

你对如何帮助我有任何想法吗?

谢谢,

2 个答案:

答案 0 :(得分:0)

您可以尝试将onClickListener定义为listview项(在您的情况下是加号按钮),以便能够检测并放置“listeDemandAccepted.add(contact);”在那个方法而不是alogInterface.OnClickListener

答案 1 :(得分:0)

要解决您的问题,我认为您必须向ListView添加arrayRequestAdaper.notifyDataSetChanged(); ,并且在侦听器内部将您的联系人添加到声明的ArrayList,最后不要忘记致电

{{1}}