Java 将SQLite数组应用于滚动活动?

Java 将SQLite数组应用于滚动活动?,java,android,sqlite,listview,android-nestedscrollview,Java,Android,Sqlite,Listview,Android Nestedscrollview,首先,我对大多数Android编码非常不确定,这只是我第二天做的,所以请原谅我在解释中犯的任何错误 我通过SQLite获得了一个数据库,目前通过listView显示我的数据。目前,它可以很好地显示数据,我可以滚动浏览并点击它没有问题。但是,我想将我的数据放在Android Studio中可以找到的“滚动活动”模板中。通过谷歌搜索,我了解到不能将listView放在nestedScrollView中,这正是活动所使用的 但是,我不确定如何在不使用listView的情况下显示数据库中的数据。有人能帮

首先,我对大多数Android编码非常不确定,这只是我第二天做的,所以请原谅我在解释中犯的任何错误

我通过SQLite获得了一个数据库,目前通过listView显示我的数据。目前,它可以很好地显示数据,我可以滚动浏览并点击它没有问题。但是,我想将我的数据放在Android Studio中可以找到的“滚动活动”模板中。通过谷歌搜索,我了解到不能将listView放在nestedScrollView中,这正是活动所使用的

但是,我不确定如何在不使用listView的情况下显示数据库中的数据。有人能帮我把listView转换成兼容的东西吗,或者解释一种将它们结合起来的方法(黑客方法现在还可以!)

我已经在下面显示了所有必要的代码

MainActivity.java:

mydb = new DBHelper(this);
    ArrayList array_list = mydb.getAllCotacts();
    ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);

    obj = (ListView)findViewById(R.id.listView1);
    TextView emptyText = (TextView)findViewById(android.R.id.empty);
    obj.setEmptyView(emptyText);
    obj.setAdapter(arrayAdapter);
    obj.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
            // TODO Auto-generated method stub
            int id_To_Search = arg2 + 1;

            Bundle dataBundle = new Bundle();
            dataBundle.putInt("id", id_To_Search);

            Intent intent = new Intent(getApplicationContext(),DisplayContact.class);

            intent.putExtras(dataBundle);
            startActivity(intent);
        }
    });
mydb=newdbhelper(此);
ArrayList数组_list=mydb.getAllCotacts();
ArrayAdapter ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,array\u list);
obj=(ListView)findViewById(R.id.listView1);
TextView emptyText=(TextView)findViewById(android.R.id.empty);
对象setEmptyView(emptyText);
对象设置适配器(阵列适配器);
obj.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
//TODO自动生成的方法存根
int id_To_Search=arg2+1;
Bundle-dataBundle=新Bundle();
dataBundle.putInt(“id”,id到搜索);
Intent Intent=新的Intent(getApplicationContext(),DisplayContact.class);
意向。额外支出(数据绑定);
星触觉(意向);
}
});
content_scrolling.xml:

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="uk.ac.tees.q5065885.diary.ScrollingActivity"
    tools:showIn="@layout/activity_scrolling">

    <android.support.v7.widget.LinearLayoutCompat
        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_alignParentLeft="true"
        android:layout_alignParentStart="true"
        />
    </android.support.v7.widget.LinearLayoutCompat>

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/emptyText"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
    />



</android.support.v4.widget.NestedScrollView>


为任何错误道歉,正如我所说,这是我的第二天;我正在尽可能快地学习

您可以通过编程方式创建视图,并随时将其添加到LinearLayout。您可以使用任何类型的视图,但在本例中,我想保持清楚,所以我假设您的联系人是字符串,您只想将其显示为文本视图。您可以尝试以下方法:

mydb = new DBHelper(this);
List<String> contacts = mydb.getAllCotacts();
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
for(String data : contacts) {
    //Create a view for an LinearLayout
    TextView textView = new TextView(this);
    //Do whatever you like with a view - add listener, adjust style, add text etc.
    textView.setOnClickListener(...);
    textView.setText(data);
    textView.setGravity(Gravity.CENTER);

    //Add textView to linearLayout
    linearLayout.addView(textView);
}
mydb=newdbhelper(此);
List contacts=mydb.getAllCotacts();
LinearLayout LinearLayout=(LinearLayout)findViewById(R.id.linear\U布局);
用于(字符串数据:联系人){
//为线性布局创建视图
TextView TextView=新的TextView(此);
//用视图做任何你喜欢的事情-添加监听器,调整样式,添加文本等等。
setOnClickListener(…);
textView.setText(数据);
textView.setGravity(Gravity.CENTER);
//将文本视图添加到linearLayout
linearLayout.addView(文本视图);
}
xml(不是很漂亮,如果喜欢,可以自定义:))



对不起,我一直在努力让它工作,但我无法控制它。我想我已经用上面的代码替换了我的代码,但是我不确定现在在XML中放什么?我已经编辑了我的答案。对不起,我应该更详细地解释一下。我已经检查过了,效果很好。问我你是否会遇到任何问题。我已经应用了这个,但我的数组仍然没有显示在屏幕上。我还应该提到,您仍然需要能够单击显示的每个数据片段,以获得该片段的自定义交互。IE:每个显示的名字,你点击它,它会带你到那个人的详细信息。这是否仍然适用于您的版本?谢谢你迄今为止的帮助!“我的数组仍然没有显示在屏幕上。”@Bari-问题是关于SQLite+ListView,而不是动态视图。。。如果您有一个适配器和一个ListView,那么问题中的ArrayAdapter应该可以工作。这就是滚动所需的全部内容。这段代码有什么问题吗?注意:当将SQLite数据库与ListView一起使用时,CursorAdapter优先于ArrayAdapter
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="uk.ac.tees.q5065885.diary.ScrollingActivity"
    tools:showIn="@layout/activity_scrolling">

    <LinearLayout android:layout_height="wrap_content"
              android:layout_width="match_parent"
              android:id="@+id/linear_layout"
              android:orientation="vertical"/>

</android.support.v4.widget.NestedScrollView>