gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:07:24 25 4
gpt4 key购买 nike

我正在为应用程序创建评论部分,每次单击“评论”按钮时,我都想将评论添加到我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com