基于List <items> </items>填充ListView

时间:2014-12-04 12:58:53

标签: android-layout android-fragments android-listview fragment

我是StackOverflow的新手,这是我的第一个问题。 我一直在寻找帮助,但却找不到对我有用的东西。

我正在使用Android Studio 0.8.14,我希望根据我从SQLite获得的项目列表填充ListView,并且每个人都说我应该使用ArrayAdapter,但这不适用于我,有人可以帮帮我吗?

这是我构建项目列表的方式:

&#13;
&#13;
//getting all items
public List < Item > getAllItems() {
  SQLiteDatabase db = this.getReadableDatabase();
  List < Item > items = new ArrayList < Item > ();
  String selectQuery = "SELECT * FROM " + TABLE_ITEM;
  Cursor c = db.rawQuery(selectQuery, null);

  // looping through all rows and adding to list
  if (c.moveToFirst()) {
    do {
      Item it = new Item();
      it.setId(c.getInt(c.getColumnIndex(KEY_ID)));
      it.setName(c.getString(c.getColumnIndex(KEY_NAME)));
      it.setDescription(c.getString(c.getColumnIndex(KEY_DESCRIPTION)));
      it.setPrice(c.getDouble(c.getColumnIndex(KEY_PRICE)));
      it.setAdd_info(c.getString(c.getColumnIndex(KEY_ADD_INFO)));
      it.category_id = c.getInt(c.getColumnIndex(KEY_ITEM_CATEGORY));
      it.vegetarian = c.getInt(c.getColumnIndex(KEY_VEGETARIAN));
      it.sustainable = c.getInt(c.getColumnIndex(KEY_SUSTAINABLE));
      it.vegan = c.getInt(c.getColumnIndex(KEY_VEGAN));
      it.frozen = c.getInt(c.getColumnIndex(KEY_FROZEN));
      it.gluten_free = c.getInt(c.getColumnIndex(KEY_GLUTEN_FREE));
      it.gm_free = c.getInt(c.getColumnIndex(KEY_GM_FREE));

      // adding to item list
      items.add(it);
    } while (c.moveToNext());
  }
  return items;
}
&#13;
&#13;
&#13;

在我的MainActivity.java上,我有这个:

&#13;
&#13;
public class MainActivity extends FragmentActivity {

  private DatabaseHelper db;
  List < String > categoryNames;
  int categoryCount = 0;

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_categories);
    
    db = new DatabaseHelper(this.getApplicationContext());
    categoryNames = db.getAllCategoryNames();
    categoryCount = catNames.size();

    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(adapter);
  }

  public class PagerAdapter extends FragmentPagerAdapter {

    public PagerAdapter(FragmentManager fm) { super(fm); }

    public Fragment getItem(int index) {

      if (index >= 0 && index < categoryCount)
        return new ListTheItems();
      else
        return null;
    }

    public CharSequence getPageTitle(int position) {
      String title = "no title";

      if (position >= 0 && position < categoryCount) {
        title = categoryNames.get(position);
      }

      return title;
    }

    public int getCount() {
      return categoryCount;
    }
  }


}
&#13;
&#13;
&#13;

这是ListTheItems.java,我尝试在ListView中显示项目列表中的数据:

&#13;
&#13;
public class ListTheItems extends Fragment {

  private List < Item > allItems = new ArrayList < Item > ();
  private DatabaseHelper db;
  private ListView listView;

  public void onCreate(Bundle saveInstanceState) {
    setContentView(R.layout.list);

    listView = (ListView) findViewById(R.id.itemList);
    allItems = db.getAllItems();

    ArrayAdapter < String > arrayAdapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1,
      allItems);

    listView.setAdapter(arrayAdapter);
  }
}
&#13;
&#13;
&#13;

我使用的是Fragments,但是在ListTheItems.java中,我在方法&#39; setContentView&#39; ,&#39; findViewById&#39;并且无法解决“ArrayAdapter”问题。构造

2 个答案:

答案 0 :(得分:0)

将视图层次结构加载到片段中的方式与将其加载到Activity中的方式相比。试试这个:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout. list, container, false);
    // You have to call findViewById on rootView, since the fragment doesn't know
    // what your views are until you return rootView from this function
    listView = (ListView) rootView.findViewById(R.id.itemList);

    allItems = db.getAllItems();
    Activity activity = (Activity) container.getContext();
    ArrayAdapter < Item > arrayAdapter = new ArrayAdapter < Item > (activity, android.R.layout.simple_list_item_1,
      allItems);
    listView.setAdapter(arrayAdapter);
    return rootView;
}

编辑:

抱歉,我错过了类型差异。您获得构造函数错误的原因是List参数的模板类型需要与ArrayAdapter的模板类型匹配。我修复了上面的代码,将arrayAdapter的类型更改为ArrayAdapter&lt;项目&gt;匹配allItems的类型,它是List&lt;项目&gt;。

答案 1 :(得分:0)

最后,我的问题的解决方案是创建我自己的适配器,并基于@Bruce的帮助更改MainActivity.java,如下所示:

&#13;
&#13;
public Fragment getItem(int index) {
  if (index >= 0 && index < categoryCount) {
    ListItemsFragment lif = new ListItemsFragment();
    lif.allItems = db.getAllItemsByCategory(index + 1);
    return lif;
  } else
    return null;
}
&#13;
&#13;
&#13;

并像这样更改ListTheItems.java:

&#13;
&#13;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

  View rootView = inflater.inflate(R.layout.list, container, false);
  listView = (ListView) rootView.findViewById(android.R.id.list);
  MyAdapter adapter = new MyAdapter(this.getActivity(), allItems);
  listView.setAdapter(adapter);
  return rootView;
}
&#13;
&#13;
&#13;