android - 列表适配器仅将 1 个项目添加到 ListView

标签 android listview scrollview

我正在为应用程序创建评论部分,每次单击“评论”按钮时,我都想将评论添加到我的 ListView。

不幸的是,我的代码只允许我向我的 ListView 添加 1 条评论。每次我键入并单击添加评论按钮时,ListView 都保持静态在 1 项。

我想我必须修改我的客户适配器中的 getCount() 方法,但我想寻求帮助

下面是我的代码:

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

最佳答案

在增加计数之前调用 notifyDataSetChanged()。在notifyDataSetChanged()之前增加计数器应该生效

mCommentCounter++;
mCommentAdapter.notifyDataSetChanged();

你也可以使用适配器的add方法,这样你就不需要重写getCount而有一个计数器,也不需要add with index。适配器添加方法已经添加了项目并调用了 notifyDataSetChanged

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

还有如下答案所述的 ScrollView / ListView 问题

关于android - 列表适配器仅将 1 个项目添加到 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35734435/

相关文章:

android - 在 Windows 中为 android 编译 protobuf 2.6.1

android - 自定义 AlertDialog ScrollView,在 2.3 中太大,缺少确定按钮

android - 添加安卓 :textIsSelectable causes TextView to autoscroll

jQuery Mobile ScrollView 行为渗透到其他页面/div

java - 等待webview中的javascript完成以继续Android Java

java - 错误 :Execution failed for task ':app:transformClassesWithFirebasePerformancePluginForRelease'

android - Imageview 跳转到起始位置

c# - 通过 ListView 进行 foreach 并访问子项?

wpf - 如何使用 TwoWay 模式将 Listview SelectedItem 绑定(bind)到文本框?

屏幕旋转后Android listview消失