1

リストビュー用に2つのXMLファイルがあります。1つはビュー用で、もう1つは最初に使用します。note_list.XMLは:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dip" >

    <Button
        android:id="@+id/allNotes_btn_refresh_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/all_note_refresh_list" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />

</LinearLayout>

そしてlist_otem.XMLは:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/list_lbl_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <TextView
        android:id="@+id/list_lbl_subject"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/list_lbl_date"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

以下のコードでは、リストのアダプターを設定します。

ArrayList<HashMap<String, String>> noteList;
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.note_list);
    noteList = new ArrayList<HashMap<String, String>>();
    lv = getListView();

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

            String note_id = ((TextView) view
                    .findViewById(R.id.list_lbl_id)).getText().toString();
            Intent i = new Intent(getApplicationContext(), NoteDetail.class);
            i.putExtra("note_id", note_id);
            startActivityForResult(i, 100);
        }
    });
}

public class LoadAllNotes extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(AllNotes.this);
            pDialog.setMessage("???? ??? ????...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

            noteList.clear();
        }

        protected String doInBackground(String... args) {

            UserFunctions userFunctions = new UserFunctions();

            jSon = userFunctions.getAllNotes(userId);

            Log.i("AllNotes >> jSon >>", jSon.toString());

            return null;
        }

        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
            try {
                if (jSon.has(KEY_SUCCESS)) {
                    String success = jSon.getString(KEY_SUCCESS);
                    if (success.equals("1")) {

                        notes = jSon.getJSONObject("notes");
                        for (int i = 0; i < notes.length(); i++) {
                            JSONObject c = notes.getJSONObject(Integer
                                    .toString(i));

                            Log.i("JSONObject c >>", c.toString());

                            String id = c.getString(KEY_NOTE_ID);
                            String subject = c.getString(KEY_NOTE_SUBJECT);
                            String date = c.getString(KEY_NOTE_DATE);

                            HashMap<String, String> map = new HashMap<String, String>();
                            map.put(KEY_NOTE_ID, id);
                            map.put(KEY_NOTE_SUBJECT, subject);
                            map.put(KEY_NOTE_DATE, date);

                            noteList.add(map);
                        }

                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            runOnUiThread(new Runnable() {

                public void run() {
                    ListAdapter adapter = new SimpleAdapter(AllNotes.this,
                            noteList, R.layout.list_item, new String[] {
                                    KEY_NOTE_ID, KEY_NOTE_SUBJECT,
                                    KEY_NOTE_DATE }, new int[] {
                                    R.id.list_lbl_id, R.id.list_lbl_subject,
                                    R.id.list_lbl_date });
                    setListAdapter(adapter);
                }
            });
        }
    }

item_list.XMLでTextViewsのタイプフェイスを設定するにはどうすればよいですか?Javaコードのコンテンツビューにnote_listを設定しました。そのため、list_item.xmlのビューにアクセスできません。私を助けてくれてありがとう。

4

2 に答える 2

6

SimpleAdaptergetView(int position, View convertView, ViewGroup parent)メソッドをオーバーライドして、結果をにキャストする必要がありますTextViewR.layout.list_itemレイアウトについてはこれで安全ですが、再確認することをお勧めします。

そこから、書体の設定は通常どおり機能します。

SimpleAdapterの(匿名の)拡張機能内に配置されるスニペット:

Typeface mTypeface = ... // only needs to be initialised once.

@Override public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    TextView textview = (TextView) view;
    textview.setTypeface(mTypeface);
    return textview;
}

おそらく将来のある時点で、1つだけではなく、より複雑なレイアウトを作成する場合はTextView、の独自の拡張機能を実装することを検討しますArrayAdapter。その方法についてはたくさんの例があり(ViewHolder / RowWrapperパターンも調べてください)、完全に制御できます。


編集:以下のサンプルコード。

public class TypefacedSimpleAdapter extends SimpleAdapter {
    private final Typeface mTypeface;

    public TypefacedSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        mTypeface = Typeface.createFromAsset(context.getAssets(), /* typeface */);
    }

    @Override public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        TextView textview = (TextView) view;
        textview.setTypeface(mTypeface);
        return textview;
    }
}

上記のクラスをコピーして貼り付け、要件に従ってタイプ面フィールドを設定してください。SimpleAdapter次に、現在の;を置き換えます。つまり、次のようなものです。

ListAdapter adapter = new TypefacedSimpleAdapter(AllNotes.this,
        noteList, R.layout.list_item, new String[] {
        KEY_NOTE_ID, KEY_NOTE_SUBJECT,
        KEY_NOTE_DATE }, new int[] {
        R.id.list_lbl_id, R.id.list_lbl_subject,
        R.id.list_lbl_date }
);

私は実際にこれをコンパイルまたは実行しなかったことに注意してください。ギャップを埋めたり、修正したりするのはあなたに任せます。

于 2012-11-15T18:24:34.400 に答える
1

私はこれをクラスの下に追加します:

public class myAdapter extends SimpleAdapter {
    public myAdapter(Context context, List<HashMap<String, String>> items,
            int resource, String[] from, int[] to) {
        super(context, items, resource, from, to);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        Typeface typeFace = Typeface.createFromAsset(getAssets(),
                "fontsfolder/B Yekan.ttf");

        TextView lbl_subject = ((TextView) view
                .findViewById(R.id.list_lbl_subject));
        TextView lbl_date = ((TextView) view
                .findViewById(R.id.list_lbl_date));

        lbl_date.setTypeface(typeFace);
        lbl_subject.setTypeface(typeFace);

        return view;
    }
}

そしてこれでリストを埋めるときにコードを変更します:

myAdapter adapter = new myAdapter(AllNotes.this, noteList,
        R.layout.list_item, new String[] { KEY_NOTE_ID,
                KEY_NOTE_SUBJECT, KEY_NOTE_DATE },
        new int[] { R.id.list_lbl_id,
                R.id.list_lbl_subject, R.id.list_lbl_date });
setListAdapter(adapter);

@MHに感謝します。ヘルプのために

于 2012-11-16T10:04:26.313 に答える