2012-11-15 73 views
1

我有2個用於列表視圖的XML文件。其中一個用於視圖,另一個用於第一個。 note_list.XML是:爲ListView設置TypeFace

<?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的視圖。謝謝你幫助我。

回答

5

您需要覆蓋SimpleAdaptergetView(int position, View convertView, ViewGroup parent)方法並將結果轉換爲TextView。對於R.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; 
} 

如果,也許在未來的某個時刻,你將不得不做出來的超過一個更復雜的佈局只是一個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 } 
); 

請注意,我沒有實際編譯或運行任何此。我會留給你填補任何空白和/或作出更正。

+0

我有'方法getView(int,View,ViewGroup)未定義類型ListActivity'和'AllNotes類型的方法getView(int,View,ViewGroup)必須重寫超類方法錯誤。 –

+0

你能幫我嗎? –

+0

@ 1mohammadi.ir:正如我的回答中所解釋的,在**'SimpleAdapter' **,而不是'ListActivity'中重寫該方法。現在你正在做後者,這是錯誤消息試圖告訴你的。要麼以匿名的方式進行(在實例化'SimpleAdapter'的地方),要麼爲它創建自己的子類。 –

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。尋求幫助

+1

只需要注意以上代碼:它遠沒有效率。它一次又一次地創建相同的字體,每次調用getView(...)'以及執行相對昂貴的視圖查找。我強烈考慮實現[ViewHolder/RowWrapper](http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder)模式來優化這一點。 –