滚动时如何停止更新列表视图?

时间:2016-02-18 12:45:00

标签: android listview baseadapter listviewitem viewgroup

我的BaseAdapter工作正常但是当我滚动列表视图时,每个项目都是双精度。我不知道怎么能阻止它。

以下是来自PHP服务器的数组。 PHP服务器将其作为json字符串发送。

Array
(
    [0] => Array
        (
            [i] => 122:133:155:201:159:113:153:202:220:222:221:225:109
            [j] => 8
            [r] => 9a
        )

    [1] => Array
        (
            [i] => 205:182:221
            [j] => 9
            [r] => 10
        )

    [2] => Array
        (
            [i] => 
            [j] => 10
            [r] => 11
        )

    [3] => Array
        (
            [i] => 101:102:132
            [j] => 12
            [r] => 13
        )

)

这是活动:

public class Strefa extends Activity {

    AdapterStrefa mListAdapter;

    ListView listview;
    ArrayList<ListData> myList = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(GlobalConfig.tryb) {
            setTheme(R.style.AppTheme_Dark);
            setContentView(R.layout.n_strefa);
        } else {
            //setContentView(R.layout.d_statystyki);
        }

        GlobalConfig config = new GlobalConfig();
        JSONArray JsonArray = config.getJsonArray();

        for (int i = 0; i < JsonArray.length(); i++) {

            try {
                JSONObject jsonObject = JsonArray.getJSONObject(i);

                int r = jsonObject.getInt("j");

                ListData ld = new ListData();
                ld.setAdres(jsonObject.getString("i"));
                ld.setStrefa(jsonObject.getString("r") + ":");
                myList.add(ld);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        listview = (ListView) findViewById(R.id.listView);

        mListAdapter = new AdapterStrefa(this, myList);
        listview.setAdapter(mListAdapter);

        /* Powrót */

        Button Back = (Button) findViewById(R.id.back);

        Back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                finish();

            }
        });

    }

}

这是我的BaseAdaper:

public class AdapterStrefa extends BaseAdapter {

    ArrayList myList = new ArrayList();
    LayoutInflater inflater;
    Context context;


    public AdapterStrefa(Context context, ArrayList myList) {
        this.myList = myList;
        this.context = context;
        inflater = LayoutInflater.from(this.context);
    }

    @Override
    public int getCount() {
        return myList.size();
    }

    @Override
    public ListData getItem(int position) {
        return (ListData) myList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        MyViewHolder mViewHolder;

        if(convertView == null) {

            if(GlobalConfig.tryb) {
                convertView = inflater.inflate(R.layout.n_strefa_item, parent, false);
            } else {
                convertView = inflater.inflate(R.layout.d_zlecenia_lista, parent, false);
            }

            mViewHolder = new MyViewHolder(convertView);
            convertView.setTag(mViewHolder);

        }else {

            mViewHolder = (MyViewHolder) convertView.getTag();

        }


        ListData currentListData = getItem(position);

        mViewHolder.strefa.setText(currentListData.getStrefa());
        mViewHolder.dzielnica.setText(currentListData.getDzielnica());
        //mViewHolder.layout.setText(currentListData.getAdres());


        String CurrentString = currentListData.getAdres();
        String[] separated = CurrentString.split(":");

        for(int i = 0; i < separated.length; i++) {

            if(!separated[i].isEmpty()) {
                Button button = new Button(context, null, android.R.attr.buttonStyleSmall);
                button.setText(context.getString(R.string.string, separated[i]));
                mViewHolder.layout.addView(button);
            }
        }

        return convertView;
    }

    private class MyViewHolder {
        TextView strefa, dzielnica;
        LinearLayout layout;

        public MyViewHolder(View item) {
            strefa = (TextView) item.findViewById(R.id.strefa);
            dzielnica = (TextView) item.findViewById(R.id.dzielnica);
            layout = (LinearLayout) item.findViewById(R.id.layout);
        }
    }
}

我希望每个项目都能从json字符串创建按钮。例如当数组json的第一个elemnt有一个字符串:205:182:221时,必须在item中创建三个按钮。当我滚动listview时,每个项目都是双倍的。我怎么能停止双倍?

1 个答案:

答案 0 :(得分:1)

添加

mViewHolder.layout.removeAllViews();

for方法内的getView()循环之前。