我有一个列表视图,其中包含我可以喜欢和评论的项目,喜欢计数增加后说1,但如果我向下滚动并回到帖子我喜欢类似的计数器重置为0就像加载的那样来自服务器。
其次,我实现了更多加载按钮,但是只要加载更多按钮,它就会返回到列表视图的开头而不是继续按下加载更多按钮的位置。问题发生在片段部分我使用Activity实现相同的加载更多逻辑,它工作正常。
public class ListViewAdapter_Post extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter_Post(Context context,ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
static class ViewHolder {
// Declare Variables
TextView usernameTV;
TextView timeTV;
TextView postTV;
TextView schyrTV;
ImageView postImg;
ImageView profilePicImg;
ImageButton commentBtn;
ImageButton reportBtn;
ImageButton likeBtn;
TextView likesCount;
TextView likestxt;
TextView commentCount;
}
ViewHolder holder;
String username;
String reg_id;
String myyear;
public View getView(final int position, View itemView, ViewGroup parent) {
itemView = null;
if (itemView == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.list_item_home, parent, false);
// Get the position
resultp = data.get(position);
holder = new ViewHolder();
// Locate the TextViews in listview_item.xml
holder.usernameTV = (TextView) itemView.findViewById(R.id.home_item_username);
holder.timeTV = (TextView) itemView.findViewById(R.id.home_item_post_time);
holder.postTV = (TextView) itemView.findViewById(R.id.home_item_post_text);
holder.schyrTV = (TextView) itemView.findViewById(R.id.home_item_schoolyear);
// Locate the ImageView in listview_item.xml
holder.profilePicImg = (ImageView) itemView.findViewById(R.id.home_item_profile_picture);
holder.postImg = (ImageView) itemView.findViewById(R.id.home_item_post_image);
// Locate the ImageView in listview_item.xml
holder.commentBtn = (ImageButton) itemView.findViewById(R.id.home_item_comment_btn);
holder.reportBtn= (ImageButton) itemView.findViewById(R.id.home_item_report_btn);
holder.likesCount = (TextView) itemView.findViewById(R.id.likesCount);
holder.likestxt = (TextView) itemView.findViewById(R.id.likestxt);
holder.commentCount = (TextView) itemView.findViewById(R.id.commentCount);
holder.likeBtn= (ImageButton) itemView.findViewById(R.id.home_item_like_btn);
}
else{
holder = (ViewHolder) itemView.getTag();
}
holder.postImg.setVisibility(View.VISIBLE);
reg_id = resultp.get(Fragment_home.POST_REG_ID);
String empty_image = resultp.get(Fragment_home.POST_PHOTO).toString();
if(empty_image.equalsIgnoreCase("http://www.dentymz.com/appx/dtbcknd/posts/")){
holder.postImg.setVisibility(View.GONE);
// Toast.makeText(context,"Post contains no pic",Toast.LENGTH_SHORT).show();
}
else{
holder.postImg.setVisibility(View.VISIBLE);
// Toast.makeText(context,"Post contains no pic",Toast.LENGTH_SHORT).show();
Picasso.with(context).load(resultp.get(Fragment_post.POST_PHOTO)).placeholder(R.drawable.default_back_ash).fit().centerCrop().error(R.drawable.default_back_ash)
.into(holder.postImg);
}
Picasso.with(context).load(resultp.get(Fragment_post.POST_PROFILE_PIC)).placeholder(R.drawable.default_back_ash).fit().centerCrop().error(R.drawable.default_back_ash)
.into(holder.profilePicImg);
// Capture position and set results to the TextViews
holder.usernameTV.setText(resultp.get(Fragment_post.POST_USERNAME));
holder.timeTV.setText(resultp.get(Fragment_post.POST_TIME));
// holder.postTV.setText(resultp.get(Fragment_post.POST_TEXT));
holder.postTV.setText(AndroidEmoji.ensure(resultp.get(Fragment_post.POST_TEXT), context));
holder.schyrTV.setText(schyr);
holder.likesCount.setText(resultp.get(Fragment_post.POST_LIKES_COUNT));
holder.commentCount.setText(resultp.get(Fragment_post.POST_COMMENTS_COUNT)+" Comments");
DatabaseHandler db = DatabaseHandler.getDatabaseHandler(context);
UserFunctions userFunctions = new UserFunctions();
userFunctions.isUserLoggedIn(context);
// user already logged in show dash board
username = db.getUsername();
if(resultp.get(Fragment_home.POST_LIKED_BY).toString().equals(username)){
holder.likeBtn.setImageResource(R.drawable.ic_favorite_24dp1);
holder.likeBtn.setTag("liked");
}
else if(!resultp.get(Fragment_home.POST_LIKED_BY).toString().equals(username)){
holder.likeBtn.setImageResource(R.drawable.ic_favorite_24dp);
holder.likeBtn.setTag("unliked");
}
else {
Toast.makeText(context,"Error Occured",Toast.LENGTH_SHORT).show();
}
final Intent user_profile = new Intent(context, MainProfile.class);
final Intent main_profile = new Intent(context, MyProfile.class);
holder.likeBtn.setTag(holder);
holder.likeBtn.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
holder = (ViewHolder) view.getTag();
resultp = data.get(position);
if (holder.likeBtn.getDrawable().getConstantState().equals(context.getResources().getDrawable(R.drawable.ic_favorite_24dp).getConstantState())) {
resultp = data.get(position);
ConnectionDetector cd = new ConnectionDetector(context);
if (!cd.isConnectingToInternet()) {
Toast.makeText(context, "No internet connection", Toast.LENGTH_SHORT).show();
} else {
String number = holder.likesCount.getText().toString();
int num = Integer.parseInt(number);
int nene = num + 1;
String str = String.valueOf(nene);
holder.likesCount.setText(str);
holder.likeBtn.setImageResource(R.drawable.ic_favorite_24dp1);
}
} else if (holder.likeBtn.getDrawable().getConstantState().equals(context.getResources().getDrawable(R.drawable.ic_favorite_24dp1).getConstantState())) {
resultp = data.get(position);
ConnectionDetector cd = new ConnectionDetector(context);
if (!cd.isConnectingToInternet()) {
Toast.makeText(context, "No internet connection", Toast.LENGTH_SHORT).show();
} else {
String number = holder.likesCount.getText().toString();
int num = Integer.parseInt(number);
int nene = num - 1;
String str = String.valueOf(nene);
holder.likesCount.setText(str);
holder.likeBtn.setImageResource(R.drawable.ic_favorite_24dp);
}
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
holder = (ViewHolder) view.getTag();
resultp = data.get(position);
if (holder.likeBtn.getDrawable().getConstantState().equals(holder.likeBtn.getContext().getDrawable(R.drawable.ic_favorite_24dp).getConstantState())) {
resultp = data.get(position);
ConnectionDetector cd = new ConnectionDetector(context);
if (!cd.isConnectingToInternet()) {
Toast.makeText(context, "No internet connection", Toast.LENGTH_SHORT).show();
} else {
String number = holder.likesCount.getText().toString();
int num = Integer.parseInt(number);
int nene = num + 1;
String str = String.valueOf(nene);
holder.likesCount.setText(str);
holder.likeBtn.setImageResource(R.drawable.ic_favorite_24dp1);
}
} else if (holder.likeBtn.getDrawable().getConstantState().equals(holder.likeBtn.getContext().getDrawable(R.drawable.ic_favorite_24dp1).getConstantState())) {
resultp = data.get(position);
ConnectionDetector cd = new ConnectionDetector(context);
if (!cd.isConnectingToInternet()) {
Toast.makeText(context, "No internet connection", Toast.LENGTH_SHORT).show();
} else {
String number = holder.likesCount.getText().toString();
int num = Integer.parseInt(number);
int nene = num - 1;
String str = String.valueOf(nene);
holder.likesCount.setText(str);
holder.likeBtn.setImageResource(R.drawable.ic_favorite_24dp);
}
}
}
}
});
final Intent post_comment = new Intent(context, Comments.class);
holder.commentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultp = data.get(position);
post_comment.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
post_comment.putExtra("id", resultp.get(Fragment_post.POST_ID));
post_comment.putExtra("username",resultp.get(Fragment_post.POST_USERNAME));
post_comment.putExtra("reg_id",resultp.get(Fragment_post.POST_REG_ID));
post_comment.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(post_comment);
}
});
holder.commentCount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultp = data.get(position);
post_comment.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
post_comment.putExtra("id", resultp.get(Fragment_post.POST_ID));
post_comment.putExtra("username", resultp.get(Fragment_post.POST_USERNAME));
post_comment.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(post_comment);
}
});
final Intent likes = new Intent(context, Likes_post.class);
holder.likesCount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultp = data.get(position);
likes.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
likes.putExtra("id",resultp.get(Fragment_post.POST_ID));
likes.putExtra("username", resultp.get(Fragment_post.POST_USERNAME));
post_comment.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(likes);
}
});
holder.likestxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultp = data.get(position);
likes.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
likes.putExtra("id",resultp.get(Fragment_post.POST_ID));
likes.putExtra("username", resultp.get(Fragment_post.POST_USERNAME));
post_comment.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(likes);
}
});
return itemView;
}
}
答案 0 :(得分:0)
加载更多问题,将位置保存到变量中然后使用listview.setselection(position)转到加载更多按钮之前的位置
即:
int lastPosition = data.size();
loadMore();
listView.setSelection(lastPosition);