I'm pretty new to Android and I need some guidance.
I have two activities so far. The first one contains an ActionBar and a ListView. I want the ListView to be populated with rows containing a TextView and a CheckBox. The text is represented by an username read from a database.The ActionBar has buttons for actions: "new" and "delete". "new" opens a new activity which adds a new user into the database, delete action should delete checked entries from the list and database. Screens:
Main activity: https://i.sstatic.net/S3y0r.jpg
Create profile activity: https://i.sstatic.net/IyKYA.jpg
To get these I used SQLiteHelper and a datasource for the database and a custom ArrayAdapter for the ListView. I have one problem and some concerns, listed below.
1 . The problem: when I add a new user, I can only see the changes in the ListView after I exit the app using the back button.
2 . Is a custom ArrayAdapter a good solution for this or should I use a CursorAdapter? When clicked, the list item should use the information associated with it into another activity/fragment. Below is my custom adapter. It works right now, but the next step is to implement the delete action, and i'm not entirely sure how I do that. This is where i need advice. Thank you!
public class CustomUserAdapter extends ArrayAdapter<UserProfile>{
ArrayList<UserProfile> users;
private LayoutInflater inflater;
public CustomUserAdapter(Context context, ArrayList<UserProfile> users) {
// TODO Auto-generated constructor stub
super(context, R.layout.users_list_row, users);
this.users = users;
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
UserProfile user = users.get(position);
CheckBox checkBox;
TextView textView;
if(convertView == null){
convertView = inflater.inflate(R.layout.users_list_row, null);
checkBox = (CheckBox) convertView.findViewById(R.id.checkElement);
textView = (TextView) convertView.findViewById(R.id.textElement);
//tag row
convertView.setTag(new UserHolder(textView, checkBox));
} else {
UserHolder userHolder = (UserHolder) convertView.getTag();
checkBox = userHolder.getCheckBox();
textView = userHolder.getUserText();
}
//tag checkbox
checkBox.setTag(user);
//checkbox listener
checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
checkBox.setChecked(false);
textView.setText(user.getUsername());
return convertView;
}
}