[Solved] Get element at the specified position in arraylist


If this was my issue, I would do the following:

Create a model class…

public class ListModel {

    View view;
    String url;

    public ListModel (View view, String url) {
        this.view = view;
        this.url = url;
    }

    public View getView() {
        return view;
    }

    public void setView(View view) {
        this.view = view;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

Make a list that takes in that class…

public class MainActivity extends AppCompatActivity {

    List<ListModel> listModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listModel = new ArrayList<>();

        listModel.add(new ListModel(view, "url"));



    }
}

From there you can create a for loop to get the view and / or the url..

    for (int i = 0; i < listModel.size(); i++) {
        View getView = listModel.get(i).getView();
        String getUrl = listModel.get(i).getUrl();
    }

Now, keep in mind, my code will NOT work because I didn’t create a VIEW to put into the list. But this is probably the best way to go about it.

solved Get element at the specified position in arraylist