简体   繁体   English

android ListView 中带有 SimpleAdapter 的自定义字体

[英]Custom Typface in android ListView with SimpleAdapter

Can anyone tell me how to change the typeface of the listview to arial i'm not able to figure it out.谁能告诉我如何将列表视图的字体更改为arial我无法弄清楚。

Following is my code:以下是我的代码:

public class TopNewsActivity extends ListActivity {

public static final String LOG_TAG = "Infra";
private ProgressDialog progressDialog;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listplaceholder);
    new BackgroundAsyncTask().execute();
}

public class BackgroundAsyncTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> {

    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(TopNewsGroup.group);
        progressDialog.setCancelable(true);
        progressDialog.setMessage("Loading...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setProgress(0);
        progressDialog.show();
    }

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(String... paths) {

        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

        String xml = XMLfunctions.getTopNewsXML();
        Document doc = XMLfunctions.XMLfromString(xml);

        int numResults = XMLfunctions.numResults(doc);
        Log.d(LOG_TAG, "Number of Results: " + numResults);
        if ((numResults <= 0)) {
            Toast.makeText(TopNewsActivity.this, "No Result Found",Toast.LENGTH_LONG).show();
            return null;
        }

        NodeList nodes = doc.getElementsByTagName("result");

        for (int i = 0; i < nodes.getLength(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();

            Element e = (Element) nodes.item(i);
            map.put("id", XMLfunctions.getValue(e, "id"));
            map.put("title", XMLfunctions.getValue(e, "title"));
            map.put("date", XMLfunctions.getValue(e, "date"));
            map.put("recordDate", XMLfunctions.getValue(e, "recordDate"));
            mylist.add(map);
        }
        return mylist;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
    }

    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {

        ListAdapter adapter = new MySimpleAdapter(TopNewsActivity.this, result, R.layout.list_item, new String[] { "title", "date" }, new int[] { R.id.item_title, R.id.item_subtitle });
        setListAdapter(adapter);
        progressDialog.dismiss();

        final ListView lv = getListView();

        lv.setTextFilterEnabled(true);  
        lv.setOnItemClickListener(new OnItemClickListener() {
            @SuppressWarnings("unchecked")
            @Override
            public void onItemClick(AdapterView<?> a, View view, final int position, long id) {

                HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);

                Intent i = new Intent(TopNewsActivity.this, NewsDetails.class);
                i.putExtra("content_id", o.get("id"));
                i.putExtra("title", o.get("title"));
                i.putExtra("date", o.get("recordDate"));
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                View v = TopNewsGroup.group.getLocalActivityManager().startActivity("ShowNews", i).getDecorView();

                // Again, replace the view
                TopNewsGroup.group.replaceView(v);
            }
        });
    }
}

public class MySimpleAdapter extends SimpleAdapter {
    public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
    }

    @Override
    public View getView(int position, View view, ViewGroup parent){

        LayoutInflater inflater = getLayoutInflater();
        Typeface localTypeface1 = Typeface.createFromAsset(getAssets(), "fonts/arial.ttf");
        View v = super.getView(position, view, parent);
         ((TextView)v).setTypeface(localTypeface1);
         return v;


    }
}

}

Please help请帮忙

Was simple enough很简单

public class MySimpleAdapter extends SimpleAdapter {

    private ArrayList<HashMap<String, String>> results;

    public MySimpleAdapter(Context context, ArrayList<HashMap<String, String>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        this.results = data;
    }

    public View getView(int position, View view, ViewGroup parent){

        Typeface localTypeface1 = Typeface.createFromAsset(getAssets(), "fonts/arial.ttf");
        View v = view;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item, null);
        }
        TextView tt = (TextView) v.findViewById(R.id.item_title);
        tt.setText(results.get(position).get("title"));
        tt.setTypeface(localTypeface1);
        TextView bt = (TextView) v.findViewById(R.id.item_subtitle);
        bt.setText(results.get(position).get("date"));
        bt.setTypeface(localTypeface1);
        return v;
    }
}

That's it:)而已:)

If you don't want to create a new class you can override the getView method when creating the SimpleAdapter... refer to my answer on a similar question:如果您不想创建新的 class 您可以在创建 SimpleAdapter 时覆盖 getView 方法...请参阅我对类似问题的回答:

Custon Typeface in listview with SimpleAdapter 带有 SimpleAdapter 的列表视图中的自定义字体

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM