【发布时间】:2023-03-19 17:15:01
【问题描述】:
我的 listview 的 convertView 中有几个小部件,所有数据都是从 json 解析的。首先,我从分别解析的数据形成数组,然后在我的 ArrayAdapter 中处理这些最终数组。但是每次我滚动列表视图时,应用程序都会转到服务器以获取相应的信息(正如我在日志中看到的那样),尽管它应该已经存储在数组中。也许,这是我的 listView 滚动很慢的原因之一。有什么问题?
我的 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;
}
以及我在其中形成数组的 Activity 的代码:
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);
}
最后我搞砸了,但我只是想让数组成为最终的(认为在每次滚动期间不使用 Internet 会有所帮助)所以这就是它看起来如此愚蠢的原因:(
【问题讨论】:
标签: android json listview android-arrayadapter smooth-scrolling