Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何更改listview中每个列表项的前面_Android_Listview_Android Typeface - Fatal编程技术网

Android 如何更改listview中每个列表项的前面

Android 如何更改listview中每个列表项的前面,android,listview,android-typeface,Android,Listview,Android Typeface,我有5种字体样式(Typeface),希望以不同的字体样式在ListView中显示用户输入的内容5次。 下面是我尝试过的代码 public void ShowTextStyles(String inputText, int color){ final Dialog dialog = new Dialog(EditImageActivity.this); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialo

我有5种字体样式(
Typeface
),希望以不同的字体样式在
ListView
中显示用户输入的内容5次。 下面是我尝试过的代码

 public void ShowTextStyles(String inputText, int color){
    final Dialog dialog = new Dialog(EditImageActivity.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.textstylelayout);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    // Array of strings...
    final String[] textStyles={"Gobold Bold.ttf","beyond_wonderland.ttf"};
    final String[] mobileArray = {inputText,inputText,inputText,inputText,inputText,inputText,inputText,inputText,inputText,inputText};
    ArrayAdapter adapter = new ArrayAdapter<String>(EditImageActivity.this,
            R.layout.text_style_list, mobileArray);

    ListView listView = (ListView) dialog.findViewById(R.id.mobile_list);
    listView.setAdapter(adapter);

    // Set an item click listener for ListView
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Get the selected item text from ListView
            String selectedItem = (String) parent.getItemAtPosition(position);

            // Display the selected item text on TextView




        }
    });
    dialog.setCancelable(true);
    dialog.show();


}
public void ShowTextStyles(字符串inputText,int-color){
最终对话框=新对话框(EditImageActivity.this);
对话框.requestWindowFeature(窗口.FEATURE\u无\u标题);
setContentView(R.layout.textstylelayout);
dialog.getWindow().setBackgroundDrawable(新的ColorDrawable(Color.TRANSPARENT));
//字符串数组。。。
最后一个字符串[]textStyles={“Gobold Bold.ttf”,“beyond_wonderland.ttf”};
最终字符串[]mobileArray={inputText,inputText,inputText,inputText,inputText,inputText,inputText,inputText};
ArrayAdapter=新的ArrayAdapter(EditImageActivity.this,
R.layout.text\u style\u list,mobileArray);
ListView=(ListView)dialog.findViewById(R.id.mobile\u list);
setAdapter(适配器);
//为ListView设置项目单击侦听器
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//从ListView获取所选项目文本
字符串selectedItem=(字符串)parent.getItemAtPosition(位置);
//在TextView上显示所选项目文本
}
});
对话框。可设置可取消(true);
dialog.show();
}

为此,您需要使用
BaseAdapter

public class Myadapter extends BaseAdapter {

    AssetManager assetManager = getAssets(); 

    LayoutInflater lif;
    ImageView sideArrow;
    TextView tv;


    public Myadapter(Context ctx) {
        lif = (LayoutInflater) ctx.getSystemService(LAYOUT_INFLATER_SERVICE);


    }

    @Override
    public int getCount() {

        return favarets.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


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

        View vi = convertView;
        if (convertView == null)
            vi = lif.inflate(R.layout.inflate, null);
        sideArrow = (ImageView) vi.findViewById(R.id.imageViewsidemark);


        tv = (TextView) vi.findViewById(R.id.textFav);
        tv.setText(favarets.get(position));

        final Typeface tvFont = Typeface.createFromAsset(assetManager, "OPTIMA.TTF");
        tv.setTypeface(tvFont);
        tv.setTextColor(Color.BLACK);

        return vi;
    }

}
下面是如何使用customAdapter的示例

解决方案2:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
            android.R.layout.simple_list_item_1, filled_arr) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView text = (TextView) view.findViewById(android.R.id.text1);
            text.setTypeface(typeface);
            return view;
        }
    };
ArrayAdapter=新的ArrayAdapter(getApplicationContext(),
android.R.layout.simple_list_item_1,已填充_arr){
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=super.getView(位置、转换视图、父级);
TextView text=(TextView)view.findViewById(android.R.id.text1);
text.setTypeface(字体);
返回视图;
}
};

创建自定义适配器并将其用于listview:

public class TestAdapter extends BaseAdapter {
    private Context context;
    private List<String> appData;
    String[] fontFiles = {"Gobold Bold.ttf", "beyond_wonderland.ttf"}; // add more fonts if wish more styles

public TestAdapter(Context context, List<String> appData) {
    this.context = context;
    this.appData = appData;
}

@Override
public int getCount() {
    return appData.size();
}

@Override
public Object getItem(int position) {
    return appData.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

private class ViewHolder {
    TextView tv_item;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_app, null);
        holder = new ViewHolder();
        holder.tv_item = convertView.findViewById(R.id.tv_item);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.tv_item.setText(appData.get(position));
    Typeface typeface;

    // change this condition according to your need
    if (position == 0){
        typeface = Typeface.createFromAsset(context.getAssets(), fontFiles[0]);
    }else {
        typeface = Typeface.createFromAsset(context.getAssets(), fontFiles[1]);
    }

    holder.tv_item.setTypeface(typeface);

    return convertView;
}
}
公共类TestAdapter扩展了BaseAdapter{
私人语境;
私有列表数据;
String[]fontFiles={“Gobold Bold.ttf”,“beyond_wonderland.ttf”};//如果需要更多样式,请添加更多字体
公共测试适配器(上下文,列表appData){
this.context=上下文;
this.appData=appData;
}
@凌驾
public int getCount(){
返回appData.size();
}
@凌驾
公共对象getItem(int位置){
返回appData.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回0;
}
私有类视窗持有者{
TextView电视节目;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
ViewHolder=null;
if(convertView==null){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(R.layout.list\u应用程序,空);
holder=新的ViewHolder();
holder.tv_项=convertView.findViewById(R.id.tv_项);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.tv_item.setText(appData.get(position));
字体;
//根据您的需要更改此条件
如果(位置==0){
typeface=typeface.createFromAsset(context.getAssets(),fontFiles[0]);
}否则{
typeface=typeface.createFromAsset(context.getAssets(),fontFiles[1]);
}
支架.电视项目.设置字体(字体);
返回视图;
}
}

我是android新手,只是不知道我该怎么做。我已经在使用arrayadapter了。那么我应该用base替换它吗adapter@farhan是,如果要设置每行不同的字体,则需要使用baseadapter..@farhan请检查我的更新答案。我们使用arrayadapter设置了单个字体。获取错误。空对象引用上的void android.widget.TextView.setTypeface(android.graphics.Typeface)“”谢谢。它工作得很好。还有一件事,我怎么能做到呢?listview.setOnItemClickListener(新的OnItemClickListener(){@Override public void onItemClick(AdapterView AdapterView,View视图,int pos,long l){//在这里您可以获取位置并访问//TicketList对象}};///会有用吗?是的。谢谢你救了我一天!