Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

marcel shimabukuro
670 PointsWhich custom adapter to use?
I`m trying to build my own simple example of an app that has a ListView and parcelables interaction.
The app has a first Activity with 3 EditTexts where the user inputs his name, address and country. On the bottom there is a button.
The Button should send the data the user inputed through an extra to a new activity and call the new activity. This new activity should have a ListView with custom list items with the user`s name, address and country - the way he typed on the first Activity.
I would like to send this user data from one activity to the other using parcelables. (I know I dont need it in this case but I
m doing this to practice for more complex examples).
Question; 1- in which format should I send this data to the 2nd activity? String, List, ArrayList, Array of Strings?
2- what type of custom adapter should I use and what kind of code should this adapter contain?
I tried to copy some code from the stormy example from the parcelables section but it did not work.
Thanks!!
2 Answers

Jon Kussmann
Courses Plus Student 7,254 PointsHi Marcel,
It's good that you recognize that you do not need to use Parcelable for this type of problem. If you wanted to practice, I would recommend creating a new class. Maybe something called User
public class User implements Parcelable{
private String mName;
private String mAddress;
private String mCountry;
public User(String name, String address, String country) {
mName = name;
mAddress = address;
mCountry = country;
}
//practice adding your parcelable code here
}
Then in your first activity you can construct your new User when the user clicks the button and add that User object to your Intent.
I tend to use RecyclerView.Adapter. I'm not sure if you've gotten that far however. A simple array adapter would work well, and you can just make a new array of Strings that contain each of the above properties in whatever order you want.

marcel shimabukuro
670 PointsThanks Jon but I still can`t seem to make it on my own. There is the code. I would really appreciate if you could help me!
*** MAIN ACTIVITY ***
package com.example.marcelshimabukuro.treino_lista_inflater_adapter_bean;
import android.content.Context; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
public Context mContext;
public EditText etNome, etEndereco, etPais;
public Button mButton;
public ArrayList<Usuarios> mUsuariosArrayList;
public String mNome, mEndereco, mPais;
public String[] mDadosUsuario;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
loadComponents();
loadDadosDeUsuario();
loadButtonActions();
}
private void loadComponents() {
mButton = (Button) findViewById(R.id.btActivity2);
etNome = (EditText) findViewById(R.id.etNome1);
etEndereco = (EditText) findViewById(R.id.etEndereco1);
etPais = (EditText) findViewById(R.id.etPais1);
}
public Usuarios[] loadDadosDeUsuario() {
Usuarios[] usuariosArray = new Usuarios[3];
for (int i = 0; i < 3; i++) {
Usuarios usuarios = new Usuarios();
usuarios.setNome(etNome.getText().toString());
usuarios.setEndereço(etEndereco.getText().toString());
usuarios.setPais(etPais.getText().toString());
usuariosArray[i] = usuarios;
}
return usuariosArray;
}
private void loadButtonActions() {
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, ListaActivity.class);
intent.putExtra("dadosdeusuario", loadDadosDeUsuario());
startActivity(intent);
}
});
}
}
*** USER CLASS ***
package com.example.marcelshimabukuro.treino_lista_inflater_adapter_bean;
import android.os.Parcel; import android.os.Parcelable;
/**
- Created by marcelshimabukuro on 7/25/15. */
public class Usuarios implements Parcelable {
public String mNome, mEndereço, mPais;
public Usuarios() {
}
public String getNome() {
return mNome;
}
public void setNome(String nome) {
mNome = nome;
}
public String getEndereço() {
return mEndereço;
}
public void setEndereço(String endereço) {
mEndereço = endereço;
}
public String getPais() {
return mPais;
}
public void setPais(String pais) {
mPais = pais;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mNome);
dest.writeString(mEndereço);
dest.writeString(mPais);
}
private Usuarios (Parcel in) {
mNome = in.readString();
mEndereço = in.readString();
mPais = in.readString();
}
//Creator
public static final Creator<Usuarios> CREATOR = new Creator<Usuarios>() {
@Override
public Usuarios createFromParcel(Parcel source) {
return new Usuarios(source);
}
@Override
public Usuarios[] newArray(int size) {
return new Usuarios[size];
}
};
}
*** LISTACTIVITY ***
package com.example.marcelshimabukuro.treino_lista_inflater_adapter_bean;
import android.app.ListActivity; import android.content.Context; import android.content.Intent; import android.os.Parcelable; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import android.widget.ListView; import android.widget.Toast;
import java.util.ArrayList; import java.util.Arrays;
public class ListaActivity extends ListActivity {
public Context mContext;
public ListView mListView;
public Usuarios[] mUsuariosArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista);
mContext = this;
loadComponents();
loadList();
loadAdapter();
}
private void loadComponents() {
mListView = (ListView) findViewById(R.id.lvLista3);
}
private void loadList() {
Intent intent = getIntent();
Parcelable[] parcelables = intent.getParcelableArrayExtra("dadosdeusuario");
mUsuariosArray = Arrays.copyOf(parcelables, parcelables.length, Usuarios[].class);
}
private void loadAdapter() {
NewAdapter adapter = new NewAdapter(mUsuariosArray);
mListView.setAdapter(adapter);
}
}
*** CUSTOM ADAPTER ***
package com.example.marcelshimabukuro.treino_lista_inflater_adapter_bean;
import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView;
/**
-
Created by marcelshimabukuro on 8/10/15. */ public class NewAdapter extends ArrayAdapter<Usuarios> {
public Usuarios[] mUsuariosArray;
public NewAdapter(Usuarios[] usuariosArray){
mUsuariosArray = usuariosArray;
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder; if (convertView == null) { convertView = View.inflate(getContext(), R.layout.itens_de_lista, null); } return convertView;
}
public class ViewHolder {
public TextView mNome, mEndereço, mPais; public ViewHolder(View itemView) { mNome = (TextView) itemView.findViewById(R.id.tvNome); mEndereço = (TextView) itemView.findViewById(R.id.tvEndereço); mPais = (TextView) itemView.findViewById(R.id.tvPais); } public void bindUsuario(Usuarios usuarios){ mNome.setText(usuarios.mNome); mEndereço.setText(usuarios.mEndereço); mPais.setText(usuarios.mPais); }
}
}

marcel shimabukuro
670 Pointshello can anybody help me with that please??
thanks!!