2017-02-19 3 views
0

まず、Androidのコーディングの大部分は非常にわかりませんが、これは私の2日目のことですので、私の説明で間違いをお許しください!スクロールアクティビティにSQLite配列を適用しますか?

私はSQLite経由でデータベースを取得していますが、現在はlistView経由でデータを表示しています。現在、それはデータを表示するうまく動作し、私はそれをスクロールしてクリックすることができます。しかし、私はAndroid Studioで見つけることができる "スクロールアクティビティ"テンプレート内に自分のデータを配置したいと思います。 Googlingから、私はあなたがlistViewをnestedScrollViewに置くことができないことを幾分理解しています。これはアクティビティが使用するものです。

ただし、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); 
     } 
    }); 

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> 

謝罪が、これは私の二日目です。私はできるだけ早く学びます!

+0

あなたは、アダプタとリストビューを持っている:あなたはこのような何かを試すことができます。それがスクロールに必要なすべてです。このコードの問題は何ですか? –

+0

注:ListViewでSQLiteデータベースを使用している場合は、ArrayAdapterよりCursorAdapterが優先されます。 –

答えて

0

プログラムでビューを作成し、その場でLinearLayoutに追加することができます。どのような種類のビューも使用できますが、この例では連絡先を文字列とみなし、TextViewsとして表示したいと仮定しています。

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); 
} 

XML(あなたは:)好きなら、それをカスタマイズし、非常にきれいではありません)

<?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> 
+0

申し訳ありませんが、私はこれを実行しようとしていますが、私はそれを取ることができません。私は私のコードを上記のコードに置き換えたと思いますが、今私はXMLに何を入れるべきか分かりません。 – joshcawthorne

+0

私は自分の答えを編集しました。申し訳ありませんが、私はより完全にそれを説明していたはずです。私はそれをチェックして正常に動作しました。あなたが問題に直面するかどうか私に尋ねなさい。 – Bari

+0

これを適用しても、配列はまだ画面内に表示されません。また、作品のカスタムインタラクションを得るために表示された各データをクリックする必要があることにも言及しておく必要があります。 IE:各名前が表示され、それをクリックすると、その人の詳細が表示されます。これはあなたのバージョンでも動作しますか?あなたのご協力ありがとうございました! – joshcawthorne

関連する問題