2015-05-18 69 views
0

的一切我有一個自定義對話框片段的搜索結果,我有一個TextView與ListView標題應該在哪裏。DialogFragment不會誇大從佈局

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

這究竟是爲什麼?

UPDATE:

我也有這個方法

public static SearchDialogFragment newInstance(String title) { 


    SearchDialogFragment frag = new SearchDialogFragment(); 

    Bundle args = new Bundle(); 

    args.putString("json", title); 

    frag.setArguments(args); 

    return frag; 

} 

回答

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

現在一切都應該好了

+0

不行,不能resove方法膨脹 – KameeCoding

+0

什麼方法inflater.inflate ??如果代碼中的膨脹線下有其他代碼,則將它們複製並放在'onViewCreated()'中,並使用'getView()'作爲父'View' @Kameegaming – Elltz

+0

@Kameegaming – Elltz