【发布时间】:2015-11-25 14:13:17
【问题描述】:
我正在尝试在片段中显示列表视图。但是,没有显示任何内容,并且日志记录似乎正确。屏幕完全空白。我认为原因是放大视图时出错,但我不知道。我研究了 SO 和其他网站,但没有运气。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.v("FeaturesFragment", "FeaturesFragment");
//View v = inflater.inflate(R.layout.carfeatures_list, container, false);
//View v = inflater.inflate(R.layout.features_list_item, container, false);
View v = inflater.inflate(R.layout.features_list_item, null);
//View v = inflater.inflate(R.layout.carfeatures_list,null);
ListView lv = (ListView) v.findViewById(android.R.id.list);
Log.v("ListView lv1-features", lv.toString());
adapter = new CustomListViewAdapter(carFeaturesList, R.layout.carfeatures_list, super.getActivity());
//adapter = new CustomListViewAdapter(carFeaturesList, R.layout.features_list_item, super.getActivity());
lv.setAdapter(adapter);
Log.v("ListView lv2-features", lv.toString());
adapter.notifyDataSetChanged();
//Listview on item click listener
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Gets values from selected ListItem
String car_features = ((TextView) view.findViewById(R.id.car_features)).getText().toString();
//Log.v("onCreate-car_features", car_features.toString());
String carfeatures_id = ((TextView) view.findViewById(R.id.carfeatures_id)).getText().toString();
//Log.v("onCreate-carfeatures_id", carfeatures_id.toString());
String model_id = ((TextView) view.findViewById(R.id.model_id)).getText().toString();
//Log.v("onCreate-model_id", model_id.toString());
String carfeatures_desc = ((TextView) view.findViewById(R.id.carfeatures_desc)).getText().toString();
//Log.v("onCreate-carfeats_desc", carfeatures_desc.toString());
Boolean b = Boolean.valueOf(TAG_CARID);
if (b == true) {
view.setBackgroundColor(Color.GREEN);
}
//Colors the selected listview item
if (previouslySelectedItem != null) {
previouslySelectedItem.setBackgroundColor(Color.TRANSPARENT);
//Log.v("previouslySelectedItem", "!=null");
//Log.v("position", Integer.toString(position));
}
view.setBackgroundColor(Color.GREEN);
//Log.v("position", Integer.toString(position));
//Log.v("outofif-view", view.toString());
previouslySelectedItem = view;
}
});
//Calls async task to get json
new GetCarFeatures().execute();
Log.v("return called-Features", "return called");
Log.v("v", v.toString());
return v;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
CarFeatures item = getItem(position);
//Log.v("CarFeaturesitem", Integer.toString(position));
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater mInflater = LayoutInflater.from(getContext());
convertView = mInflater.inflate(R.layout.carfeatures_list, null);
//convertView = mInflater.inflate(R.layout.features_list_item, null);
Log.v("getView", "getView");
Log.v("View convertView", convertView.toString());
Log.v("ViewGroup parent", parent.toString());
holder.carfeatures_id = (TextView) convertView.findViewById(R.id.carfeatures_id);
//Log.v("if-holder.carfeat_id", holder.carfeatures_id.toString());
holder.model_id = (TextView) convertView.findViewById(R.id.model_id);
//Log.v("if-holder.model_id", holder.model_id.toString());
holder.carfeatures_desc = (TextView) convertView.findViewById(R.id.carfeatures_desc);
//Log.v("if-holder.carfeat_id", holder.carfeatures_desc.toString());
convertView.setTag(holder);
//Log.v("if-convertView", convertView.toString());
//Log.v("if-holder", holder.toString());
} else {
holder = (ViewHolder) convertView.getTag();
//Log.v("else-convertView", convertView.toString());
//Log.v("else-holder", holder.toString());
}
holder.carfeatures_id.setText(item.carfeatures);
//Log.v("holder.carfeatures_id", holder.carfeatures_id.toString());
holder.model_id.setText(item.modelid);
//Log.v("holder.model_id", holder.model_id.toString());
holder.carfeatures_desc.setText(item.carfeaturesdesc);
//Log.v("holder.carfeatures_desc", holder.carfeatures_desc.toString());
adapter.getItemId(position);
//Log.v("getitemid1", Long.toString(adapter.getItemId(position)));
Log.v("convertView", convertView.toString());
return convertView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:choiceMode="singleChoice"
android:layout_gravity="center_horizontal|top"
android:layout_alignParentStart="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/car_features"
android:layout_below="@+id/model_id"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/carfeatures_id"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/car_features" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/model_id"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/carfeatures_desc"
android:layout_above="@+id/car_features"
android:layout_alignParentStart="true" />
</RelativeLayout>
【问题讨论】:
-
您的 XML 可能配置错误。比如你有
android:layout_below="@+id/header,这里没有header ID。您也不需要对未添加的任何 id 进行加号。 -
有关未来的问题:您能尝试仅显示相关代码吗?很难弄清楚问题到底是哪一行,但在许多情况下,您可以将其分解为几行代码。如果你只是复制这么多代码,那么只有少数人会完整地阅读你的问题,而你得到的答案就会更少。
-
首先你的 ListView 有一个 match_parent 高度并且父布局(RelativeLayout)有一个 wrap_content 高度,如果是你的活动的根,你至少可以设置相对视图的高度来匹配父级/fragment 并且如果片段设置在具有匹配父级作为高度的视图上
-
为了消除歧义,您应该更新您的问题并记下给定的 XML 布局,以便我们知道它是否代表在片段或适配器中膨胀的布局:)
-
@Cata 已更新。被膨胀的布局被记录下来。
标签: android android-fragments android-listview