0

不幸的是,listView 已满后它不会滚动。为什么?其他一切都很好。我觉得我会崩溃并哭泣。listview 没有单独滚动。我有以下课程:

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;
    }
}
}

布局是:

 <?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"/>
4

1 回答 1

1

您通过 while(!isCancelled()) 在 AsyncTask 内部循环,其中每 50 毫秒调用一次 publishProgress,然后 onProgressUpdate 将每 50 毫升的适配器设置为列表视图,因此列表视图被重置。这就是您无法滚动的原因。对我来说,这段代码都是错误的,例如,您应该在 getView 中查询一次 the_list.get(position) 中的项目,而不是每次检查。

于 2013-10-17T16:44:51.797 回答