【问题标题】:Add data to a listview on fragment将数据添加到片段上的列表视图
【发布时间】:2012-12-04 12:34:58
【问题描述】:

我有一个关于活动的片段,上面有一个列表视图元素。无法将数据添加到列表视图。谁能告诉我怎么了?

public class MainActivity extends FragmentActivity {

    public String[] listS ={"item1","item2","item3"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        MainFragment f = new MainFragment();
        ft.replace(R.id.fragcontainer, f);
        ft.commit();    
        //here the problem occurs
        ListView lv = (ListView)findViewById(R.id.listf);
        lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row,R.id.labeld, listS));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

片段的类:

public class MainFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sis){
        return inflater.inflate(R.layout.fragment_layout, container, false);
    }

}

【问题讨论】:

  • 您的listf 元素在哪里?在activity_main.xml ?
  • 不,在片段的布局中...
  • 使用 f.getView().findViewById();

标签: android android-listview android-fragmentactivity


【解决方案1】:

此方法存在许多问题,但其根源在于 FragmentTransaction replace() 添加片段的方法,我假设该片段包含您要查找的 ListView 是异步的过程。该片段不会立即添加到 Activity 的视图层次结构中,而是稍后添加。

如果您想在Fragment 中向ListView 添加内容,为什么不从MainFragment 子类的onCreateView() 方法中进行呢?这就是它的预期工作方式。

编辑:典型的onCreateView()

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_layout, container, false);
    ListView lv = (ListView) view.findViewById(R.id.listf);
    lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row,R.id.labeld, listS));             
    return view;
}

【讨论】:

  • 已经尝试过了,但我无法在 MainFragment 子类中获得对 ListView 的引用
  • 这是为什么呢?如何在 MainFragment 中创建布局,以使您无法获得对该布局中某些内容的引用?
  • 当我尝试将“ListView lv = (ListView)findViewById(R.id.listf) 添加到 MainFragment 时,会强调 findViewById 函数(我在 Eclipse IDE 中工作)...
  • 我也在做同样的事情,但是我得到了这个错误:“方法 findViewById(int) is undefined for the type MainFragment”
  • 你做的不一样。我从view 变量中调用findViewById()
【解决方案2】:

当你添加代码来定义你的类时,

public class MainFragment extends Fragment {
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sis) {
            View view = inflater.inflate(R.layout.my_fragment, container, false);
            ListView listView = (ListView) view.findViewById(R.id.list);
            return view;
        }
    }

【讨论】:

    【解决方案3】:

    问题是您正在询问该 ListView 的活动

    “这个。”findViewById(...

    您可以向 Fragment 请求视图,或在 Fragment 类中管理 listView 的内容。

    【讨论】:

      【解决方案4】:

      请使用如下设计窗口中的列表视图。我遇到了确切的问题,现在我可以在源代码中找到视图。

      主要问题是 IDE 在 ID 前面附加了“android”

      <ListView
          android:id="@+id/list"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" >
      </ListView>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-13
        • 1970-01-01
        相关资源
        最近更新 更多