I am learning a prototype in Android Studio and I have reached the latch for which I cannot find the answers.
I have an Activity that displays a custom ArrayAdapter as a ListView. I can edit items in a ListView by clicking on them and typing in the resulting AlertDialog. There is also an add button that I can click, and it calls a similar AlertDialog, but when I am, saving is not added to the ListView. How to get AlertDialog text input to save as a new ArrayAdapter element?
Most of the examples I found created an instance of ArrayAdapter directly in Activity, and not through Fragment, as I did.
MainActivity.java
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) {
MainActivityFragment.java
public class MainActivityFragment extends Fragment { @BindView(R.id.lvUsers) ListView listView; public MainActivityFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_main, container, false); ButterKnife.bind(this, view); ArrayList<User> arrayOfUsers = new ArrayList<User>(); arrayOfUsers.add(new User("Person 1", "Hometown 1")); arrayOfUsers.add(new User("Person 2", "Hometown 2")); arrayOfUsers.add(new User("Person 3", "Hometown 3")); arrayOfUsers.add(new User("Person 4", "Hometown 4")); arrayOfUsers.add(new User("Person 5", "Hometown 5"));
CustomUsersAdapter.java
public class CustomUsersAdapter extends ArrayAdapter<User> { private ArrayList<User> users; public CustomUsersAdapter(Context context, ArrayList<User> users) { super(context, 0, users); this.users = users; } @Override public View getView(final int position, View convertView, ViewGroup parent) {
ListView layout (from main_xml fragment)
<ListView android:id="@+id/lvUsers" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" > </ListView>