Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.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/3/android/229.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
Java 自定义列表视图+;getView方法_Java_Android_Listview_Listadapter - Fatal编程技术网

Java 自定义列表视图+;getView方法

Java 自定义列表视图+;getView方法,java,android,listview,listadapter,Java,Android,Listview,Listadapter,我需要使用getView()方法的自定义适配器的帮助。当适配器在getView()方法中创建一个列表时,每次呈现都会调用该方法,如holder.textSpoint.setTextColor()等。这会带来很大的负载,列表开始变慢。 请帮我解决这个问题。谢谢 public class myAdapterDouble extends ArrayAdapter<Order> { private int[] colorWhite = new int[] { -0x1 }; private

我需要使用getView()方法的自定义适配器的帮助。当适配器在getView()方法中创建一个列表时,每次呈现都会调用该方法,如
holder.textSpoint.setTextColor()
等。这会带来很大的负载,列表开始变慢。
请帮我解决这个问题。谢谢

public class myAdapterDouble extends ArrayAdapter<Order> {

private int[] colorWhite = new int[] { -0x1 };
private int[] colors = new int[] { -0x1, -0x242425 };
private int[] colorBlack = new int[] { -0x1000000 };
private int[] colorTransparent = new int[] { android.R.color.transparent };
private LayoutInflater lInflater;
private ArrayList<Order> data;
private Order o;
private DisplayImageOptions options;
private ImageLoader imageLoader;
private ImageLoaderConfiguration config;
private Context ctx;
private Typeface tf;

public myAdapterDouble(Context c, int listItem, ArrayList<Order> data) {
super(c, listItem, data);
lInflater = LayoutInflater.from(c);
this.data = data;
ctx = c;

tf = Typeface.createFromAsset(ctx.getAssets(), "meiryo.ttc");

imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
        .showStubImage(R.drawable.no_image)
        .showImageForEmptyUri(R.drawable.no_image).cacheOnDisc()
        .cacheInMemory().build();

config = new ImageLoaderConfiguration.Builder(c.getApplicationContext())
        .threadPriority(Thread.NORM_PRIORITY - 2)
        .memoryCacheSize(2 * 1024 * 1024)  // 2 Mb
        .memoryCacheExtraOptions(100, 100)
        .denyCacheImageMultipleSizesInMemory()
        .discCacheFileNameGenerator(new Md5FileNameGenerator())
        .tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging()
        .build();
ImageLoader.getInstance().init(config);
}

SharedPreferences sharedPref;
boolean posters, fixFont;
float headerSize, timeSize, dateSize;
int imageWSize;

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

ViewHolder holder = null;
o = data.get(position);

sharedPref = PreferenceManager.getDefaultSharedPreferences(ctx);
posters = sharedPref.getBoolean("poster", true);
fixFont = sharedPref.getBoolean("fix_font", false);

if (convertView == null) {
    convertView = lInflater.inflate(R.layout.double_list_item, null);
    holder = new ViewHolder();
    holder.textName = (TextView) convertView.findViewById(R.id.text);   
    if (fixFont) {
        try {
            holder.textName.setTypeface(tf);
        } catch (Exception e) {
            Toast.makeText(ctx, e.toString(), Toast.LENGTH_SHORT).show();
        }
    } else {
        try {
            holder.textName.setTypeface(Typeface.DEFAULT);
        } catch (Exception e) {
            Toast.makeText(ctx, e.toString(), Toast.LENGTH_SHORT).show();
        }
    }
    holder.textEpisode = (TextView) convertView.findViewById(R.id.text2);
    holder.img = (ImageView) convertView.findViewById(R.id.image);

    String width = sharedPref.getString("image_width", "70");
    imageWSize = Integer.parseInt(width); // ширина
    final float scale = getContext().getResources().getDisplayMetrics().density;
    int px = (int) (imageWSize*scale + 0.5f);

    holder.img.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
    holder.img.getLayoutParams().width = px;
    if(imageWSize == 0) {
        holder.img.getLayoutParams().width = LayoutParams.WRAP_CONTENT;
    }
    holder.img.setPadding(5, 5, 5, 5);

    convertView.setTag(holder);
} else {
    holder = (ViewHolder) convertView.getTag();
}

headerSize = Float.parseFloat(sharedPref.getString("headsize", "20"));
holder.textName.setTextSize(headerSize); // размер названия
timeSize = Float.parseFloat(sharedPref.getString("timesize", "15"));
holder.textEpisode.setTextSize(timeSize); // размер времени

if (posters) {
    holder.img.setVisibility(View.VISIBLE);
    try {
        imageLoader.displayImage(o.getLink(), holder.img, options);
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
} else {
    holder.img.setVisibility(View.GONE);
}

holder.img.setTag(o);
holder.textName.setText(o.getTextName());
holder.textEpisode.setText(o.getTextEpisode());
holder.textEpisode.setTextColor(Color.BLACK);

if (o.getTextEpisode().toString().contains(ctx.getString(R.string.final_ep))) { 
    String finaleColor = sharedPref.getString("finale_color", "1");
    if (finaleColor.contains("default")) {
        holder.textEpisode.setTextColor(Color.parseColor("#2E64FE"));
    }
    if (finaleColor.contains("yelow")) {
        holder.textEpisode.setTextColor(Color.YELLOW);
    }
    if (finaleColor.contains("red")) {
        holder.textEpisode.setTextColor(Color.RED);
    }
    if (finaleColor.contains("green")) {
        holder.textEpisode.setTextColor(Color.GREEN);
    }
    if (finaleColor.contains("white")) {
        holder.textEpisode.setTextColor(Color.WHITE);
    }
    if (finaleColor.contains("gray")) {
        holder.textEpisode.setTextColor(Color.GRAY);
    }           
} else {
    holder.textEpisode.setTextColor(Color.parseColor("#2E64FE"));
}

String chooseColor = sharedPref.getString("colorList", "");
if (chooseColor.contains("white")) {
    int colorPos = position % colorWhite.length;
    convertView.setBackgroundColor(colorWhite[colorPos]);
}
if (chooseColor.contains("black")) {
    int colorPos = position % colorBlack.length;
    convertView.setBackgroundColor(colorBlack[colorPos]);
    holder.textName.setTextColor(Color.parseColor("#FFFFFF"));
}
if (chooseColor.contains("whitegray")) {
    int colorPos = position % colors.length;
    convertView.setBackgroundColor(colors[colorPos]);
}
if (chooseColor.contains("transparent")) {
    int colorPos = position % colorTransparent.length;
    convertView.setBackgroundColor(colorTransparent[colorPos]);
}

return convertView;
}
公共类myAdapterDouble扩展ArrayAdapter{
私有int[]colorWhite=新int[]{-0x1};
私有int[]颜色=新int[]{-0x1,-0x2425};
私有int[]colorBlack=新int[]{-0x1000000};
私有int[]colorTransparent=新int[]{android.R.color.transparent};
私人停车场;
私有数组列表数据;
私人命令o;
私有显示图像选项;
私有图像加载器;
私有ImageLoaderConfiguration配置;
私有上下文ctx;
专用字体tf;
公共myAdapterDouble(上下文c、int-listItem、ArrayList数据){
超级(c、列表项、数据);
lInflater=从(c)开始的LayoutInflater;
这个数据=数据;
ctx=c;
tf=Typeface.createFromAsset(ctx.getAssets(),“meiryo.ttc”);
imageLoader=imageLoader.getInstance();
选项=新建DisplayImageOptions.Builder()
.showStubImage(右侧可绘制,无图像)
.showmageforemptyuri(R.drawable.no_image).cacheOnDisc()
.cacheInMemory().build();
config=newImageLoaderConfiguration.Builder(c.getApplicationContext())
.threadPriority(Thread.NORM_PRIORITY-2)
.memoryCacheSize(2*1024*1024)//2 Mb
.memoryCacheExtraOptions(100100)
.DenycacheMageMultipleSizesInMemory()
.discCacheFileNameGenerator(新的Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging()
.build();
ImageLoader.getInstance().init(配置);
}
SharedPreferences sharedPref;
布尔海报,固定字体;
float headerSize、timeSize、dateSize;
int imageWSize;
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
ViewHolder=null;
o=数据获取(位置);
sharedPref=PreferenceManager.getDefaultSharedPreferences(ctx);
posters=sharedPref.getBoolean(“poster”,true);
fixFont=sharedPref.getBoolean(“fix_-font”,false);
if(convertView==null){
convertView=lInflater.充气(R.layout.double\U列表项,空);
holder=新的ViewHolder();
holder.textName=(TextView)convertView.findViewById(R.id.text);
如果(固定字体){
试一试{
holder.textName.setTypeface(tf);
}捕获(例外e){
Toast.makeText(ctx,e.toString(),Toast.LENGTH_SHORT).show();
}
}否则{
试一试{
holder.textName.setTypeface(Typeface.DEFAULT);
}捕获(例外e){
Toast.makeText(ctx,e.toString(),Toast.LENGTH_SHORT).show();
}
}
holder.textSection=(TextView)convertView.findViewById(R.id.text2);
holder.img=(ImageView)convertView.findViewById(R.id.image);
字符串宽度=sharedPref.getString(“图像宽度”,“70”);
imageWSize=Integer.parseInt(宽度);//
最终浮动比例=getContext().getResources().getDisplayMetrics().density;
int px=(int)(图像大小*比例+0.5f);
holder.img.getLayoutParams().height=LayoutParams.WRAP_CONTENT;
holder.img.getLayoutParams().width=px;
如果(imageWSize==0){
holder.img.getLayoutParams().width=LayoutParams.WRAP_CONTENT;
}
支架安装衬垫(5,5,5,5);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
headerSize=Float.parseFloat(sharedPref.getString(“headsize”,“20”));
holder.textName.setTextSize(headerSize);//
timeSize=Float.parseFloat(sharedPref.getString(“timeSize”,“15”));
holder.textSpidence.setTextSize(timeSize);//
如有(海报){
holder.img.setVisibility(视图可见);
试一试{
displayImage(o.getLink(),holder.img,options);
}捕获(NullPointerException e){
e、 printStackTrace();
}
}否则{
holder.img.setVisibility(View.GONE);
}
保持架img设置标签(o);
holder.textName.setText(o.getTextName());
holder.textSpoint.setText(o.getTextSpoint());
holder.textSection.setTextColor(Color.BLACK);
如果(o.getTextSpoint().toString()包含(ctx.getString(R.string.final_ep)){
字符串finaleColor=sharedPref.getString(“finale_color”,“1”);
if(最终颜色包含(“默认”)){
holder.textSection.setTextColor(Color.parseColor(“#2E64FE”);
}
if(最终颜色包含(“黄色”)){
holder.textSession.setTextColor(Color.YELLOW);
}
如果(最终颜色包含(“红色”)){
holder.textSection.setTextColor(Color.RED);
}
如果(最终颜色包含(“绿色”)){
holder.textSection.setTextColor(Color.GREEN);
}
如果(最终颜色包含(“白色”)){
holder.textSection.setTextColor(Color.WHITE);
}
如果(最终颜色包含(“灰色”)){
holder.textSection.setTextColor(Color.GRAY);
}           
}否则{
holder.textSection.setTextColor(Color.parseColor(“#2E64FE”);
}
String chooseColor=sharedPref.getString(“colorList”,”);
如果(选择颜色包含(“白色”)){
int colorPos=位置%colorWhite.length;
convertView.setBackgroundColor(colorWhite[colorPos]);
}
如果(选择颜色包含(“黑色”)){
int colorPos=位置%COLORBLOCK.length;
convertView.setBackgroundColor(colorBlack[colorPos]);
holder.textName.setTextColor(Color.parseColor(“#FFFFFF”));
}
如果(选择颜色包含(“白灰色”)){
int colorPos=位置%colors.length;
convertView.setBackgroundColor(颜色[colorPos]);
}
如果(选择颜色包含(“透明”)){
int colorPos=位置%colorTransparent.length;
convertView.setBackgroundColor(colorTransparent[colorPos]);
}
返回视图;
}
getView()方法将在每次滚动加载下一项时调用

sharedPref = PreferenceManager.getDefaultSharedPreferences(ctx);
posters = sharedPref.getBoolean("poster", true);
fixFont = sharedPref.getBoolean("fix_font", false);
这会减慢滚动,因为每次需要读取和解析首选项时

public class myAdapterDouble extends ArrayAdapter<Order> {
    private int[] colorWhite = new int[] { -0x1 };
    private int[] colors = new int[] { -0x1, -0x242425 };
    private int[] colorBlack = new int[] { -0x1000000 };
    private int[] colorTransparent = new int[] { android.R.color.transparent };
    private LayoutInflater lInflater;
    private ArrayList<Order> data;
    private Order o;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    private ImageLoaderConfiguration config;
    private Context ctx;
    private Typeface tf;


    boolean posters, fixFont;
    float headerSize, timeSize, dateSize;
    int imageWSize;
    private String finaleColor;
    private String chooseColor;
    private String final_ep;

    public myAdapterDouble(Context c, int listItem, ArrayList<Order> data) {
        super(c, listItem, data);
        lInflater = LayoutInflater.from(c);
        this.data = data;
        ctx = c;

        tf = Typeface.createFromAsset(ctx.getAssets(), "meiryo.ttc");

        imageLoader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder().showStubImage(R.drawable.no_image).showImageForEmptyUri(R.drawable.no_image).cacheOnDisc().cacheInMemory().build();

        config = new ImageLoaderConfiguration.Builder(c.getApplicationContext()).threadPriority(Thread.NORM_PRIORITY - 2).memoryCacheSize(2 * 1024 * 1024)
                // 2 Mb
                .memoryCacheExtraOptions(100, 100).denyCacheImageMultipleSizesInMemory().discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO)
                .enableLogging().build();
        ImageLoader.getInstance().init(config);

        SharedPreferences sharedPref;

        sharedPref = PreferenceManager.getDefaultSharedPreferences(ctx);
        posters = sharedPref.getBoolean("poster", true);
        fixFont = sharedPref.getBoolean("fix_font", false);

        String width = sharedPref.getString("image_width", "70");
        imageWSize = Integer.parseInt(width); // ширина
        headerSize = Float.parseFloat(sharedPref.getString("headsize", "20"));
        timeSize = Float.parseFloat(sharedPref.getString("timesize", "15"));

        finaleColor = sharedPref.getString("finale_color", "1");
        chooseColor = sharedPref.getString("colorList", "");
        final_ep = ctx.getString(R.string.final_ep);
    }

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

        ViewHolder holder = null;
        o = data.get(position);

        if (convertView == null) {
            convertView = lInflater.inflate(R.layout.double_list_item, null);
            holder = new ViewHolder();
            holder.textName = (TextView) convertView.findViewById(R.id.text);
            if (fixFont) {
                try {
                    holder.textName.setTypeface(tf);
                }
                catch (Exception e) {
                    Toast.makeText(ctx, e.toString(), Toast.LENGTH_SHORT).show();
                }
            }
            else {
                try {
                    holder.textName.setTypeface(Typeface.DEFAULT);
                }
                catch (Exception e) {
                    Toast.makeText(ctx, e.toString(), Toast.LENGTH_SHORT).show();
                }
            }
            holder.textEpisode = (TextView) convertView.findViewById(R.id.text2);
            holder.img = (ImageView) convertView.findViewById(R.id.image);


            final float scale = getContext().getResources().getDisplayMetrics().density;
            int px = (int) (imageWSize * scale + 0.5f);

            holder.img.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
            holder.img.getLayoutParams().width = px;
            if (imageWSize == 0) {
                holder.img.getLayoutParams().width = LayoutParams.WRAP_CONTENT;
            }
            holder.img.setPadding(5, 5, 5, 5);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.textName.setTextSize(headerSize); // размер названия

        holder.textEpisode.setTextSize(timeSize); // размер времени

        if (posters) {
            holder.img.setVisibility(View.VISIBLE);
            try {
                imageLoader.displayImage(o.getLink(), holder.img, options);
            }
            catch (NullPointerException e) {
                e.printStackTrace();
            }
        }
        else {
            holder.img.setVisibility(View.GONE);
        }

        holder.img.setTag(o);
        holder.textName.setText(o.getTextName());
        holder.textEpisode.setText(o.getTextEpisode());
        holder.textEpisode.setTextColor(Color.BLACK);


        if (o.getTextEpisode().toString().contains()) {

            if (finaleColor.contains("default")) {
                holder.textEpisode.setTextColor(Color.parseColor("#2E64FE"));
            }
            if (finaleColor.contains("yelow")) {
                holder.textEpisode.setTextColor(Color.YELLOW);
            }
            if (finaleColor.contains("red")) {
                holder.textEpisode.setTextColor(Color.RED);
            }
            if (finaleColor.contains("green")) {
                holder.textEpisode.setTextColor(Color.GREEN);
            }
            if (finaleColor.contains("white")) {
                holder.textEpisode.setTextColor(Color.WHITE);
            }
            if (finaleColor.contains("gray")) {
                holder.textEpisode.setTextColor(Color.GRAY);
            }
        }
        else {
            holder.textEpisode.setTextColor(Color.parseColor("#2E64FE"));
        }


        if (chooseColor.contains("white")) {
            int colorPos = position % colorWhite.length;
            convertView.setBackgroundColor(colorWhite[colorPos]);
        }
        if (chooseColor.contains("black")) {
            int colorPos = position % colorBlack.length;
            convertView.setBackgroundColor(colorBlack[colorPos]);
            holder.textName.setTextColor(Color.parseColor("#FFFFFF"));
        }
        if (chooseColor.contains("whitegray")) {
            int colorPos = position % colors.length;
            convertView.setBackgroundColor(colors[colorPos]);
        }
        if (chooseColor.contains("transparent")) {
            int colorPos = position % colorTransparent.length;
            convertView.setBackgroundColor(colorTransparent[colorPos]);
        }

        return convertView;
    }
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
sharedPref = PreferenceManager.getDefaultSharedPreferences(ctx);
posters = sharedPref.getBoolean("poster", true);
fixFont = sharedPref.getBoolean("fix_font", false);

if (v == null) {
    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.double_list_item, null);
}

TextView textName = (TextView) v.findViewById(R.id.text);
if (fixFont){
   try {
        textName.setTypeface(tf);
   } catch (Exception e) {
        Toast.makeText(ctx, e.toString(), Toast.LENGTH_SHORT).show();
   }
}else {
   try {
        textName.setTypeface(Typeface.DEFAULT);
   } catch (Exception e) {
        Toast.makeText(ctx, e.toString(), Toast.LENGTH_SHORT).show();
   }
}
return super.getView(position, v, parent);
}
};