【发布时间】:2015-07-16 06:47:32
【问题描述】:
我的自定义适配器没有填充片段中的 ListView。它不会抛出任何异常,也不会被填充。
ArrayList 不为空。我通过打印它们进行了检查。
我的片段:
public class NewsfeedTabActivity extends Fragment {
View rootView;
private ArrayList<ActivityTable> activitiesList = new ArrayList<ActivityTable>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_newsfeed_tab, container, false);
context = container.getContext();
rootView = inflater.inflate(R.layout.activity_newsfeed_tab, container, false);
RefreshNewsFeed();
}
private void RefreshNewsFeed() {
Log.e("CHECK", "HEY1");
ListView newsreel = (ListView) rootView.findViewById(R.id.newsfeed_list); //the listview
FeedAdapter adapter = new FeedAdapter(getActivity(), activitiesList); //the adapter
newsreel.setAdapter(adapter);
Log.e("CHECK", "HEY2");
//The log successfully logs the HEY1 and HEY2
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
}
在上述示例中,HEY1 和 HEY2 已成功登录
适配器:
public class FeedAdapter extends BaseAdapter {
private Activity activity;
private static LayoutInflater inflater = null;
ArrayList<ActivityTable> actList = new ArrayList<ActivityTable>();
public FeedAdapter(Activity a, ArrayList<ActivityTable> actList) {
activity = a;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.actList = actList;
Log.d("CHECK", "WORKS1");
}
public int getCount() {
return actList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("CHECK", "WORKS2");
View vi = convertView;
ActivityTable act = actList.get(position);
Log.d("CHECK", "WORKS3");
if (convertView == null) {
vi = inflater.inflate(R.layout.feed_single_text, null);
}
TextView caption = (TextView) vi.findViewById(R.id.postcaption); // caption
// Setting all values in listview
caption.setText(act.getContent());
return vi;
}
}
在适配器中,只有“WORKS1”被记录在构造函数中,而应用程序不记录“WORKS2”和“WORKS3”。我想它会中途停止?我做错了什么?
【问题讨论】:
-
在 ** Log.d("CHECK", "WORKS1");** 之后打印 actList 大小并检查。我确定它会是 0
-
它有 16 个项目。我刚刚检查了@CapDroid
-
我也想知道为什么有人对我的问题投了反对票?
-
可能是你分享了一半代码,请分享完整代码。我无法找到您将数据填充到 activitiesList 的代码
-
@CapDroid 假设它是完全伴侣。这是一个很长的代码,我检查了列表是否为空:)
标签: android android-layout listview android-fragments android-listview