2011-01-11 2 views
12

Hey, j'essaie de créer un design pour une liste qui ressemble (et se comporte principalement comme) le journal des appels, comme montré ici:

alt text

pour cela, je téléchargé le code source et j'étudie pour savoir quelle classe et le fichier xml mettre en œuvre.

Et je trouve ces deux fichiers xml recent_calls_list_item.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" android:layout_height="?android:attr/listPreferredItemHeight" 
android:paddingLeft="7dip"> 

<com.psyhclo.DontPressWithParentImageView 
    android:id="@+id/call_icon" android:layout_width="wrap_content" 
    android:layout_height="match_parent" android:paddingLeft="14dip" 
    android:paddingRight="14dip" android:layout_alignParentRight="true" 

    android:gravity="center_vertical" android:src="@android:drawable/sym_action_call" 
    android:background="@drawable/call_background" /> 

<include layout="@layout/recent_calls_list_item_layout" /> 

et l'autre est recent_calls_list_item_layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android"> 

<View android:id="@+id/divider" 
    android:layout_width="1px" 
    android:layout_height="match_parent" 
    android:layout_marginTop="5dip" 
    android:layout_marginBottom="5dip" 
    android:layout_toLeftOf="@id/call_icon" 
    android:layout_marginLeft="11dip" 
    android:background="@drawable/divider_vertical_dark" 
/> 

<ImageView android:id="@+id/call_type_icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_marginLeft="4dip" 
/> 

<TextView android:id="@+id/date" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toLeftOf="@id/divider" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="8dip" 
    android:layout_marginLeft="10dip" 

    android:textAppearance="?android:attr/textAppearanceSmall" 
    android:singleLine="true" 
/> 

<TextView android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentBottom="true" 
    android:layout_marginLeft="36dip" 
    android:layout_marginRight="5dip" 
    android:layout_alignBaseline="@id/date" 

    android:singleLine="true" 
    android:ellipsize="marquee" 
    android:textAppearance="?android:attr/textAppearanceSmall" 
    android:textStyle="bold" 
/> 

<TextView android:id="@+id/number" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/label" 
    android:layout_toLeftOf="@id/date" 
    android:layout_alignBaseline="@id/label" 
    android:layout_alignWithParentIfMissing="true" 

    android:singleLine="true" 
    android:ellipsize="marquee" 
    android:textAppearance="?android:attr/textAppearanceSmall" 
/> 

<TextView android:id="@+id/line1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_toLeftOf="@id/divider" 
    android:layout_above="@id/date" 
    android:layout_alignWithParentIfMissing="true" 
    android:layout_marginLeft="36dip" 
    android:layout_marginBottom="-10dip" 

    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:singleLine="true" 
    android:ellipsize="marquee" 
    android:gravity="center_vertical" 
/> 

Et mon activité est la suivante:

public class RatedCalls extends ListActivity { 

private static final String LOG_TAG = "RecentCallsList"; 
private TableLayout table; 
private CallDataHelper cdh; 
private TableRow row; 
private TableRow row2; 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.recent_calls_list_item); 
    Log.i(LOG_TAG, "calling from onCreate()"); 

    cdh = new CallDataHelper(this); 
    table = new TableLayout(getApplicationContext()); 
    row = new TableRow(getApplicationContext()); 

    startService(new Intent(this, RatedCallsService.class)); 
    Log.i(LOG_TAG, "Service called."); 
    fillList(); 

} 

public void onResume() { 

    super.onResume(); 
    fillList(); 

} 

public void fillList() { 

    List<String> ratedCalls = new ArrayList<String>(); 
    ratedCalls = this.cdh.selectTopCalls(); 

    setListAdapter(new ArrayAdapter<String>(RatedCalls.this, 
      android.R.id.list, ratedCalls)); 

    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 

    lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

      Toast.makeText(getApplicationContext(), 
        ((TextView) view).getText(), Toast.LENGTH_LONG).show(); 
     } 
    }); 

} 

}

Et puis j'ai essayé de l'exécuter sur l'émulateur Android, puis il l'LogCat a renvoyé une erreur comme ceci:

Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

Donc, à l'intérieur du recent_calls_list_item.xml J'ai déclaré ceci:

<ListView android:id="@android:id/list" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:layout_x="1px" 
    android:layout_y="1px"> 
</ListView> 

Et maintenant, avec ce que je déclare essayé de courir dans l'émulateur, mais le LogCat retourne une autre erreur:

android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x102000a

Alors, ma question est, pourquoi est-ce qui se passe? Et si vous avez une autre solution pour mettre en œuvre une vue comme celle-ci, j'essaie.

Merci.

Répondre

3

définir l'ID de votre listView à

android:id="@id/android:list" 

Je ne sais pas, si

android:id="@android:id/list" 

ne fonctionne correctement

7

Sons boiteux, mais si vous utilisez Eclipse, avez-vous essayé le nettoyage et reconstruire votre projet? Cela a réglé un problème similaire que j'avais.

97

J'ai eu le même problème. Je mettais le texte comme celui-ci:

statusView.setText(firstVisiblePosition); 

Le problème était que firstVisiblePosition était un entier. Le compilateur ne s'est pas plaint.Fixe par

statusView.setText(""+firstVisiblePosition); 
+1

grâce moi avons eu exactement le même problème. Je trouve l'exception vraiment mauvaise, rien n'indiquant vraiment l'erreur. – pdiddy

+0

Celui-ci fonctionne pour moi aussi !!! +1 – Jomoos

+3

Super, devrait être la réponse acceptée. – Derek

6

Je suis en mesure de résoudre le problème en convertissant la valeur entier en chaîne en utilisant:

name.setText(name_value_needs_to_be_string); 

où le nom est un TextView.

4

Le compilateur interprète vos arguments int comme une référence des ressources et suppose que vous appelez:

public final void setText (int resourceId) 

mais votre intention était d'appeler

public final void setText (CharSequence text) 

Pour résoudre ce problème, faire comme si:

myTextView.setText(Integer.toString(myIntegerValue)); 
0

Je résolu ce problème en copiant le fichier XML à partir layout-land répertoire à layout (ou vice versa). Certains appareils ont le mode paysage par défaut, d'autres le portrait. Si ce fichier n'existe pas dans ce mode, il lève cette exception.

Questions connexes