2011-01-11 28 views
12

Xin chào, tôi đang cố gắng tạo một thiết kế cho danh sách trông giống như (và hầu hết hoạt động như) nhật ký cuộc gọi, như đưa ra ở đây:

alt text

đối với điều này tôi đã tải về mã nguồn và tôi đang học nó cho biết những gì lớp và file xml thực hiện nó.

Và Tôi thấy điều này hai tập tin 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" /> 

và thứ hai là 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" 
/> 

Và hoạt động của tôi là thế này:

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

} 

}

Và sau đó tôi đã cố gắng để chạy nó trên giả lập android, và sau đó nó trở thành LogCat trả về một lỗi như thế này:

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

Vì vậy, bên trong recent_calls_list_item.xml Tôi đã tuyên bố điều này:

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

Và bây giờ với điều này tuyên bố tôi đã cố gắng để chạy trong giả lập, nhưng LogCat trả về một lỗi:

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

Vì vậy, câu hỏi của tôi là, tại sao điều này xảy ra? Và nếu bạn có một giải pháp khác để thực hiện một cái nhìn như thế này tôi đang cố gắng.

Cảm ơn.

Trả lời

3

thiết lập id của ListView của bạn để

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

Tôi không biết, cho dù

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

không hoạt động chính xác

7

Âm thanh què nhưng nếu bạn đang sử dụng Eclipse, bạn đã cố gắng làm sạch và xây dựng lại dự án của bạn? Điều đó đã khắc phục được một vấn đề tương tự mà tôi đang gặp phải.

97

Tôi gặp vấn đề tương tự. Tôi đã thiết lập các văn bản như thế này:

statusView.setText(firstVisiblePosition); 

Vấn đề là firstVisiblePosition là một số nguyên. Trình biên dịch không phàn nàn.Cố định nó bằng cách

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

cảm ơn tôi đã có cùng một vấn đề chính xác. Tôi thấy ngoại lệ thực sự xấu, không có gì thực sự chỉ ra lỗi. – pdiddy

+0

Điều này cũng phù hợp với tôi !!! +1 – Jomoos

+3

Tuyệt vời, phải là câu trả lời được chấp nhận. – Derek

6

Tôi cũng có thể giải quyết vấn đề bằng cách chuyển đổi các giá trị Integer để String trong khi sử dụng:

name.setText(name_value_needs_to_be_string); 

nơi tên là một TextView.

4

Trình biên dịch dịch int luận của bạn như là một tài liệu tham khảo tài nguyên và giả định bạn đang gọi:

public final void setText (int resourceId) 

nhưng bạn đã định gọi

public final void setText (CharSequence text) 

Để khắc phục nó, làm như vậy:

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

Tôi đã giải quyết sự cố này bằng cách sao chép tệp XML từ layout-land thư mục đến layout (hoặc ngược lại). Một số thiết bị có chế độ nằm ngang mặc định, các thiết bị khác có hình chân dung. Nếu tệp đó không tồn tại trong chế độ đó, nó sẽ ném ngoại lệ này.

Các vấn đề liên quan