DialogFragment不会从布局中膨胀所有内容

时间:2015-05-18 23:55:40

标签: android user-interface listview android-listview

我有一个搜索结果的自定义对话框片段,我有一个带有ListView的TextView,标题应该是。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/res"
        android:textStyle="bold"
        android:textSize="36sp"
        android:textColor="@color/button"
        />
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list" />
</LinearLayout>

这是onCreate对话

 @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = (View)inflater.inflate(R.layout.activity_result,null);

然而它没有显示TextView,即使ListView在那里并且完全由自定义适配器填充。

类JSONAdapter扩展BaseAdapter实现ListAdapter {

private final Activity activity;
private final JSONArray jsonArray;
private JSONAdapter(Activity activity, JSONArray jsonArray) {
    assert activity != null;
    assert jsonArray != null;

    this.jsonArray = jsonArray;
    this.activity = activity;
}


@Override public int getCount() {
    return jsonArray.length();
}

@Override public JSONObject getItem(int position) {

    return jsonArray.optJSONObject(position);
}

@Override public long getItemId(int position) {
    JSONObject jsonObject = getItem(position);
    return jsonObject.optLong("id");
}

@Override public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null)
        convertView = activity.getLayoutInflater().inflate(R.layout.simple_list_item, parent,false);
    Double score= 0.0;
    JSONObject jsonObject = getItem(position);
    TextView firstline= (TextView) convertView.findViewById(R.id.firstLine);
    TextView secondline = (TextView) convertView.findViewById(R.id.secondLine);
    TextView confidence = (TextView) convertView.findViewById(R.id.score);
    try {
        firstline.setText(jsonObject.getString("title"));
    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {
        score= Double.valueOf(jsonObject.getString("score"));
        confidence.setText(jsonObject.getString("score"));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    try {
        secondline.setText(jsonObject.getString("text"));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    if (score<=0.1)
    {
        convertView.setBackgroundColor(getResources().getColor(R.color.red));
    }
    if (score>0.1)
    {
        convertView.setBackgroundColor(getResources().getColor(R.color.orangish));
    }
    if (score>=0.3)
    {
        convertView.setBackgroundColor(getResources().getColor(R.color.yellow));
    }
    if (score>=0.5)
    {
        convertView.setBackgroundColor(getResources().getColor(R.color.green));
    }

    return convertView;
}
}

为什么会这样?

更新

我也有这个方法

 public static SearchDialogFragment newInstance(String title) {


    SearchDialogFragment frag = new SearchDialogFragment();

    Bundle args = new Bundle();

    args.putString("json", title);

    frag.setArguments(args);

    return frag;

}

1 个答案:

答案 0 :(得分:0)

传递所有代码

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = (View)inflater.inflate(R.layout.activity_result,null);

到这样的oncreateView()

@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.activity_result, container,false);
}

现在一切都很好