Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
855 views
in Technique[技术] by (71.8m points)

android - Set TypeFace for ListView

I have 2 XML file for list View. One for views and other for use first. note_list.XML is :

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

and list_otem.XML is :

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

in below code, i set adapter for list:

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

How can i set type face for TextViews in item_list.XML? I set note_list for content view in java code. so can't access to views of list_item.xml. thanks for helping me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You'll need to override the SimpleAdapter's getView(int position, View convertView, ViewGroup parent) method and cast the result to a TextView. That should be safe to do for the R.layout.list_item layout, although you may want to double check that.

From there, setting a typeface works as usual.

Snippet, to be placed inside an (anonymous) extension of 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;
}

If, perhaps at some point in the future, you're going to have a more complex layout made out of more than just a single TextView, I'd consider implementing your own extension of ArrayAdapter. There are heaps of examples on how to go about that (also look up the ViewHolder/RowWrapper pattern) and it'll give you full control.


Edit: example code below.

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

Copy-paste the class above and make sure to set up the type face field as per your requirements. Then replace the current SimpleAdapter; i.e. something like this:

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

Note that I didn't actually compile or run any of this. I'll leave it up to you to fill in any gaps and/or make corrections.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...