嘿,我試圖創建一個設計列表,看起來像(和大多表現)像通話記錄,像如下圖所示:
爲此,我下載的源代碼,我學習它知道什麼類和XML文件執行。
而且我發現這兩個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" />
,另一個是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"
/>
我的活動是這樣的:
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();
}
});
}
}
然後我嘗試在Android模擬器中運行它,然後它的logcat返回這樣的錯誤:
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
所以在recent_calls_list_item.xml我宣稱這個:
<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>
現在有了這個聲明我試圖在模擬器中運行,但logcat的返回另一個錯誤:
android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x102000a
所以,我的問題是,爲什麼會出現這種情況?如果你有另一種解決方案來實現這樣的視圖,我正在嘗試。
感謝。
謝謝我有同樣的確切問題。我發現這個異常真的很糟糕,沒有任何真正的錯誤。 – pdiddy 2011-11-30 05:36:41