我正在为应用程序创建一个评论部分,每次单击“评论”按钮,我都想将评论添加到我的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>
答案 0 :(得分:3)
在增加计数之前,请致电notifyDataSetChanged()
。在notifyDataSetChanged()
之前增加计数器,它应该起作用
mCommentCounter++;
mCommentAdapter.notifyDataSetChanged();
您也可以使用适配器add
方法,因此您不需要覆盖getCount并拥有计数器,也不需要添加索引。适配器添加方法已添加该项并调用notifyDataSetChanged
adapter.add(new Comments(....));
还有如下所述的scrollview / listview问题回答
答案 1 :(得分:1)
将一个可滚动元素添加到另一个可执行元素是一种不好的做法。如果没有肮脏的黑客攻击,你就无法行动。
因此,请删除您的ScrollView
,您的ListView
将按预期工作
答案 2 :(得分:1)
问题是您要将项目直接添加到ListView
。您应该管理Comments
的全局列表,并与ListAdapter
共享。
请注意,通过这种方式,您根本不需要mCommentCounter
变量。
然后,在此列表中添加新注释并通知您的适配器。下面我提供了更新的代码。让我知道它是否适合你。
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;
}
}
}
我现在注意到关于ScrollView
/ ListView
的评论。这也是一个问题。您只能保留ListView
。