1

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.

This is the code that I use to LoadFields (if can help):

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

}

This is my adapter class:

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

}

Simone
  • 311
  • 4
  • 16

1 Answers1

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.

=> Android doesn't provide any such methods through which you can go to the particular method via data used in the ListView. You have to iterate through the list item to find out list item position having the same item ID.

BTW, your code needs to be improved with following standard practices.

    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()]);

You should not take individual arrays for each data items, rather go for a POJO class. You should also give GSON library a try which helps you in preparing list of pojo objects directly from the given json response.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295