Andorid 通过AlertDialog添加listview

如题,不过是在碎片中通过单击按钮弹出dialog 输入信息之后获取生成一个新的listview
可是我的点击监听器不能不妨oncreateview里

还有一个疑问就是 onActivityCreated中的监听器getContext 是指dialog吗?

代码如下

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.preference.DialogPreference;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment ;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class NoteFragment extends Fragment{

List<ChatListView> data = new ArrayList<ChatListView>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup,
                         Bundle savedInstanceState){
    View view=inflater.inflate(R.layout.note_fragment,viewGroup,false);

    for (int i = 0; i < 3; i++) {
        ChatListView chatListView = new ChatListView("1111","2222");
        chatListView.setGood(i+1);
        chatListView.setNo(i);
        data.add(chatListView);
    }
    ListView listview = (ListView)view.findViewById(R.id.listview_layout_notefragment);
    ChatAdapter chatAdapter=new ChatAdapter(getContext(),data);
    listview.setAdapter(chatAdapter);


    return view;
}
Button chat;
Button goodButton;
Button noButton;
@Override
public void onActivityCreated (Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    chat=(Button)getView().findViewById(R.id.note_fragment_button);
    chat.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            AlertDialog.Builder dialog=new AlertDialog.Builder(getContext());
            final View view=View.inflate(getContext(),R.layout.dialog_chat_notefragment,null);
            dialog.setView(view);
            dialog.setCancelable(true);

            dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    EditText editTextTO=(EditText) view.findViewById(R.id.text_dialog_To);
                    String ToText=editTextTO.getText().toString();
                    EditText editTextSay=(EditText)view.findViewById(R.id.text_dialog_say);
                    String SayText=editTextSay.getText().toString();

                    ChatListView chatListView = new ChatListView(ToText,SayText);
                    chatListView.setGood(i+1);
                    chatListView.setNo(i);
                    data.add(chatListView);
                    ListView listview = (ListView)view.findViewById(R.id.listview_layout_notefragment);
                    ChatAdapter chatAdapter=new ChatAdapter(getContext(),data);
                    listview.setAdapter(chatAdapter);

                }
            });
            dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });
            dialog.show();
        }
    });




}

}

1个回答

好啦 我已经开开心心的解决啦 在Dialog里面将数据加到data数组之后 再适配器.notifyDataSetChanged()这个就好啦 动态刷新

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐