【问题标题】:Listview in Fragment without extends ListFragment片段中的列表视图没有扩展 ListFragment
【发布时间】:2015-06-29 10:27:09
【问题描述】:

扩展 ListFragment 时,我使用 ListView 没问题。但我想要 extends Fragment (不是 ListFragment )。我该怎么做?

public class ListEnployees extends ListFragment {

private Cursor employees;
private MyDatabase db;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    db = new MyDatabase(getActivity());
    employees = db.getEmployees();
    // no more this
    // setContentView(R.layout.list_fruit);

    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,employees));

    ListView listView = getListView();
    listView.setTextFilterEnabled(true);

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // When clicked, show a toast with the TextView text
            Toast.makeText(getApplicationContext(),
            ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
        }
    });

}

}

【问题讨论】:

    标签: android listview android-fragments


    【解决方案1】:

    试试这个,它对我有用!

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
        View rootView = inflater.inflate(R.layout.fragment_lista, container, false);
    
        ListView lst = (ListView) rootView.findViewById(R.id.lst);
        Adapter adapter = //youradapter;
        lst.setAdapter(adapter);
    
        return rootView;
    }
    

    【讨论】:

      【解决方案2】:

      试试这个,

      public class ListEnployees extends Fragment{
      
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) {
      View parentView = inflater.inflate(R.layout.your_fragment_with_list view, container, false);
      ListView list = (ListView) parentView .findViewById(R.id.ur_list);
      Adapter adapter = new Adapter(getActivity(),object you want to pass);
      list.setAdapter(adapter);
      return parentView;
      }
      
      }
      

      【讨论】:

      • 我已经尝试过了,但此代码 setListAdapter(new ArrayAdapter&lt;String&gt;(this, R.layout.list_fruit,employees)); 仅适用于类 extends ListFragment。 如何更改我的代码适用于类 extends Fragment
      【解决方案3】:

      在代码下使用:

        public class MainActivity extends Activity {
      
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
                  ListView lst=(ListView) findViewById(R.id.listView1);
                  lst.setAdapter(...)
              }
          }
      
      
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent" >
      
          <ListView
              android:id="@+id/listView1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true" >
          </ListView>
      
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-03
        • 1970-01-01
        • 2022-08-14
        相关资源
        最近更新 更多