2017-02-19 5 views
0

첫 번째로, 안드로이드 코딩의 대다수에 대해 매우 확신하지 못하고 있습니다. 이것은 단지 두 번째 날이므로, 설명에서 실수를 저 지르십시오.스크롤링 작업에 SQLite 배열 적용?

SQLite를 통해 데이터베이스가 있고 현재 listView를 통해 데이터를 표시하고 있습니다. 현재, 그것은 데이터를 표시 잘 작동하고 스크롤하고 아무 문제도 그것을 클릭 할 수 있습니다. 그러나 Android Studio에서 찾을 수있는 '스크롤링 활동'템플릿에 내 데이터를 배치하고 싶습니다. Googling around에서 나는 activity가 사용하는 nestedScrollView에 listView를 배치 할 수 없다는 것을 이해합니다.

그러나 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); 
     } 
    }); 

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

(당신이 :) 좋아하면 그것을 사용자 정의, 아주 예쁜되지 않음)

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 : 당신은 이런 식으로 뭔가를 시도 할 수 있습니다. 이것이 스크롤링에 필요한 전부입니다. 이 코드의 문제점은 무엇입니까? –

+0

참고 : ListView가있는 SQLite 데이터베이스를 사용하는 경우 ArrayAdapter보다 CursorAdapter가 좋습니다. –

답변

0

뷰를 프로그래밍 방식으로 생성하여 실시간으로 LinearLayout에 추가 할 수 있습니다. 모든 종류의보기를 사용할 수 있지만,이 예에서는 연락처를 String으로 가정하고 TextViews로 표시하려고한다고 가정합니다. 당신은 어댑터와 목록보기가

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