您好我正在尝试将列表视图添加到警报对话框中。单击第一个警报对话框上的“确定”按钮,程序将指向响应片段,该片段包含需要使用列表视图填充的第二个对话框。我遇到的问题是我得到一个空的警告框,列表视图显示在片段后面
public class ResponseFragment extends Fragment {
ListView list;
private String urlString;
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_response, container, false);
list=(ListView)v.findViewById(R.id.listviewResp);
urlString = "http://172.20.10.5:1012/easyQ.svc/rest/reasons";
new getReasons().execute(urlString);
return v;
}
private class getReasons extends AsyncTask<String, Void, String> {
protected String doInBackground(String... strings) {
String stream;
String urlString = strings[0];
HTTPDataHandler hh = new HTTPDataHandler();
stream = hh.GetHTTPData(urlString);
// Return the data from specified url
System.out.println(stream);
return stream;
}
protected void onPostExecute(String stream) {
if (stream != null)
{
try
{
JSONObject object = new JSONObject(stream);
JSONArray array = object.getJSONArray("reasonsResult");
for(int i = 0 ; i < array.length(); i++) {
JSONObject reasonObj = array.getJSONObject(i);
String ID = reasonObj.getString("reason_leaving_id");
String reason = reasonObj.getString("description");
HashMap<String, String> map = new HashMap<String, String>();
map.put("description", reason);
map.put("id", ID);
oslist.add(map);
}
for(int i = 0; i < oslist.size(); i++)
{
ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
R.layout.dialog_list,
new String[] { "description"}, new int[] {
R.id.txtResp});
// alertDialog.setView(inflater.inflate(R.layout.fragment_response,null));
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String branchId = oslist.get(+position).get("id");
final SessionV globalVariable = (SessionV) getActivity().getApplicationContext();
globalVariable.setBranchId(branchId);
Fragment fragment = null;
fragment = new HomeFragment();
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container_body, fragment).commit();
}
});
}
AlertDialog alertDialog = new AlertDialog.Builder(
getActivity()).create();
alertDialog.setTitle("easyQ");
alertDialog.setMessage("reason");
LayoutInflater inflater = (getActivity()).getLayoutInflater();
final View dialogView=inflater.inflate(R.layout.fragment_response,null);
alertDialog.setView(dialogView);
alertDialog.show();
alertDialog.show();
}
catch(JSONException e)
{
e.printStackTrace();
}
}
}
}
}
&#13;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listviewResp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="15dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtResp"
android:layout_width="match_parent"
android:layout_height="?listPreferredItemHeightSmall"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:gravity="center_vertical"
android:paddingLeft="?listPreferredItemPaddingLeft"
android:paddingRight="?listPreferredItemPaddingRight"
android:textAppearance="?textAppearanceListItemSmall" />
</LinearLayout>
答案 0 :(得分:0)
我想你想在ListView
内显示AlertDialog
。所以,你需要修改一些代码。
第一名:
您从fragment_response
布局中扩展列表视图。这没有必要。在onCreateView()
方法中,只需初始化ListView
的新对象。
替换:
list=(ListView)v.findViewById(R.id.listviewResp);
通过
list = new ListView(getActivity());
第二名:
更改此代码:
AlertDialog alertDialog = new AlertDialog.Builder(
getActivity()).create();
alertDialog.setTitle("easyQ");
alertDialog.setMessage("reason");
LayoutInflater inflater = (getActivity()).getLayoutInflater();
final View dialogView=inflater.inflate(R.layout.fragment_response,null);
alertDialog.setView(dialogView);
alertDialog.show();
alertDialog.show();
通过
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
alertDialog.setTitle("easyQ");
alertDialog.setMessage("reason");
alertDialog.setView(listView);
alertDialog.show();
如果其他代码没问题,那么我希望它能运作良好。