【问题标题】:how can I make two listviews fetching from two sqlite tables in one activity in Android如何在 Android 的一个活动中从两个 sqlite 表中获取两个列表视图
【发布时间】:2013-05-09 06:42:54
【问题描述】:

我有一项活动需要显示从两个 sqlite 表中获取的两个列表。对于每个列表,我都在写customCursorAdapter 任何人都可以指导我找到一个有用的例子,或者只是如何使这种情况成为可能?谢谢。

【问题讨论】:

  • 如果您能够填充一个 List ,您应该能够使用相同/相似的逻辑填充另一个列表。
  • 但必须有一个 ID 为“list”的列表视图,并且一个布局中不能有两个具有相同 ID 的项目。你有什么例子吗?我知道这是可能的,并且想法是自己定义适配器,但我仍在研究如何去做。
  • 不要使用 ListActivity 。使用两个不同的列表视图创建自己的布局。

标签: android android-listview listactivity android-listfragment android-cursoradapter


【解决方案1】:

这是包含两个列表的布局 -

<LinearLayout 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"
android:weightSum="2" >

<ListView
    android:id="@+id/lvOne"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

<ListView
    android:id="@+id/lvTwo"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

</LinearLayout>

你的 class 应该扩展 Activity 类(不是 ListActivity 类)应该类似于这个--

public class TwoListActivity extends Activity {

ListView lvOne ;
ListView lvTwo ;

MyAdapter adapter ; // Initialise your adapter

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_two_list);

    lvOne = (ListView) findViewById(R.id.lvOne);
    lvTwo = (ListView) findViewById(R.id.lvTwo);

    lvOne.setAdapter(/*** Create and set your adapter****/);
    lvTwo.setAdapter(/*** Create and set your adapter****/);

}

希望这会有所帮助。

【讨论】:

  • 我已经使用 ListFragment 解决了它,但您的解决方案也可以。 :)
【解决方案2】:

您应该从 db 中获取数据并将其存储在两个单独的 arrayList 中,然后将每个传递给相应的 listView 适配器(您应该为每个 listView 有单独的适配器实例)

【讨论】:

  • 我认为这个想法是正确的方向,只是无法弄清楚如何让它发挥作用,你有什么例子吗?谢谢你:)
猜你喜欢
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
相关资源
最近更新 更多