12

ねえ、ここに示すように、通話ログのように見える (そしてほとんどのように動作する) リストのデザインを作成しようとしています:

代替テキスト

このために、ソースコードをダウンロードし、それを実装するクラスとxmlファイルを知るために勉強しています。

そして、この 2 つの 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" />

もう 1 つは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>

And now with this declared I tried to run in the emulator, but the LogCat returns another error:

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

So, my question is, why is this happening? And if you have another solution for implement a view like this I'm trying.

Thanks.

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

また、以下を使用しながら整数値を文字列に変換することで問題を解決することもできます。

name.setText(name_value_needs_to_be_string);

ここで、nameは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 に答える