我从JSON填充listview,我需要使用自定义适配器滚动到特定项目。我没有职位,因此我无法使用此post中提到的程序。
在这个结果JSON中,有一个字段ID,所以我想滚动到具有相应id的项目。
这是我用于LoadFields的代码(如果有帮助的话):
private void LoadData(String outputJSON) {
try {
calenList.clear();
ParseJson(outputJSON);
String[] ids = calId.toArray(new String[calId.size()]);
String[] date = calData.toArray(new String[calData.size()]);
String[] ore = calOra.toArray(new String[calOra.size()]);
String[] isNexts = calIsNext.toArray(new String[calIsNext.size()]);
for (int i = 0; i < calId.size(); i++) {
Calendario calendario = new Calendario();
calendario.setId(ids[i]);
calendario.setData(date[i]);
calendario.setOra(ore[i]);
calendario.setIsNext(isNexts[i]);
if (isNexts[i].equals("1")) {
calendario.setType(1);
} else
calendario.setType(0);
calenList.add(calendario);
adapter.updateData(calenList);
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
shareResult(position);
}
});
} catch (JSONException e) {
e.printStackTrace();
Log.d(TAG, e.toString());
Log.d(TAG, e.getMessage());
}
}
这是我的适配器类:
public class CalendarioAdapter extends BaseAdapter {
private ArrayList<Calendario> listData;
private LayoutInflater layoutInflater;
public CalendarioAdapter(Context context, ArrayList listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return listData.get(position).getType();
}
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
ViewHolder holder;
int type = getItemViewType(position);
if (convertView == null) {
if (type == 0)
convertView = layoutInflater.inflate(R.layout.calendario_row_layout, null);
else
convertView = layoutInflater.inflate(R.layout.calendario_row_layout_next, null);
holder = new ViewHolder();
holder.edData = (TextView) convertView.findViewById(R.id.edData);
holder.edOra = (TextView) convertView.findViewById(R.id.edOra);
holder.edSqCasa = (TextView) convertView.findViewById(R.id.edSqCasa);
holder.edSqTras = (TextView) convertView.findViewById(R.id.edSqTrasf);
holder.edRisult = (TextView) convertView.findViewById(R.id.edRisultato);
holder.imLogoCasa = (ImageView) convertView.findViewById(R.id.imLogoCasa);
holder.imLogoTras = (ImageView) convertView.findViewById(R.id.imLogoTras);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Calendario calendario = (Calendario) listData.get(position);
holder.edData.setText(calendario.getData());
holder.edOra.setText(calendario.getOra());
holder.edSqCasa.setText(calendario.getSqCasa());
holder.edSqTras.setText(calendario.getSqTras());
holder.edRisult.setText(calendario.getPuntiCasa() + " - " + calendario.getPuntiTras());
if (holder.imLogoCasa != null) Ion.with(holder.imLogoCasa).load(calendario.getLogoCasa());
if (holder.imLogoTras != null) Ion.with(holder.imLogoTras).load(calendario.getLogoTras());
return convertView;
}
static class ViewHolder {
TextView edData;
TextView edOra;
TextView edSqCasa;
TextView edSqTras;
TextView edRisult;
ImageView imLogoCasa;
ImageView imLogoTras;
}
public void updateData(ArrayList<Calendario> updatedData) {
listData = updatedData;
this.notifyDataSetChanged();
}
}
答案 0 :(得分:0)
I fill a listview from a JSON and I need to scroll to a specific item, with a custom adapter. I don't have a position so I can't use the procedures mentioned in this post.
In this result JSON, there is a field ID, so i want to scroll to the item that has got the corresponding id.
=&GT; Android没有提供任何此类方法,您可以通过这些方法通过ListView中使用的数据转到特定方法。您必须遍历列表项以查找具有相同项ID的列表项位置。
顺便说一下,您的代码需要通过以下标准做法进行改进。 String[] ids = calId.toArray(new String[calId.size()]);
String[] date = calData.toArray(new String[calData.size()]);
String[] ore = calOra.toArray(new String[calOra.size()]);
String[] isNexts = calIsNext.toArray(new String[calIsNext.size()]);
您不应该为每个数据项采用单独的数组,而是选择POJO类。您还应该尝试一下GSON库,它可以帮助您直接从给定的json响应中准备pojo对象列表。