java – ListView Scrolling Problem and Performance Tips

I want to know something… What’s wrong about my ListView, the problem is when I perform a fast scroll sometimes my application stopped…

In my MainActivity extending the a4 class —-> that extends Activity class:

    ArrayList<a> AL; 
    static ListView LV; 
    SearchView SV; 
    Toast TOAST; 
    TextView TV; 
    static boolean isEnglish;
    private static boolean isSearching, canClear; 
    static MultiList newAdapter;
    LinearLayout NV;
    static RelativeLayout.LayoutParams PR;
    ImageButton IB;
    ImageView IV;
    
    public void closeSearch(View view) {
        if (!isSearching) return; 
        TV.setVisibility(0);
        IB.setVisibility(8);
        SV.onActionViewCollapsed();
    PR.removeRule(9);       
    isSearching = false; 
    } 
 
    public void d(View view) {
        num(true, 5, 0, 8);
    } 
 
    public void e(View view) { 
        num(false, 6, 8, 0); 
    } 
 
    public void goInfo(View view) { 
    co(3); 
    } 
 
    public void goIns(View view) { 
    co(2); 
    } 
 
    public void goSet(View view) { 
    co(4); 
    } 
 
    public void goTools(View view) { 
    co(1); 
    } 
    
void num(boolean bl, final int n, int n2, int n3) { 
        int n4 = 0;
        isEnglish = bl;
        AL = new ArrayList<a>();
        newAdapter = new MultiList(this, true, AL);
findViewById(R.id.l1).setVisibility(n2);
findViewById(R.id.l2).setVisibility(n3);
        LV.setAdapter(newAdapter);
        LV.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 

            public void onItemClick(AdapterView<?> adapterView, View view, int ol, long l) { 
                rt(n, ol); 
            } 
        });
        do {
        if(bl){
        if(n4 >= songs_e.length || n4 >= singers_e.length) return;
        AL.add(new a(songs_e[n4], singers_e[n4]));
        } else {
        if(n4 >= artists.length) return;
        AL.add(new a(artists[n4])); 
        }
        ++n4;
        } while (true);      
    } 

    public void onBackPressed() {
    if(isSearching) {
    closeSearch(isViewed);
    } else {
        if (b0 < System.currentTimeMillis() - 1600L) {
            TOAST = Toast.makeText(this, "Tap again to exit " + app, 0);
            TOAST.show();
            b0 = System.currentTimeMillis();
            return; 
        } else { 
            if (TOAST != null) { 
          TOAST.cancel();
          finish();
          return;
          }
         }
        }
}

@Override
protected void onCreate(Bundle bundle) {
show(4, R.layout.slist, q + app, 0);
LV = findViewById(R.id.lv);
SV = findViewById(R.id.search);
TV = findViewById(R.id.mytv);
NV = findViewById(R.id.llp);
PR = (RelativeLayout.LayoutParams)SV.getLayoutParams();
IB = findViewById(R.id.search_close);        
IV = (ImageView)findViewById((View)SV, "android:id/search_close_btn");
final TextView actv = (TextView)findViewById(SV, "android:id/search_src_text");
actv.setBackgroundColor(-1);
ImageView imageView2 = (ImageView)findViewById((View)SV, "android:id/search_button");
        imageView2.setImageResource(R.drawable.ic_search); 
        imageView2.setBackgroundColor(-12369085);
LV.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){

public boolean onItemLongClick(AdapterView<?> av, View v, int n, long l){
return true;
}

});
IV.setOnClickListener(new View.OnClickListener(){ 
 
            public void onClick(View view) {
            if(canClear){
            actv.setText("");
            }           
            }
        });
d(isViewed);
newAdapter.initForMulti(R.layout.abc_listview, R.id.txt1, R.id.txt2, R.id.image, R.drawable.abc_tab);
SV.setLayoutParams(PR);
SV.setQueryHint("  Search songs and artists . . .");
SV.setOnSearchClickListener(new View.OnClickListener(){ 
 
            public void onClick(View view) {
            IV.setImageResource(R.drawable.search_clear_inactive);
            TV.setVisibility(8);
            canClear = false;
            isSearching = true;
            IB.setVisibility(0);
            PR.addRule(9);
            } 
        }); 

SV.setOnQueryTextListener(new SearchView.OnQueryTextListener(){ 

            public boolean onQueryTextChange(String string2) {
ArrayList<a> arrayList = new ArrayList<a>();
if(string2.equals((Object)"")) {
IV.setImageResource(R.drawable.search_clear_inactive);
canClear = false;
} else {
IV.setImageResource(R.drawable.search_clear_active);
canClear = true;
}

//Error for Tagalog, text is not for multi
for (a aa : MainActivity.this.AL) {
if (isEnglish && !aa.getFirst().toLowerCase().contains(string2) && !aa.getFirst().toUpperCase().contains(string2) && !aa.getFirst().contains(string2) && !aa.getLast().toLowerCase().contains(string2) && !aa.getLast().toUpperCase().contains(string2) && !aa.getLast().contains(string2) || !isEnglish && !aa.getLast().toLowerCase().contains(string2) && !aa.getLast().toUpperCase().contains(string2) && !aa.getLast().contains(string2)) continue;
arrayList.add(aa);
}
newAdapter.filterResult((ArrayList<a>)arrayList);
return false;
            } 
 
            public boolean onQueryTextSubmit(String string2) { 
                return true; 
            } 
        });
        MusicUtils.playAsset(this, "audios/a.ogg");
        IV.setBackgroundColor(-12369085);
super.onCreate(bundle);

} 
 
    public void onPause() {
    super.onPause();
    closeSearch(isViewed);
    if (in != 5 && in != 6) {
   LV.setVisibility(4);
   int n = 0;
            do {
            if (n >= 10) return;
                LV.scrollListBy(-999999999);
                ++n; 
            } while (true);
         }
    } 

//onRestart not included because it basically contains setVisibility
  final void rt(int n, int n2) { 
        in = n; 
        Bundle bundle = new Bundle(); 
        newAdapter.getLC(n2); 
        bundle.putString("First", newAdapter.list.getFirst()); 
        bundle.putString("Last", newAdapter.list.getLast()); 
        go(new Intent(this, ContProv.class).putExtras(bundle)); 
    }

And for the custom list adapter:

private static Context Context;
private static ArrayList<a> ab; 
private static int view, mText, mText1, mText2, mImage, mDrawable;
private static boolean listType;
public a list;

public MultiList(Context c, boolean isMultiText, ArrayList<a> array) {
if(c != null || array != null) {
Context = c;
listType = isMultiText;
ab = array;
  }
}

public final void initForMulti(int layout, int text1ID, int text2ID, int imageID, int imageDrawable) {
view = layout;
mText1 = text1ID;
mText2 = text2ID;
mImage = imageID;
mDrawable = imageDrawable;
}

public final void initForNonMulti(int layout, int textID, int imageID, int imageDrawable){
view = layout;
mText = textID;
mImage = imageID;
mDrawable = imageDrawable;
}

public void filterResult(ArrayList<a> arrayList) {
        ab = new ArrayList<a>(); 
        ab.addAll((ArrayList<a>)arrayList);
        notifyDataSetChanged();
    }

public int getCount() { 
return ab.size(); 
} 
 
public Object getItem(int n) { 
return null; 
} 
 
public long getItemId(int n) { 
return 0L;
} 

public a getLC(int n) {
return list = ab.get(n);
}

public View getView(int n, View v, ViewGroup vg) {
a newList = getLC(n);
v = View.inflate(Context, view, null);
((ImageView)v.findViewById(mImage)).setImageResource(mDrawable);
if(listType) {
((TextView)v.findViewById(mText1)).setText(newList.getFirst());
((TextView)v.findViewById(mText2)).setText(newList.getLast());
} else ((TextView)v.findViewById(mText)).setText(newList.getText());
return v;
   }

Thank you for your help… I will really appreciate it… Also I need tips about the performance of my existing code and please write your answer in java and don’t use support libraries cuz I want my application work for android phones below android 5…

Final:

I’ve found out that the problem is at my list adapter so I edited it into this:

package dn.widget;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import dn.widget.ListAdapter;
import dn.widget.a.a;
import java.util.ArrayList;

public class ListAdapter extends BaseAdapter {

private ArrayList<a> AR; 
private static int N, N2, N3, N4, N5, N6;
private boolean LT;
public a L;

public ListAdapter(ArrayList<a> ar, boolean b) {
AR = ar;
LT = b;
}

public void isDual(int n, int n2, int n3, int n4, int n5) {
if (LT) {
N = n;
N3 = n2;
N4 = n3;
N5 = n4;
N6 = n5;
  }
}

public void isSingle(int n, int n2, int n3, int n4){
if (!LT) {
N = n;
N2 = n2;
N4 = n3;
N5 = n4;
  }
}

public void filterResult(ArrayList<a> ar) {
if(AR != null) {
AR = new ArrayList<a>(); 
AR.addAll((ArrayList<a>)ar);
notifyDataSetChanged();
 }
}

public int getCount() { 
return AR.size(); 
}
 
public Object getItem(int n) { 
return null; 
} 
 
public long getItemId(int n) { 
return 0L;
}

public a getLC(int n) {
L = AR.get(n);
return L;
}

static class LH {
private ImageView I;
private TextView T, T1, T2;
}

@Override
public View getView(int n, View v, ViewGroup g) {
getLC(n);
final LH h;
if(v == null) {
v = View.inflate(g.getContext(), N, null);
h = new LH();
h.I = v.findViewById(N5);
h.T = v.findViewById(N2);
h.T1 = v.findViewById(N3);
h.T2 = v.findViewById(N4);
h.I.setImageResource(N6);
v.setTag(h);
} else {
h = (LH)v.getTag();
}
if(LT) {
h.T1.setText(L.f());
h.T2.setText(L.l());
} else h.T.setText(L.t());
return v;
   }
}

Leave a Comment