我有一个自定义的对话片段搜索结果,我有一个TextView与ListView的标题应该在哪里。
<?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;
}
传递这里的所有代码
@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);
}
现在一切都好了