Android ListView没有';不要在碎片中填充
我的自定义适配器不填充片段中的ListView。它不会抛出任何异常,也不会填充 ArrayList不是空的。我通过打印来检查 我的片段:Android ListView没有';不要在碎片中填充,android,android-layout,listview,android-fragments,android-listview,Android,Android Layout,Listview,Android Fragments,Android Listview,我的自定义适配器不填充片段中的ListView。它不会抛出任何异常,也不会填充 ArrayList不是空的。我通过打印来检查 我的片段: public class NewsfeedTabActivity extends Fragment { View rootView; private ArrayList<ActivityTable> activitiesList = new ArrayList<ActivityTable>(); @O
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);
}
}
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;
}
}
公共类NewsfeedTabActivity扩展片段{
视图根视图;
private ArrayList activitiesList=新建ArrayList();
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图v=充气机。充气(R.layout.activity\u newsfeed\u选项卡,容器,false);
context=container.getContext();
rootView=充气机。充气(R.layout.activity\u newsfeed\u选项卡,容器,false);
刷新新闻提要();
}
私有新闻提要(){
Log.e(“检查”、“HEY1”);
ListView新闻短片=(ListView)rootView.findViewById(R.id.newsfeed_list);//ListView
FeedAdapter=新的FeedAdapter(getActivity(),activitiesList);//适配器
设置适配器(适配器);
Log.e(“CHECK”、“HEY2”);
//日志成功地记录了HEY1和HEY2
}
已创建ActivityState上的公共无效(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
}
在上面的示例中,成功记录了HEY1和HEY2
适配器:
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);
}
}
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;
}
}
公共类FeedAdapter扩展了BaseAdapter{
私人活动;
专用静态充气机=空;
ArrayList actList=新的ArrayList();
公共FeedAdapter(活动a,ArrayList actList){
活动=a;
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
this.actList=actList;
日志d(“检查”、“工作1”);
}
public int getCount(){
返回actList.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
日志d(“检查”、“工作2”);
视图vi=转换视图;
ActivityTable act=actList.get(位置);
日志d(“检查”、“工作3”);
if(convertView==null){
vi=充气机充气(R.layout.feed_single_text,空);
}
TextView caption=(TextView)vi.findViewById(R.id.postcaption);//caption
//在listview中设置所有值
caption.setText(act.getContent());
返回vi;
}
}
在适配器中,构造函数中只记录“WORKS1”,而应用程序不记录“WORKS2”和“WORKS3”。我想它中途停了?我做错了什么,伙计们?改变这一点:
public Object getItem(int position) {
return position;
}
致:
我通常会将
对象
返回更改为正在返回的类型,在您的情况下是活动表
。这也是一件好事,因为以后调用getItem
时不必强制转换它,因为您的代码是正确的,而不是使全局根视图对象使ListView
成为全局对象,并检查列表是否为空
public class NewsfeedTabActivity extends Fragment {
private ListView newsreel;
private ArrayList<ActivityTable> activitiesList = new ArrayList<ActivityTable>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_newsfeed_tab, container, false);
newsreel = (ListView) rootView.findViewById(R.id.newsfeed_list); //the listview
RefreshNewsFeed();
return rootView;
}
private void RefreshNewsFeed() {
Log.e("CHECK", "HEY1");
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);
}
}
公共类NewsfeedTabActivity扩展片段{
私有ListView新闻短片;
private ArrayList activitiesList=新建ArrayList();
@凌驾
创建视图上的公共视图(布局充气机、视图组容器、捆绑包保存状态){
View rootView=充气机。充气(R.layout.activity\u newsfeed\u选项卡,容器,false);
Newsleel=(ListView)rootView.findViewById(R.id.newsfeed_list);//ListView
刷新新闻提要();
返回rootView;
}
私有新闻提要(){
Log.e(“检查”、“HEY1”);
FeedAdapter=新的FeedAdapter(getActivity(),activitiesList);//适配器
设置适配器(适配器);
Log.e(“CHECK”、“HEY2”);
//日志成功地记录了HEY1和HEY2
}
已创建ActivityState上的公共无效(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
}
在**Log.d(“检查”、“工作1”)之后打印actList大小;**检查一下。我肯定会是0,它有16项,伙计。我刚查了一下@CapDroidI,我也在想为什么我的问题会被否决?也许你们分享了一半的代码,请分享完整的代码。我无法找到您在其中填充数据的代码activitiesList@CapDroid假设它是完全的伴侣。这是一个很长的代码,我检查了列表是否为空:)我对此寄予厚望,但没有,伙计。仍然没有到达适配器的末端:/