更新卡片视图列表中的文本? [英] Update text within card view list?

查看:74
本文介绍了更新卡片视图列表中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有卡片视图的回收视图列表.我试图在用户单击卡片时更新每个视图中的文本时间文本,然后在时间过去后将其重置.我在recyclerviewadapter中设置了一个内部方法,但是当我调用updateTimes(从片段调用)时,它不会将文本重置回去.有人可以告诉我我做错了什么吗?

So I have a recyclerview list of card views.I am trying to update the text time text in each view when the user clicks the card, and then reset it back when time has past. I set up an internal method in recyclerviewadapter but when I call updateTimes (calling from fragment), it does not reset the text back. Can someone instruct me on what I'm doing wrong?

import java.util.ArrayList;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.net.Uri;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
 import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import java.util.Calendar;
import android.content.Intent;
import java.text.SimpleDateFormat;
import java.util.Locale;


public class RecyclerAdapter extends RecyclerView.Adapter<FaucetHolder> {
    private ArrayList<Faucet> faucets;
    private Context context;
    private SharedPreferences sharedPref;
    private String dateFormat = "h:mm a";
    public    RecyclerAdapter(ArrayList<Faucet> faucetsI, Context context) {
    this.faucets = faucetsI;
    this.context = context;


}

@Override
public FaucetHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_view, viewGroup, false);
    FaucetHolder f = new FaucetHolder(v);
    return f;
}

@Override
public void onBindViewHolder(FaucetHolder f, int k) {
    final Faucet faucet = faucets.get(k);
    f.titleText.setOnClickListener(new View.OnClickListener() {
            public void onClick(View btn) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(faucet.getLink()));
                setRenewal(faucet);
                notifyDataSetChanged();
                context.startActivity(intent);
            }
        });
    f.titleText.setText(faucet.getName());
    sharedPref = context.getSharedPreferences("myPref", 0);
    String time = sharedPref.getString(faucet.getSPName(), "could not retrieve time");
    Log.e("tree", time);
    f.timeText.setText(time);

}

@Override
public int getItemCount() {
    return faucets.size();
}



private void setRenewal(Faucet g) {
    sharedPref = context.getSharedPreferences("myPref", 0);
    SharedPreferences.Editor edit = sharedPref.edit();
    Calendar c = Calendar.getInstance();
    long current = c.getTimeInMillis();
    String x = sharedPref.getString(g.getSPName(), null);

    long future = current + g.getLength();
    g.setCT(future);
    c.setTimeInMillis(future);
    SimpleDateFormat df = new SimpleDateFormat(dateFormat, Locale.US);
    String date = df.format(c.getTime());

    edit.putString(g.getSPName(), date).commit();

}


public void updateTimes() {
    sharedPref = context.getSharedPreferences("myPref", 0);
    SharedPreferences.Editor edit = sharedPref.edit();
    for ( Faucet f : faucets ) {
        boolean m = checkifPast(f);
        if ( m == true ) {
            edit.putString(f.getSPName(), "Ready!").commit();
        }
    }
    notifyDataSetChanged();
}

private boolean checkifPast(Faucet f) {
    Calendar c = Calendar.getInstance();
    long comp = c.getTimeInMillis();
    if ( comp > f.getCT() ) {
        return false;
    }
    return true;
}

}

片段代码

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{

    v = inflater.inflate(R.layout.faucetcards, container, false);
    r = (RecyclerView) v.findViewById(R.id.RecyclerView);
    rA = new RecyclerAdapter(generateCards(), getActivity());
    lm = new LinearLayoutManager(getActivity());
    r.setAdapter(rA);
    r.setLayoutManager(lm);

    sharedPref = getActivity().getPreferences(0);
    return v;
}


@Override
public void onResume()
{
    rA.updateTimes();
    super.onResume();
}

推荐答案

notifyDataSetChanged应该用作万不得已的方法,因为它没有指定发生了什么,因此适配器假定不再有效,这将导致该列表不仅可以重新绑定,还可以中继所有可见视图.相反,您应该使用notifyItem[Range][Inserted|Changed|Removed]()方法之一,在您的情况下

notifyDataSetChanged should be used as a last resort, since it does not specify what has happened and therefore the adapter asumes nothing is valid anymore, which causes the list to not only rebind, but also relayout all visible views. You should instead use one of the notifyItem[Range][Inserted|Changed|Removed]() methods instead, in your case notifyItemChanged(int position) will do.

updateText()中,您可以编辑文本,但不要将结果存储在任何字段中.然后,随后将调用bindViewHolder(),该调用将调用setText(getTime())并将文本重置为其先前的值.这可能就是为什么什么都没有改变的原因.您应该在ViewHolder中添加一个方法,该方法首先存储结果,然后调用TextView.setText().

In updateText() you edit the text, but don't store the result in any field. Then later, bindViewHolder() will be called, which calls setText(getTime()) and resets the text to its previous value. That is probably why nothing changes. You should add a method to your ViewHolder which first stores the result and then calls TextView.setText().

如果从未调用过onClick(),则可能要检查是否已在其布局文件中将视图设置为可聚焦和可单击.

If your onClick() is never called, you might want to check that you have set the view focusable and clickable in its layout-file.

这是我在StackOverflow上的第一个发言人,英语不是我的母语,所以请打错我一下.

This is my first awnser on StackOverflow and English is not my first language, so excuse me if I typed something wrong.

这篇关于更新卡片视图列表中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆