如何使用自定义字体在SimpleCursorAdapter在检索字典应用程序的列表视图? [英] How to use custom font in SimpleCursorAdapter for a list view at Searchable Dictionary App?

查看:117
本文介绍了如何使用自定义字体在SimpleCursorAdapter在检索字典应用程序的列表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的新的。这是我第project.I我试图在孟加拉一个名称词典,所以我
需要更改列表视图字体。我已经添加的字体到资源文件夹。

 私人无效showResults(查询字符串){    光标光标= managedQuery(DictionaryProvider.CONTENT_URI,NULL,NULL,
                            新的String [] {查询},NULL);    如果(光标== NULL){
        //没有结果
        mTextView.setText(的getString(R.string.no_results,新的对象[] {查询}));
    }其他{
        //显示的结果数
        诠释计数= cursor.getCount();
        字符串countString = getResources()getQuantityString(R.plurals.search_results,计数,新的对象[] {算,查询})。
        mTextView.setText(countString);        //指定列,我们希望在结果中显示
        的String [] =由新的String [] {DictionaryDatabase.KEY_WORD,
                                       DictionaryDatabase.KEY_DEFINITION};        //指定我们想要的列去相应的布局元素
        INT []为= INT新[] {R.id.word,
                               R.id.definition};
        //创建用于定义一个简单的游标适配器,并将其应用到ListView        SimpleCursorAdapter字=新SimpleCursorAdapter(这一点,R.layout.result,光标,从,到);
        mListView.getAdapter();        //定义上单击侦听器列表项
        mListView.setOnItemClickListener(新OnItemClickListener(){
            公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
                //建立用于打开WordActivity与特定词的URI意向
                意图wordIntent =新意图(getApplicationContext(),WordActivity.class);
                乌里数据= Uri.withAppendedPath(DictionaryProvider.CONTENT_URI,
                                                将String.valueOf(ID));
                wordIntent.setData(数据);
                startActivity(wordIntent);
            }
        });
    }
}


  



解决方案

 公共类myAdapter扩展的CursorAdapter {公共myAdapter(上下文的背景下,光标C){
    超(背景下,C);
}@覆盖
公共无效bindView(查看视图,上下文的背景下,光标光标){   字体TF = Typeface.createFromAsset(context.getAssets(),字体/ Rupali.ttf);
   TextView的电视=(TextView中)view.findViewById(R.id.definition);
   字符串s = cursor.getString(cursor.getColumnIndex(DictionaryDatabase.KEY_DEFINITION));
   tv.setText(多个);
   tv.setTypeface(TF);
   //任何其他修改你想要
 }
@覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
    LayoutInflater INFL = LayoutInflater.from(上下文);
    视图V = infl.inflate(R.layout.result,父母,假);
    bindView(ⅴ,上下文,光标);
    返回伏;
}

}

I am new in android. this is my first project.I am trying to make a Name dictionary in bangla so i need to change the list view font. I already added the font into the asset folder..

    private void showResults(String query) {

    Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, null,
                            new String[] {query}, null);

    if (cursor == null) {
        // There are no results
        mTextView.setText(getString(R.string.no_results, new Object[] {query}));
    } else {
        // Display the number of results
        int count = cursor.getCount();
        String countString = getResources().getQuantityString(R.plurals.search_results,count, new Object[] {count, query});
        mTextView.setText(countString);

        // Specify the columns we want to display in the result
        String[] from = new String[] { DictionaryDatabase.KEY_WORD,
                                       DictionaryDatabase.KEY_DEFINITION };

        // Specify the corresponding layout elements where we want the columns to go
        int[] to = new int[] { R.id.word,
                               R.id.definition };




        // Create a simple cursor adapter for the definitions and apply them to the ListView          

        SimpleCursorAdapter words = new SimpleCursorAdapter(this, R.layout.result, cursor, from, to);


        mListView.getAdapter();

        // Define the on-click listener for the list items
        mListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Build the Intent used to open WordActivity with a specific word Uri
                Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class);
                Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI,
                                                String.valueOf(id));
                wordIntent.setData(data);
                startActivity(wordIntent);
            }
        });
    }     
}

解决方案

public class myAdapter extends CursorAdapter {

public myAdapter(Context context, Cursor c) {
    super(context, c);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

   Typeface tf = Typeface.createFromAsset(context.getAssets(), "font/Rupali.ttf");
   TextView tv = (TextView)view.findViewById(R.id.definition);           
   String s = cursor.getString(cursor.getColumnIndex(DictionaryDatabase.KEY_DEFINITION));
   tv.setText(s);
   tv.setTypeface(tf);
   //Any other modifications you want 
 }
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater infl = LayoutInflater.from(context);
    View v = infl.inflate(R.layout.result, parent, false);
    bindView(v, context, cursor);
    return v;
}

}

这篇关于如何使用自定义字体在SimpleCursorAdapter在检索字典应用程序的列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆