Max Zonov Max Zonov - 1 year ago 104
Android Question

ArrayList java error

Firstly, sorry for my English.

So, I have to write an app with ListView. It have to work: you are clicking on item and it direct you into the second activity. There must be information from JSON. I done it with okHttp. I'm not far from my destiny, but here is an error. SOLVED.
Next problem: there is no reaction on clicking on ListView item. (part of problem code):

public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayList<String> list = new ArrayList<>();
private ArrayAdapter<String> adapter;
private List<MyDataModel> datas = new ArrayList<>();

public class MyClickListener implements AdapterView.OnItemClickListener{
List<MyDataModel> datas;

public MyClickListener(List<MyDataModel> datas){
this.datas = datas;

public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Intent intent = new Intent(MainActivity.this, DetailsActivity.class);

if(position < datas.size()){
MyDataModel data = datas.get(position);

datas.add(new MyDataModel());


protected void onCreate(Bundle savedInstanceState) {

Toolbar toolbar = (Toolbar) findViewById(;

list = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,, list);

listView = (ListView) findViewById(;
listView.setOnItemClickListener(new MyClickListener(datas));

And code of

public class DetailsActivity extends AppCompatActivity{

private TextView textViewId;
private TextView textViewAbout;
private TextView textViewAddress;
private TextView textViewCompany;
private TextView textViewEmail;
private TextView textViewFirstName;
private TextView textViewLastName;
private TextView textViewPhone;

protected void onCreate(Bundle savedInstanceState) {

textViewId = (TextView) findViewById(;
textViewAbout = (TextView) findViewById(;
textViewAddress = (TextView) findViewById(;
textViewCompany = (TextView) findViewById(;
textViewEmail = (TextView) findViewById(;
textViewFirstName = (TextView) findViewById(;
textViewLastName = (TextView) findViewById(;
textViewPhone = (TextView) findViewById(;

Intent intent = getIntent();

textViewId.setText(String.valueOf(intent.getIntExtra(Key.KEY_ID, 0)));

And I think it will be useful to give you

public class MyDataModel {
private String about;
private String address;
private String company;
private String email;
private String firstname;
private String lastname;
private String phone;
private int itemId;
private String photo;

public String getAbout(){
return about;
public void setAbout(String about){
this.about = about;

public String getAddress(){
return address;
public void setAddress(String address){
this.address = address;

public String getEmail(){
return email;
public void setEmail(String email){ = email;

public String getFirstName(){
return firstname;
public void setFirstName(String firstname){
this.firstname = firstname;

public String getCompany(){
return company;
public void setCompany(String company){ = company;

public String getLastName(){
return lastname;
public void setLastName(String lastname){
this.lastname = lastname;

public String getPhone(){
return phone;
public void setPhone(String phone){ = phone;

public int getItemId(){
return itemId;
public void setItemId(int itemId){
this.itemId = itemId;

public String getPhoto(){
return photo;
public void setPhoto(String photo){ = photo;

Answer Source

You are not inserting data in private List<MyDataModel> datas = new ArrayList<>();. And I think you should not put datas = new ArrayList<MyDataModel>(); in ononItemClick because it empty the array, each time it is called.

Data should be added in datas ArrayList something like datas.add(new MyDataModel());

You should pass same ArratList to ArrayAdapter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download