How to add data to custom BaseAdapter for listView - Android

android, android-asynctask, android-listview, baseadapter

Solution

You try this :

public class Comment {
String username;
String content;
String number;
 }

Class Adapter:

public class CommentAdapter extends BaseAdapter {
private List<Comment> listComment;
private Context context;

public CommentAdapter(List<Comment> listComment, Context context) {
    super();
    this.listComment = listComment;
    this.context = context;
}

@Override
public int getCount() {

    return listComment.size();
}

@Override
public Comment getItem(int position) {
    return listComment.get(position);
}

@Override
public long getItemId(int arg0) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = mInflater.inflate(R.layout.comment_item, null);
    }

    final TextView textViewUsername = (TextView) v
            .findViewById(R.id.comment_Username);
    final TextView textViewNumber = (TextView) v
            .findViewById(R.id.comment_number);
    final TextView textViewContent = (TextView) v
            .findViewById(R.id.comment_Content);

    final String username = listComment.get(position).getUsername();
    final String number= listComment.get(position).getNumber();
    String content = listComment.get(position).getContent();

    textViewUsername.setText(username);
    textViewNumber.setText(number);

    textViewContent.setText(content);
    return v;
}

}

When you need to add new comment to list. just create new Comment and add to listComment(listComment.add(newComment)), after that, call adapter.notifyDataSetChanged();

Problem

I have a custom BaseAdapter class that creates views for comments, usernames, and numbers. This BaseAdapter receives this information from An AsyncTask. The AsyncTask runs when the user reaches the bottom of the listView. The problem is the BaseAdapter wont add new data. When I try to add new data it deletes the current data in the list and then adds the new data. I want it to keep all the data and just add data to the bottom of the listView. All of these classes are in the same Activity. Here is my current code. ``` class CreateCommentLists extends BaseAdapter{ Context ctx_invitation; String[] listComments; String[] listNumbers; String[] listUsernames; public CreateCommentLists(String[] comments, String[] usernames, String[] numbers, DashboardActivity context) { super(); ctx_invitation = context; listComments = comments; listNumbers = usernames; listUsernames = numbers; } @Override public int getCount() { if(null == listComments) { return 0; } // TODO Auto-generated method stub return listComments.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return listComments[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View v = null; try { String inflater = Context.LAYOUT_INFLATER_SERVICE; LayoutInflater li = (LayoutInflater)ctx_invitation.getSystemService(inflater); v = li.inflate(R.layout.list_item, null); TextView commentView = (TextView)v.findViewById(R.id.listComment); TextView NumbersView = (TextView)v.findViewById(R.id.listNumber); TextView usernamesView = (TextView)v.findViewById(R.id.listPostedBy); Button usernameButton = (Button)v.findViewById(R.id.listUsernameButton); Button numberButton = (Button)v.findViewById(R.id.listNumberButton); commentView.setText(listComments[position]); NumbersView.setText(listNumbers[position]); usernamesView.setText(listUsernames[position]); usernameButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent i = new Intent(getApplicationContext(), ProfileActivity.class); i.putExtra("usernameOfProfile",listUsernames[position]); startActivity(i); finish(); } }); numberButton.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { Intent i = new Intent(getApplicationContext(), ProfileActivity.class); i.putExtra("NumberProfile",listNumbers[position]); startActivity(i); finish(); } }); } catch(Exception e) { e.printStackTrace(); } return v; } public void add(String[] comments, String[] usernames, String[] numbers) { listComments = comments; listNumbers = usernames; listUsernames = numbers; } public int getCount1() { if(null == listComments) { return 0; } // TODO Auto-generated method stub return listComments.length; } public Object getItem1(int position) { // TODO Auto-generated method stub return listComments[position]; } public long getItemId1(int position) { // TODO Auto-generated method stub return 0; } public View getView1(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View v = null; try { String inflater = Context.LAYOUT_INFLATER_SERVICE; LayoutInflater li = (LayoutInflater)ctx_invitation.getSystemService(inflater); v = li.inflate(R.layout.list_item, null); TextView commentView = (TextView)v.findViewById(R.id.listComment); TextView NumbersView = (TextView)v.findViewById(R.id.listNumber); TextView usernamesView = (TextView)v.findViewById(R.id.listPostedBy); Button usernameButton = (Button)v.findViewById(R.id.listUsernameButton); Button numberButton = (Button)v.findViewById(R.id.listNumberButton); commentView.setText(listComments[position]); NumbersView.setText(listNumbers[position]); usernamesView.setText(listUsernames[position]); usernameButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent i = new Intent(getApplicationContext(), ProfileActivity.class); i.putExtra("usernameOfProfile",listUsernames[position]); startActivity(i); finish(); } }); numberButton.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { Intent i = new Intent(getApplicationContext(), ProfileActivity.class); i.putExtra("NumberProfile",listNumbers[position]); startActivity(i); finish(); } }); } catch(Exception e) { e.printStackTrace(); } return v; } final CreateCommentLists mycmlist = new CreateCommentLists(comments, usernames, numbers, DashboardActivity.this); lstComments = (ListView)findViewById(android.R.id.list); lstComments.setAdapter(mycmlist); final ProgressDialog progDailog = new ProgressDialog(DashboardActivity.this); class loadComments extends AsyncTask<JSONObject, String, JSONObject> { @Override protected void onPreExecute() { super.onPreExecute(); progDailog.setIndeterminate(false); progDailog.setCancelable(true); progDailog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); progDailog.show(); progDailog.setContentView(R.layout.progress_circle); } @Override protected void onProgressUpdate(String... values) { super.onProgressUpdate(values); } protected JSONObject doInBackground(JSONObject... params) { JSONObject json2 = CollectComments.collectComments(usernameforcomments, offsetNumber); return json2; } @Override protected void onPostExecute(JSONObject json2) { try { if (json2.getString(KEY_SUCCESS) != null) { registerErrorMsg.setText(""); String res2 = json2.getString(KEY_SUCCESS); if(Integer.parseInt(res2) == 1){ JSONArray commentArray = json2.getJSONArray(KEY_COMMENT); String comments[] = new String[commentArray.length()]; for ( int i=0; i<commentArray.length(); i++ ) { comments[i] = commentArray.getString(i); } JSONArray numberArray = json2.getJSONArray(KEY_NUMBER); String numbers[] = new String[numberArray.length()]; for ( int i=0; i<numberArray.length(); i++ ) { numbers[i] = numberArray.getString(i); } JSONArray usernameArray = json2.getJSONArray(KEY_USERNAME); String usernames[] = new String[usernameArray.length()]; for ( int i=0; i<usernameArray.length(); i++ ) { usernames[i] = usernameArray.getString(i); } mycmlist.add(comments,usernames,numbers); mycmlist.notifyDataSetChanged(); }//end if key is == 1 else{ // Error in registration registerErrorMsg.setText(json2.getString(KEY_ERROR_MSG)); }//end else }//end if } //end try catch (JSONException e) { e.printStackTrace(); }//end catch progDailog.dismiss(); } } mainListView = (ListView) findViewById(android.R.id.list); class EndlessScrollListener implements OnScrollListener { private int i = 0; private int visibleThreshold = 5; private int previousTotal = 0; private boolean loading = true; public EndlessScrollListener() { } public EndlessScrollListener(int visibleThreshold) { this.visibleThreshold = visibleThreshold; } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if ((firstVisibleItem + visibleItemCount) == totalItemCount) { new loadComments().execute(); mainListView.smoothScrollToPosition(0); } } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } } mainListView.setOnScrollListener(new EndlessScrollListener()); ```

Original source