滚动时如何停止更新列表视图?
How can I stop update listview when is scrolling?
我的 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
)
)
这是Activity:
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 的第一个元素有一个字符串:205:182:221 时,必须在项目中创建三个按钮。当我滚动列表视图时,每个项目都是双倍的。我怎样才能停止加倍?
添加
mViewHolder.layout.removeAllViews();
就在 getView()
方法内的 for
循环之前。
我的 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
)
)
这是Activity:
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 的第一个元素有一个字符串:205:182:221 时,必须在项目中创建三个按钮。当我滚动列表视图时,每个项目都是双倍的。我怎样才能停止加倍?
添加
mViewHolder.layout.removeAllViews();
就在 getView()
方法内的 for
循环之前。