tccpg288
tccpg288

Reputation: 3352

List Adapter Only Adding 1 Item to List View

I am creating a comments section for an application, and every time I click the "comment" button, I want to add the comment to my ListView.

Unfortunately, my code is only allowing me to add 1 comment to my ListView. Every time I type and click the add comment button, the ListView remains static at 1 item.

I believe I have to modify the getCount() method in my Customer Adapter, but I wanted to seek assistance

Below is my code:

public class Discussion_Activity extends AppCompatActivity {

private EditText mUserComment;
private String mUserID;
private ImageView mUserAvatar;
private ListView mPollComments;
private ArrayAdapter<Comments> mCommentAdapter;
private Firebase mBaseRef;
private int mCommentCounter;

private static final String FIREBASE_URL = "https://fan-polls.firebaseio.com/";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_discussion);
    Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);

    mBaseRef = new Firebase(FIREBASE_URL);

    mUserComment = (EditText) findViewById(R.id.user_comment);
    mUserAvatar = (ImageView) findViewById(R.id.profile_image_avatar);
    mPollComments = (ListView) findViewById(R.id.poll_comments_list);
    final ArrayList<Comments> pollComments = new ArrayList<Comments>();
    mCommentAdapter = new ListAdapter(getApplicationContext(),R.layout.individual_comment, pollComments);
    mPollComments.setAdapter(mCommentAdapter);
    mCommentCounter = 0;
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.add_comment_button);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            pollComments.add(mCommentCounter, new Comments(mUserAvatar, mBaseRef.getAuth().getUid(), mUserComment.getText().toString() ));
            mCommentAdapter.notifyDataSetChanged();
            mCommentCounter++;
            hideKeyboard(view);
            mUserComment.setText("");
        }
    });
}

public void hideKeyboard(View view) {
    InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

public class ListAdapter extends ArrayAdapter<Comments> {

    public ListAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public ListAdapter(Context context, int resource, List<Comments> items) {
        super(context, resource, items);
    }

    @Override
    public int getCount() {
        return mCommentCounter;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.individual_comment, null);
        }

        Comments p = getItem(position);

        if (p != null) {
            TextView userID = (TextView) v.findViewById(R.id.user_ID);
            TextView userComment = (TextView) v.findViewById(R.id.user_comment);

            if (userID != null) {
                userID.setText(p.getUserID());
            }

            if (userComment != null) {
                userComment.setText(p.getUserComment());
            }
        }


        return v;
    }

  }

}

XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/toolbar" />

    <ImageView
        android:id="@+id/poll_image"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".475"
        android:background="@drawable/heyward"
        android:scaleType="fitXY" />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".2"
        android:orientation="horizontal">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_margin="10dp"
            android:background="@drawable/textlines">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/profile_image_avatar"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="8dp"
                android:src="@drawable/empty_avatar_256x256"
                app:civ_border_color="#FF000000"
                app:civ_border_width="2dp" />

            <EditText
                android:id="@+id/user_comment"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_gravity="bottom"
                android:layout_marginEnd="60dp"
                android:layout_marginLeft="65dp"
                android:layout_marginRight="60dp"
                android:layout_marginStart="65dp"
                android:hint="Enter a comment....."
                android:inputType="textCapSentences|textMultiLine"
                android:scrollHorizontally="false"
                android:textSize="16sp" />

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/add_comment_button"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_gravity="end|center_vertical"
                android:layout_marginEnd="4dp"
                android:layout_marginRight="4dp"
                android:clickable="true" />


        </FrameLayout>

    </LinearLayout>

    <ScrollView
        android:layout_marginStart="30dp"
        android:layout_marginLeft="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.40">

        <ListView
            android:id="@+id/poll_comments_list"
            android:divider="@drawable/list_divide"
            android:dividerHeight="1px"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


        </ListView>


    </ScrollView>

</LinearLayout>

enter image description here

Upvotes: 2

Views: 1004

Answers (3)

andrea.petreri
andrea.petreri

Reputation: 4147

Problem is that you are adding items directly into ListView. You should manage a global list of Comments and share it with your ListAdapter.

Please notice that in this way you don't need mCommentCounter variable at all.

Then, add new comments to this list and notify your adapter. Below I provided you updated code. Let me know if it works for you.

public class Discussion_Activity extends AppCompatActivity {

    private EditText mUserComment;
    private String mUserID;
    private ImageView mUserAvatar;
    private ListView mPollComments;
    private List<Comments> mComments;
    private ArrayAdapter<Comments> mCommentAdapter;
    private Firebase mBaseRef;

    private static final String FIREBASE_URL = "https://fan-polls.firebaseio.com/";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discussion);
        Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

        private List<Comments> mComments = new ArrayList<Comments>;
        mBaseRef = new Firebase(FIREBASE_URL);

        mUserComment = (EditText) findViewById(R.id.user_comment);
        mUserAvatar = (ImageView) findViewById(R.id.profile_image_avatar);
        mPollComments = (ListView) findViewById(R.id.poll_comments_list);
        mCommentAdapter = new ListAdapter(getApplicationContext(),R.layout.individual_comment, mComments);
        mPollComments.setAdapter(mCommentAdapter);
        mCommentCounter = 0;
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.add_comment_button);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mComments.add(new Comments(mUserAvatar, mBaseRef.getAuth().getUid(), mUserComment.getText().toString()));
                mCommentAdapter.notifyDataSetChanged();
                hideKeyboard(view);
                mUserComment.setText("");
            }
        });
    }

    public void hideKeyboard(View view) {
        InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    public class ListAdapter extends ArrayAdapter<Comments> {

        public ListAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

        public ListAdapter(Context context, int resource, List<Comments> items) {
            super(context, resource, items);
        }

        @Override
        public int getCount() {
            return mComments.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View v = convertView;

            if (v == null) {
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                v = vi.inflate(R.layout.individual_comment, null);
            }

            Comments p = getItem(position);

            if (p != null) {
                TextView userID = (TextView) v.findViewById(R.id.user_ID);
                TextView userComment = (TextView) v.findViewById(R.id.user_comment);

                if (userID != null) {
                    userID.setText(p.getUserID());
                }

                if (userComment != null) {
                    userComment.setText(p.getUserComment());
                }
            }

            return v;
        }

    }

}

I noticed now comment about ScrollView / ListView. This is also a problem. You can keep ListView only.

Upvotes: 1

Nathua
Nathua

Reputation: 8826

You call notifyDataSetChanged() before increasing the count. Increase the counter before notifyDataSetChanged() and it should take the effect

mCommentCounter++;
mCommentAdapter.notifyDataSetChanged();

You can also use the adapter add method, thus you don't need to override getCount and have a counter, and also no need to add with index. Adapter add method already adds the item and invokes notifyDataSetChanged

adapter.add(new Comments(....));

And also scrollview/listview issue as mentioned below answer

Upvotes: 4

mohax
mohax

Reputation: 4585

It's a bad practise to add one scrollable element to another. You can't make it works without dirty hacks.

So delete yours ScrollView and yours ListView will work as expected

Upvotes: 1

Related Questions