Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

java - Bad performance of custom Listview, Listview Items Name gets converted from Database

I got the Problem that the Listview ist loading to slow, so that my array adapter laags a bit. i dont know how to improve it, or implement a AsyncTask in it.

public class RezeptArrayAdapter extends ArrayAdapter<String> {

public final Context context;
public final String[] values;




public RezeptArrayAdapter(Context context, String[] values) {
    super(context, R.layout.list_rezepte, values);
    this.context = context;
    this.values = values;
    //this.notifyDataSetChanged();

}




@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);



    // DatenBank Abfrage Angepasst auf den Array "context" View
    final DBHandler_Rezepte db_Rzpt = new DBHandler_Rezepte(context);

    //Einzelabfrage der Datenbank Ratings damit Sie weiter unten verarbeitet werden k?nnen
    ConDB_Rezepte Data1 = db_Rzpt.getConDB_Rezepte(1);
    ConDB_Rezepte Data2 = db_Rzpt.getConDB_Rezepte(2);
    ConDB_Rezepte Data3 = db_Rzpt.getConDB_Rezepte(3);
    ConDB_Rezepte Data4 = db_Rzpt.getConDB_Rezepte(4);
    ConDB_Rezepte Data5 = db_Rzpt.getConDB_Rezepte(5);
    ConDB_Rezepte Data6 = db_Rzpt.getConDB_Rezepte(6);
    ConDB_Rezepte Data7 = db_Rzpt.getConDB_Rezepte(7);
    ConDB_Rezepte Data8 = db_Rzpt.getConDB_Rezepte(8);
    ConDB_Rezepte Data9 = db_Rzpt.getConDB_Rezepte(9);
    ConDB_Rezepte Data10 = db_Rzpt.getConDB_Rezepte(10);
    ConDB_Rezepte Data11 = db_Rzpt.getConDB_Rezepte(11);
    ConDB_Rezepte Data12 = db_Rzpt.getConDB_Rezepte(12);
    ConDB_Rezepte Data13 = db_Rzpt.getConDB_Rezepte(13);
    ConDB_Rezepte Data14 = db_Rzpt.getConDB_Rezepte(14);
    ConDB_Rezepte Data15 = db_Rzpt.getConDB_Rezepte(15);
    ConDB_Rezepte Data16 = db_Rzpt.getConDB_Rezepte(16);
    ConDB_Rezepte Data17 = db_Rzpt.getConDB_Rezepte(17);
    ConDB_Rezepte Data18 = db_Rzpt.getConDB_Rezepte(18);
    ConDB_Rezepte Data19 = db_Rzpt.getConDB_Rezepte(19);
    ConDB_Rezepte Data20 = db_Rzpt.getConDB_Rezepte(20);
    ConDB_Rezepte Data21 = db_Rzpt.getConDB_Rezepte(21);
    ConDB_Rezepte Data22 = db_Rzpt.getConDB_Rezepte(22);
    ConDB_Rezepte Data23 = db_Rzpt.getConDB_Rezepte(23);
    ConDB_Rezepte Data24 = db_Rzpt.getConDB_Rezepte(24);
    ConDB_Rezepte Data25 = db_Rzpt.getConDB_Rezepte(25);
    ConDB_Rezepte Data26 = db_Rzpt.getConDB_Rezepte(26);
    ConDB_Rezepte Data27 = db_Rzpt.getConDB_Rezepte(27);
    ConDB_Rezepte Data28 = db_Rzpt.getConDB_Rezepte(28);
    ConDB_Rezepte Data29 = db_Rzpt.getConDB_Rezepte(29);
    ConDB_Rezepte Data30 = db_Rzpt.getConDB_Rezepte(30);



    View rowView = inflater.inflate(R.layout.list_rezepte, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
    RatingBar ratingbar1 = (RatingBar) rowView.findViewById(R.id.ratingBar1);

    //stellt ein das man die Ratingbar nicht clicken kann, somit bleibt die Steuerung erhalten. Sonst ist die Liste nicht mehr clickbar
    ratingbar1.setIsIndicator(true);



    textView.setText(values[position]);
    //TYPEFACE///////////
    Typeface tf = Typeface.createFromAsset(context.getAssets(),"fonts/OFLGoudyStMTT.ttf");
    textView.setTypeface(tf);



    // Change icon based on name
    String pos = values[position];

    int KuchenIcon = R.drawable.ic_kuchen;
    int MuffinIcon = R.drawable.ic_muffin;
    int PlaetzchenIcon = R.drawable.ic_plaetzchen;
    int ApfelkandiertIcon = R.drawable.ic_apfelkandiert;
    int OthersIcon = R.drawable.ic_others;



    //bei dem Equals Vergleich musste ich die GetRessource Methode nach dem Context senden, da die Array Class Context hei?t.

    //System.out.println(s); //DEBUG Ausgabe


    if (pos.matches(Data1.getName()))  {
        imageView.setImageResource(KuchenIcon);
        ratingbar1.setRating(Data1.getrating());
        //context.getResources().getString(R.string.Rezept_01))
        //ratingbar1.setRating(Float.parseFloat(Data1.getrating()));

    } else if (pos.matches(Data2.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data2.getrating());

    } else if (pos.matches(Data3.getName())) {
        imageView.setImageResource(ApfelkandiertIcon);
        ratingbar1.setRating(Data3.getrating());

    } else if (pos.matches(Data4.getName())) {
        imageView.setImageResource(MuffinIcon);
        ratingbar1.setRating(Data4.getrating());

    } else if (pos.matches(Data5.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data5.getrating());

    } else if (pos.matches(Data6.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data6.getrating());

    } else if (pos.matches(Data7.getName())) {
        imageView.setImageResource(OthersIcon);
        ratingbar1.setRating(Data7.getrating());

    } else if (pos.matches(Data8.getName())) {
        imageView.setImageResource(KuchenIcon);
        ratingbar1.setRating(Data8.getrating());

    } else if (pos.matches(Data9.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data9.getrating());

    } else if (pos.matches(Data10.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data10.getrating());

    } else if (pos.matches(Data11.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data11.getrating());

    } else if (pos.matches(Data12.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data12.getrating());

    } else if (pos.matches(Data13.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data13.getrating());

    } else if (pos.matches(Data14.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data14.getrating());

    } else if (pos.matches(Data15.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data15.getrating());

    } else if (pos.matches(Data16.getName())) {
        imageView.setImageResource(KuchenIcon);
        ratingbar1.setRating(Data16.getrating());

    } else if (pos.matches(Data17.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data17.getrating());

    } else if (pos.matches(Data18.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data18.getrating());

    } else if (pos.matches(Data19.getName())) {
        imageView.setImageResource(ApfelkandiertIcon);
        ratingbar1.setRating(Data19.getrating());

    } else if (pos.matches(Data20.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data20.getrating());

    } else if (pos.matches(Data21.getName())) {
        imageView.setImageResource(MuffinIcon);
        ratingbar1.setRating(Data21.getrating());

    } else if (pos.matches(Data22.getName())) {
        imageView.setImageResource(KuchenIcon);
        ratingbar1.setRating(Data22.getrating());

    } else if (pos.matches(Data23.getName())) {
        imageView.setImageResource(KuchenIcon);
        ratingbar1.setRating(Data23.getrating());

    } else if (pos.matches(Data24.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data24.getrating());

    } else if (pos.matches(Data25.getName())) {
        imageView.setImageResource(KuchenIcon);
        ratingbar1.setRating(Data25.getrating());

    } else if (pos.matches(Data26.getName())) {
        imageView.setImageResource(KuchenIcon);
        ratingbar1.setRating(Data26.getrating());

    } else if (pos.matches(Data27.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data27.getrating());

    } else if (pos.matches(Data28.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data28.getrating());

    } else if (pos.matches(Data29.getName())) {
        imageView.setImageResource(PlaetzchenIcon);
        ratingbar1.setRating(Data29.getrating());

    } else if (pos.matches(Data30.getName())) {
        imageView.setImageResource(KuchenIcon);
        ratingbar1.setRating(Data30.getrating());


    }
    else {
        imageView.setImageResource(OthersIcon);
    }

    return rowView;
}








} 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are doing quite a lot that is not necessary. I reduced the code and added some comments. In short:

  1. Use the position to find which data should be fetched (it could also be a prefetched list which would reduce the fetch from database to 1 query)
  2. You should use the ViewHolder pattern to recycle and reuse views -> quite some performance improvement
  3. You need to do "static" stuff outside the getView() method because stuff like loading a Typeface is quite slow.

Hope the below code helps you!

public class RezeptArrayAdapter extends ArrayAdapter<String> {

    public final Context context;
    public final String[] values;
    final DBHandler_Rezepte db_Rzpt;
    //TYPEFACE///////////
    Typeface tf;


    public RezeptArrayAdapter(Context context, String[] values) {
        super(context, R.layout.list_rezepte, values);
        this.context = context;
        this.values = values;

        // Do "static/reusable" stuff here
        // DatenBank Abfrage Angepasst auf den Array "context" View
        db_Rzpt = new DBHandler_Rezepte(context);
        //TYPEFACE///////////
        tf = Typeface.createFromAsset(context.getAssets(), "fonts/OFLGoudyStMTT.ttf");
    }

    // will be called quite often! Make it fast as possible!
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //Einzelabfrage der Datenbank Ratings damit Sie weiter unten verarbeitet werden k?nnen
        ConDB_Rezepte data = db_Rzpt.getConDB_Rezepte(position);

        // To not do too much at once this tip for later: Use the ViewHolder pattern
        View rowView = inflater.inflate(R.layout.list_rezepte, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        RatingBar ratingbar1 = (RatingBar) rowView.findViewById(R.id.ratingBar1);

        //stellt ein das man die Ratingbar nicht clicken kann, somit bleibt die Steuerung erhalten. Sonst ist die Liste nicht mehr clickbar
        ratingbar1.setIsIndicator(true);

        textView.setText(values[position]);

        textView.setTypeface(tf);


        // Change icon based on name
        String pos = values[position];

        // data should contain the information which icon should be shown!
        //int KuchenIcon = R.drawable.ic_kuchen;
        //int MuffinIcon = R.drawable.ic_muffin;
        //int PlaetzchenIcon = R.drawable.ic_plaetzchen;
        //int ApfelkandiertIcon = R.drawable.ic_apfelkandiert;
        //int OthersIcon = R.drawable.ic_others;

        // apply values/data to the view
        imageView.setImageResource(data.getIcon());
        ratingbar1.setRating(data.getrating());

        return rowView;
    }

}

Update:

For the problem with the icon, the solution might be pretty easy: You can get the icon by name. All you need to do is storing the name of the icon in the database.

When you have done that, you can use the following method to load the drawables:

public int getDrawableResource(String drawableName){
    return getResources().getIdentifier(drawableName, "drawable", getActivity().getPackageName());
}

So all you need is to store the icon name in the database so that data.getIcon() returns the resource id you get by using the above method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...