我喜欢布局中的按钮并设置文字"如0" ,我已经设置在基本适配器中,当我点击Like Buttton时,文本将增加而不刷新整个活动就像在Facebook应用程序中,我被困在三天,我已阅读并实现几乎每个stackoverflow的答案,但没有得到解决方案。我希望我会得到答案。这是My Base Adapter< ------->
public class FeedAdaptorView extends BaseAdapter{
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();
String UserID;
public FeedAdaptorView(Context context,
ArrayList<HashMap<String, String>> arraylist, String uSERid2) {
this.context = context;
data = arraylist;
}
public class ViewHolder {
TextView name,time,status,likecount;
ImageView userimage,postimage;
Button like,comment,share;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, final ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.feed_item, null);
holder=new ViewHolder();
pos=getItemViewType(position);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.status=(TextView)convertView.findViewById(R.id.txtStatusMsg);
holder.userimage=(ImageView)convertView.findViewById(R.id.profilePic);
holder.postimage=(ImageView)convertView.findViewById(R.id.feedImage1);
holder.like=(Button)convertView.findViewById(R.id.like);
holder.comment=(Button)convertView.findViewById(R.id.comment);
holder.pos=position;
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.like.setTag(pos);
/* inflater = (LayoutInflater) context
holder.comment.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0);
}
}); .getSystemService(Context.LAYOUT_INFLATER_SERVICE);*/
holder.comment.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0);
}
});
holder.postimage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0);
}
});
holder.like.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "You Liked The Post", Toast.LENGTH_SHORT).show();
holder.like.setText("Like"+"4");
notifyDataSetChanged();
// here i am calling the web services for like button update
new likepost().execute();
}
});
&LT; ---------------------------------&GT;
webservice callin method
class likepost extends AsyncTask<Void, Void, Void>
{
int flag=0;
@Override
protected void onPreExecute()
{
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(context);
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setCancelable(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... args0) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
String Status="1";
String ID=feedid;
params.add(new BasicNameValuePair("UserID",UserID));
params.add(new BasicNameValuePair("FeedId",ID));
params.add(new BasicNameValuePair("like_status",Status));
JSONObject json = jsonParser.makeHttpRequest(AppControllerClass.FeedLike,"POST", params);
Log.d("Create Response", json.toString());
try{
if(json!=null)
{
String status=json.getString("status");
if(status.equals("Success"))
{
flag=1;
// Toast.makeText(context, "You Liked The Post", Toast.LENGTH_SHORT).show();
}
else
{
flag=2;
//Toast.makeText(context, "Check The Connection", Toast.LENGTH_SHORT).show();
}
}
else{
flag=2;
}
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args)
{
mProgressDialog.dismiss();
if(flag==1)
{
Toast.makeText(context, "You Liked The Post", Toast.LENGTH_SHORT).show();
}
else if (flag==2)
{
Toast.makeText(context, "Check The Connection", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
动态增加BaseAdapter中单击的文本计数
使用getTag/setTag
执行此操作:
@Override
public void onClick(View c) {
int count=0;
if(v.getTag()==null)
count=0;
else{
count=Integer.parseInt(v.getTag().toString());
}
count++;
((TextView)v).setText("Like"+count);
v.setTag(count);
}
答案 1 :(得分:0)
试试这个..
@Override
public Object getItem(int position) {
return data.get(position);
}
而不是
@Override
public Object getItem(int position) {
return null;
}
答案 2 :(得分:0)
这是我第一次回答有关StackOverflow的问题,我希望我的回答能够帮助您解决问题。
问题:
holder.like.setText("Like"+"4");
notifyDataSetChanged();
问题出在这里。您已手动将其设置为setText(“Like”+“4”)。在BaseAdapter的notifyDataSetChanged()函数中,它引用了一个信息的适配器。
在您的情况下,它将是resulttp的参考。它引用了您传入的数据ArrayList。当您调用notifyDataSetChanged()时,它将返回resulttp并显示在ArrayList中的WHAT。
解决方案:
1)删除此行
holder.like.setText("Like"+"4");
2)在BaseAdapter类中添加一个名为update()的新函数
public void updateAdapter(ArrayList<HashMap<String, String>> newData)
{
this.data.clear();
this.data = data;
notifyDataSetChanged();
}
3)在你喜欢的按钮/图像onClickListener上,调用函数updateAdapter()并传入一个UPDATED ArrayList。
它应该没问题。祝你好运!