2016-03-24 21 views
0

Sorunumun xml görüntülerini etkinliğimle düzgün bir şekilde birbirine bağladığına inanıyorum. Ben basit_list_item_1.xml Android söz konusu düzeni adlandırmak için varsayılan yolu olduğunu düşündüm ama belki de bir yere yanlış ...Android Etkinliği ve Düzenleme sorunu

Bu öğretici takip ettik ve şimdi benim kod ve uygulamada hiç hatam yok (talimatlar genel olarak büyük, ama hayal gücüne büyük ölçüde düzenleri bırakır) telefonumda çalışır:

https://guides.codepath.com/android/Loading-Contacts-with-Content-Providers#using-cursorloader-to-query-the-contentprovider

ama benim app çalıştırdığınızda o şuna benzer:

enter image description here

tasarım modunda item_contact.xml şuna benzer:

enter image description here

Alakalı dosyaların benim kesin proje kodunu post ediyorum. Herhangi bir yardım, bunu takdir çok olur (bu soruna bağlantılı olabilir - Uygulamamda altındaki gri dikdörtgenin hafif gölgede o?)

MainActivity.java:

package com.example.chris.contactswithcursorloader; 

import android.support.v4.app.LoaderManager; 
import android.support.v4.content.CursorLoader; 
import android.support.v4.content.Loader; 
import android.database.Cursor; 
import android.provider.ContactsContract; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 


public class MainActivity extends AppCompatActivity { 
    // ... existing code ... 
    private SimpleCursorAdapter adapter; 

    // Defines the asynchronous callback for the contacts data loader 
    private LoaderManager.LoaderCallbacks<Cursor> contactsLoader = 
      new LoaderManager.LoaderCallbacks<Cursor>() { 
       // Create and return the actual cursor loader for the contacts data 
       @Override 
       public Loader<Cursor> onCreateLoader(int id, Bundle args) { 
        // Define the columns to retrieve 
        String[] projectionFields = new String[]{ContactsContract.Contacts._ID, 
          ContactsContract.Contacts.DISPLAY_NAME, 
          ContactsContract.Contacts.PHOTO_URI}; 
        // Construct the loader 
        CursorLoader cursorLoader = new CursorLoader(MainActivity.this, 
          ContactsContract.Contacts.CONTENT_URI, // URI 
          projectionFields, // projection fields 
          null, // the selection criteria 
          null, // the selection args 
          null // the sort order 
        ); 
        // Return the loader for use 
        return cursorLoader; 
       } 

       // When the system finishes retrieving the Cursor through the CursorLoader, 
       // a call to the onLoadFinished() method takes place. 
       @Override 
       public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { 
        // The swapCursor() method assigns the new Cursor to the adapter 
        adapter.swapCursor(cursor); 
       } 

       // This method is triggered when the loader is being reset 
       // and the loader data is no longer available. Called if the data 
       // in the provider changes and the Cursor becomes stale. 
       @Override 
       public void onLoaderReset(Loader<Cursor> loader) { 
        // Clear the Cursor we were using with another call to the swapCursor() 
        adapter.swapCursor(null); 
       } 
      }; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.item_contact); 

     setupCursorAdapter(); 

     // Initialize the loader with a special ID and the defined callbacks from above 
     getSupportLoaderManager().initLoader(CONTACT_LOADER_ID, 
       new Bundle(), contactsLoader); 

     // Find list and bind to adapter 
     ListView lvContacts = (ListView) findViewById(R.id.lvContacts); 

     if(lvContacts != null) { 
      lvContacts.setAdapter(adapter); 
     } 
    } 

    // Create simple cursor adapter to connect the cursor dataset we load with a ListView 
    private void setupCursorAdapter() { 
     // Column data from cursor to bind views from 
     String[] uiBindFrom = { ContactsContract.Contacts.DISPLAY_NAME, 
       ContactsContract.Contacts.PHOTO_URI }; 
     // View IDs which will have the respective column data inserted 
     int[] uiBindTo = { R.id.tvName, R.id.ivImage }; 
     // Create the simple cursor adapter to use for our list 
     // specifying the template to inflate (item_contact), 
     adapter = new SimpleCursorAdapter(
       this, R.layout.item_contact, 
       null, uiBindFrom, uiBindTo, 
       0); 
    } 

    public static final int CONTACT_LOADER_ID = 78; // From docs: A unique identifier for this loader. Can be whatever you want. 

} 

item_contact .xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ListView android:id="@+id/lvContacts" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:layout_weight="1"> 
    </ListView> 

    <!--android:background="#ff3399"--> 

</LinearLayout> 

simple_list_item_1.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView android:id="@+id/tvName" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="TextView"/> 

    <TextView android:id="@+id/ivImage" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="TextView_callnumber"/> 

</LinearLayout> 

AndroidManifest.xml'sinde

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.chris.contactswithcursorloader" > 

    <uses-permission android:name="android.permission.READ_CONTACTS"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

Sadece FYI RecyclerView genellikle ListView üzerinde tercih edilir. –

+0

@AdamNelson Gerçekten değil. Her şey için bu basit geri dönüşümcü daha fazla iş ve fazla meseledir. –

+1

Hiçbir zaman Android Studio'nun "önizleme" penceresine bağlı olarak verilerin yükleneceğini varsaymayın. Bağdaştırıcınızda veri yoksa, boş bir ekran da görürdünüz. –

cevap

2
adapter = new SimpleCursorAdapter(
       this, R.layout.item_contact, 
       null, uiBindFrom, uiBindTo, 
       0); 

düzen öğesinin düzeni değil, faaliyet için ana düzen olmalıdır. Burada simple_list_item1 istiyorum.

+0

Güzel olan. Bu hızlı oldu. Çok hızlı StackOverFlow bana doğru cevabı kabul etmeden önce 5 dakika bekletiyorlar. Bu listeyi softkeyboard ile arama ve metni düzenleme hakkında bilgi almak için bakmak istediğiniz herhangi bir öneri/okuma var mı? – CHarris

İlgili konular