android - smooth scrolling listview with data from URL

I have several widgets in the convertView of my listview, and all the data is parsed from json. First I form arrays from respectively parsed data and then in my ArrayAdapter I work with those final arrays. But everytime I scroll the listview, the app goes to server for the respective information (as I see in logs), despite it should have already been stored in arrays. Maybe, it's one of the reasons my listView scrolling is very slow. What's the problem?

My code for ArrayAdapter:

public class CustomAdapterSecond extends ArrayAdapter<String> {

private final Activity context;
private final String[] sSecondStartTimeArray;
private final String[] sSecondEndTimeArray;
private final String[] sSecondTitleArray;
public CustomAdapterSecond(Activity context,
                     String[] sSecondStartTimeArray, String[] sSecondEndTimeArray, String[] sSecondTitleArray) {
    super(context, R.layout.relcell2, sSecondStartTimeArray);
    this.context = context;
    this.sSecondStartTimeArray = sSecondStartTimeArray;
    this.sSecondEndTimeArray = sSecondEndTimeArray;
    this.sSecondTitleArray = sSecondTitleArray;
}

static class ViewHolder {
    public TextView textStartTime;
    public TextView textEndTime;
    public TextView textUpper;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    String AllData2 = HTTPRequest.GetData(URL);
    ViewHolder holder;
    if (convertView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        convertView = inflater.inflate(R.layout.relcell2main,parent,false);
        holder = new ViewHolder();
        holder.textStartTime = (TextView)convertView.findViewById(R.id.textStartTime);
        holder.textEndTime = (TextView)convertView.findViewById(R.id.textEndTime);
        holder.textUpper = (TextView)convertView.findViewById(R.id.textUpper);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder)convertView.getTag();
    }
    holder.textStartTime.setText(sSecondStartTimeArray[position]);
    holder.textEndTime.setText(sSecondEndTimeArray[position]);
    holder.textUpper.setText(sSecondTitleArray[position]);

    final int chosen = position;
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int chosenP = chosen;
            if (chosenP != 0) {
                Intent intent = new Intent(context, MainThird.class);
                intent.putExtra("chosenItem", chosenP);
                context.startActivity(intent);                   
            }
        }
    });
    return convertView;
}

And code for my Activity where I form the arrays:

public class MainSecond extends AppCompatActivity {

ListView listViewLeft;
ListView listViewRight;
int secondChosen=-1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainsecond);

    secondChosen = getIntent().getExtras().getInt("chosenItem");
    final String sStartDate = getIntent().getExtras().getString("startDate");
    String sEndDate = getIntent().getExtras().getString("endDate");
    final String AllData1 = HTTPRequest.GetData(URL1);

    TabHost tabs = (TabHost) findViewById(android.R.id.tabhost);
    tabs.setup();
    TabHost.TabSpec spec = tabs.newTabSpec("tag1");
    spec.setContent(R.id.tab1);
    spec.setIndicator("Caption1");
    tabs.addTab(spec);
    spec = tabs.newTabSpec("tag2");
    spec.setContent(R.id.tab2);
    spec.setIndicator("Caption2");
    tabs.addTab(spec);
    tabs.setCurrentTab(0);

    final String AllData2 = HTTPRequest.GetData(URL);

    TabHost tabs2 = (TabHost) findViewById(R.id.tabhost2);
    tabs2.setup();
    TabHost.TabSpec spec2 = tabs2.newTabSpec("tag12");
    spec2.setContent(R.id.tab12);
    spec2.setIndicator(sStartDate);
    tabs2.addTab(spec2);
    if (!(sStartDate.equals(sEndDate))) {
        spec2 = tabs2.newTabSpec("tag22");
        spec2.setContent(R.id.tab22);
        spec2.setIndicator(sEndDate);
        tabs2.addTab(spec2);
        tabs2.setCurrentTab(0);
        listViewRight = (ListView)findViewById(R.id.listViewRight);
    }

    listViewLeft = (ListView)findViewById(R.id.listViewLeft);

    //TODO:CreateListView
    int leng = HTTPRequest.GetLength(AllData2);
    int lengLeft=0;
    for (int i=0;i<leng;i++) {
        if (HTTPRequestSecond.GetSecondStartFecha(AllData2,i).equals(sStartDate)) lengLeft++;
    }
    ArrayList<String>sSecondStartTime=new ArrayList<>();
    ArrayList<String>sSecondEndTime=new ArrayList<>();
    ArrayList<String>sSecondTitle=new ArrayList<>();
    for (int j=0;j<lengLeft;j++) {
        sSecondStartTime.add(j,HTTPRequestSecond.GetSecondStartTime(AllData2,j));
        sSecondEndTime.add(j,HTTPRequestSecond.GetSecondEndTime(AllData2, j));
        sSecondTitle.add(j,HTTPRequestSecond.GetSecondTitle(AllData2, j));
    }
    String[] sSecondStartTimeArra = new String[sSecondStartTime.size()];
    sSecondStartTimeArra = sSecondStartTime.toArray(sSecondStartTimeArra);
    String[] sSecondEndTimeArra = new String[sSecondEndTime.size()];
    sSecondEndTimeArra = sSecondEndTime.toArray(sSecondEndTimeArra);
    String[] sSecondTitleArra = new String[sSecondTitle.size()];
    sSecondTitleArra = sSecondTitle.toArray(sSecondTitleArra);
    final String[] sSecondStartTimeArray = sSecondStartTimeArra;
    final String[] sSecondEndTimeArray = sSecondEndTimeArra;
    final String[] sSecondTitleArray = sSecondTitleArra;
    CustomAdapterSecond adapter2 = new
            CustomAdapterSecond(MainSecond.this,sSecondStartTimeArray,sSecondEndTimeArray,sSecondTitleArray);
    listViewLeft.setAdapter(adapter2);
}

I messed up at the end but I just wanted to make the arrays final (thought it would help not to use the Internet during every scrolling) so that's why it looks so dumb way :(

Answers


In your adapter getView(), the first line is calling again the remote service. Comment it and try again

public View getView(int position, View convertView, ViewGroup parent) {
    // String AllData2 = HTTPRequest.GetData(URL);
...

Need Your Help

Regex validation for 'Does Not Start With' in MVC

regex asp.net-mvc-5

OK I am trying to use Regex to validate a model property in MVC5.

Server control width won't apply with IE11 ?

html asp.net .net-4.0 cross-browser

I have this simple server control with a valid Width attribute : ( asp.net 4 , vs2010 )