I really need some good hint.... i have been hours debugging this (((...
public class Travel {
public static String from, to;
Travel(String f, String t) {
this.from = f;
this.to = t;
}
}
final class NotesAdapter extendes ArrayAdapter<Travel>{
private final List<Travel> myC;
public NotesAdapter(Context context, int resource, List<Travel> myCt) {
super(context, resource, myCt);
this.myC = myCt;
}
@Override
public int getCount() {
return myC.size();
}
@Override
public Travel getItem(int position) {
return myC.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup adapterView) {
Travel trv = myC.get(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.my_lists, adapterView, false);
}
TextView from = (TextView)convertView.findViewById(R.id.tv_from);
TextView to = (TextView)convertView.findViewById(R.id.tv_to);
from.setText(trv.from);//.getMFrom()
to.setText(trv.to);//.getMTo()
return convertView;
}
In NotesAdapter i have implemented only the method you see here. Then in the main activity i launch startactivityforresult and in the onactivityresults i add elements to the List object.
List<Travel> myCities = new ArrayList<Travel>();
the List object is initialized as an ArrayList (...new ArrayList<>()...). I also have:
mList = (ListView)findViewById(R.id.for_lists);
na = new NotesAdapter(this,R.layout.my_lists,myCities);
So:
nCity = data.getStringExtra("content");
to collect the new string
myCities.add(new Travel("Home Town",nCity));
to add a new travel.... and:
na.notifyDataSetChanged();
mList.setAdapter(na);
this is the all code:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == MY_REQUEST_CODE){
if(resultCode == RESULT_OK){
nCity = data.getStringExtra("content");
if (!myCities.contains(nCity)) {
myCities.add(new Travel("Home Town",nCity));
na.notifyDataSetChanged();
} else {
Toast.makeText(this,"This City has already been added",Toast.LENGTH_LONG).show();
}
}
else {
//do something when user refused to complete action
}
hT.setText("Home Town");
iB.setText(infoBar);
na.notifyDataSetChanged();
mList.setAdapter(na);
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
else {
// not my business another request
}
}
When i use just a List of String all works perfectly. The result with a List of Travel is that all views in the listview show the last added travel.
For layout files i use basic implementations.
Please help... ^_^