Max Zonov Max Zonov - 1 month ago 19
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.

MainActivity.java (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;
}

@Override
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);
intent.putExtra(Key.KEY_ID,data.getItemId());
intent.putExtra(Key.KEY_ABOUT,data.getAbout());
intent.putExtra(Key.KEY_ADDRESS,data.getAddress());
intent.putExtra(Key.KEY_COMPANY,data.getCompany());
intent.putExtra(Key.KEY_EMAIL,data.getEmail());
intent.putExtra(Key.KEY_FIRST_NAME,data.getFirstName());
intent.putExtra(Key.KEY_LAST_NAME,data.getLastName());
intent.putExtra(Key.KEY_PHONE,data.getPhone());

}
datas.add(new MyDataModel());
}

}

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

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

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

listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new MyClickListener(datas));


And code of DetailsActivity.java:

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;

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

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

Intent intent = getIntent();

textViewId.setText(String.valueOf(intent.getIntExtra(Key.KEY_ID, 0)));
textViewAbout.setText(intent.getStringExtra(Key.KEY_ABOUT));
textViewAddress.setText(intent.getStringExtra(Key.KEY_ADDRESS));
textViewCompany.setText(intent.getStringExtra(Key.KEY_COMPANY));
textViewEmail.setText(intent.getStringExtra(Key.KEY_EMAIL));
textViewFirstName.setText(intent.getStringExtra(Key.KEY_FIRST_NAME));
textViewLastName.setText(intent.getStringExtra(Key.KEY_LAST_NAME));
textViewPhone.setText(intent.getStringExtra(Key.KEY_PHONE));
}
}


And I think it will be useful to give you MyDataModel.java:

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){
this.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){
this.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){
this.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){
this.photo = photo;
}
}

Answer

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.