如何从RECYCLERVIEW

时间:2019-08-07 09:57:41

标签: java android android-recyclerview recycler-adapter

我正在尝试从RecyclerView中的选定项中获取TextView值,并将该值发送到其他活动。我的RecyclerView布局文件有两个TextView,一个用于名字和姓氏,另一个用于电子邮件。我发现recyclerview不支持LIST VIEW之类的某些功能,如果有人可以为我提供一些建议,这就是我的代码。


import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.Editable;
import android.text.Spannable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.example.lab.Activities.MainActivity;
import com.example.lab.Activities.ModifyPers;
import com.example.lab.Activities.NewPersActivity;
import com.example.lab.Entities.Personne;

import java.util.List;

public class ApiPersListAdapter extends RecyclerView.Adapter<ApiPersListAdapter.ViewHolder> {
    TextView text1;
    Handler handler =new Handler();
String s;

    private Context context;
private List<Personne> list;
String nom;

public ApiPersListAdapter(Context context, List<Personne> list) {
        this.context = context;
        this.list = list;
        }

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.recyclerview_api_list_item, parent, false);
        return new ViewHolder(v);
        }

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
        Personne p = list.get(position);
      holder.nom_pre.setText(p.getPrenom()+" "+p.getNom());

    holder.email.setText(p.getEmail());



        }


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

public   class ViewHolder extends RecyclerView.ViewHolder {
    public TextView nom_pre,email;


    public ViewHolder(View itemView) {
        super(itemView);
        nom_pre = itemView.findViewById(R.id.nom_pren);
        email = itemView.findViewById(R.id.email);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(itemView.getContext(), ModifyPers.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               Log.v("nom",nom);
               intent.putExtra("key_prenom",nom);

                itemView.getContext().startActivity(intent);
            }
        });


    }

}

}

3 个答案:

答案 0 :(得分:1)

在您的itemView.setOnClickListener中  在ViewHolder类的intent.putExtra("key_prenom",nom);行中,将变量nom替换为:

 list.get(getAdapterPosition()).getNom();

答案 1 :(得分:0)

我对您的课堂进行了更改。请看一看。在活动中实现监听器

scala> val myCols = Seq("myColA", "myColB", "myColC")
myCols: Seq[String] = List(myColA, myColB, myColC)

scala> val requiredDf = tempDf.select((0 until lengthOfRow).map(i => col("value")(i).alias( myCols(i) )): _*)
requiredDf: org.apache.spark.sql.DataFrame = [myColA: string, myColB: string ... 1 more field]

答案 2 :(得分:0)

您可以在onCreateViewHolder上尝试

Range

要获取Time回收视图的文本视图或为其设置值,您需要先找到该视图,以便可以通过定义的视图访问它们。

LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
View v = inflater.inflate(R.layout.recyclerview_api_list_item, null);

然后返回视图

R.layout.recyclerview_api_list_item