Reputation: 97
Unfortunately after listView is full it does not scroll. why? Everything else just works fine. I feel I'm gonna break down and cry. listview is not scrolling individually. I have following class:
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import co.uk.aq.application.R;
import co.uk.aq.application.core.ApplicationGlobalState;
import co.uk.aq.application.core.WifiDataSets;
import co.uk.aq.application.ui.qActivity;
import com.google.inject.Inject;
import roboguice.inject.InjectView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class WifiFeedbackActivity extends qActivity {
@Inject private ApplicationGlobalState applicationGlobalState;
private RealtimeUpdateScreenCS realtimeUpdateCurrentState;
@InjectView(R.id.list_of_strings) private ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wifi_feedback);
setTitle(getString(R.string.wifi_current_connection));
}
@Override
public void onResume() {
super.onResume();
realtimeUpdateCurrentState = new RealtimeUpdateScreenCS();
realtimeUpdateCurrentState.execute();
}
@Override
public void onPause() {
super.onPause();
if (realtimeUpdateCurrentState != null){
realtimeUpdateCurrentState.cancel(true);
realtimeUpdateCurrentState = null;
}
}
private class RealtimeUpdateScreenCS extends AsyncTask{
String [] from = new String[]{"Details"};
int [] to = new int []{R.id.wan_details_text};
List<HashMap<String, String>> fillMaps;
@Override
protected Object doInBackground(Object... arg0) {
while(!isCancelled()){
fillMaps = new ArrayList<HashMap<String, String>>();
try {
for(String line: WifiDataSets.currentStateStrings){
if(!line.equals("\n")&& !line.equals(" ") && !line.equals("")){
HashMap<String, String> map = new HashMap<String, String>();
map.put("Details", line);
fillMaps.add(map);
}
}
publishProgress(fillMaps);
} catch (Exception e) {
Log.e("","",e);
}
try{Thread.sleep(50L);}catch(Exception e){}
}
return null;
}
protected void onProgressUpdate (Object...objects ){
SpecialAdapter adapter = new SpecialAdapter(getApplicationContext(), (List)objects[0], R.layout.grid_item, from, to);
lv.setClickable(false);
lv.setAdapter(adapter);
}
}
private class SpecialAdapter extends SimpleAdapter {
private int[] colors = new int[] {/*DARK_BLUE*/ new Color().rgb(14,41,179), /*RED*/ new Color().rgb(224,0,0), /*LIGHT_BLUE*/ new Color().rgb(0,153,255), /*GREEN*/ new Color().rgb(81,191,17)};
private List<HashMap<String, String>> the_list;
public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
this.the_list = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = 2 ;
if(the_list.get(position).get("Details").contains("successfully")
|| the_list.get(position).get("Details").contains("succeeded")
|| the_list.get(position).get("Details").contains("local IP address")
|| the_list.get(position).get("Details").contains("remote IP address")
|| the_list.get(position).get("Details").trim().contains("primary DNS address")
|| the_list.get(position).get("Details").trim().contains("secondary DNS address")
|| the_list.get(position).get("Details").trim().contains(applicationGlobalState.getString(R.string.wan_authentication_not_needed)))
colorPos = 3;
if(the_list.get(position).get("Details").contains("sent"))
colorPos = 2;
if(the_list.get(position).get("Details").contains("rcvd"))
colorPos = 0;
view.setBackgroundColor(colors[colorPos]);
return view;
}
}
}
And Layout is:
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_of_strings"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#000000"
android:dividerHeight="1px"/>
Upvotes: 0
Views: 175
Reputation: 1457
You are in a loop inside the AsyncTask by while(!isCancelled()) where call publishProgress every 50ms, then onProgressUpdate sets the adapter every single 50ml to the listview and so the listview is reseted. This is why you cannot scroll. For me, this code is all wrong, for example you should query the item inside the getView once the_list.get(position), not for every single check.
Upvotes: 1