12

嘿,我正在尝试为一个看起来像(并且主要表现得像)呼叫日志的列表创建一个设计,如下所示:

替代文字

为此,我下载了源代码并正在研究它以了解实现它的类和 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 返回如下错误:

原因:java.lang.RuntimeException:您的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView

所以在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: 来自 xml 类型布局资源 ID #0x102000a 的文件

所以,我的问题是,为什么会这样?如果你有另一个解决方案来实现这样的视图,我正在尝试。

谢谢。

4

8 回答 8

97

我有同样的问题。我正在设置这样的文本:

statusView.setText(firstVisiblePosition);

问题是firstVisiblePosition是一个整数。编译器没有抱怨。通过修复它

statusView.setText(""+firstVisiblePosition);
于 2011-10-30T16:54:50.040 回答
7

听起来很蹩脚,但如果您使用的是 Eclipse,您是否尝试过清理和重建您的项目?这解决了我遇到的类似问题。

于 2011-08-03T16:37:04.233 回答
6

我还可以通过在使用时将 Integer 值转换为 String 来解决问题:

name.setText(name_value_needs_to_be_string);

其中名称是一个 TextView。

于 2012-02-08T10:12:10.503 回答
4

编译器将您的int参数解释为资源引用并假设您正在调用:

 public final void setText (int resourceId)

但你的意图是打电话

 public final void setText (CharSequence text)

要修复它,请这样做:

myTextView.setText(Integer.toString(myIntegerValue));
于 2013-11-14T09:54:41.857 回答
3

将您的 listView 的 id 设置为

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

我不知道,是否

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

确实工作正常

于 2011-01-11T14:56:24.287 回答
0

我通过将 XML 文件从layout-land目录复制到layout(反之亦然)解决了这个问题。一些设备具有默认横向模式,其他设备具有纵向模式。如果该文件在该模式下不存在,则会引发此异常。

于 2015-07-30T10:29:55.750 回答
0

就我而言,我的 xml 中只有一些 id 不正确。检查布局中的 id

错误参考:

应用程序:layout_constraintBottom_toBottomOf="@id/loginEmail"

真实参考:

应用程序:layout_constraintBottom_toBottomOf="@id/loginEmailEntryCode"

于 2021-03-10T06:05:53.633 回答
0

只需清理重建您的项目。它会起作用的。

于 2022-01-20T06:28:42.083 回答