问题的标题不能完全解释我的问题,因此请允许我详细说明。我有一个将Google Civic API中的JSON数据解析为recyclerview的应用程序。一个特定的字段是TextView,它以字符串形式存储PDF网址。如何设置TextView,使其可单击并最好在应用程序中打开PDF。我查找了解决方案,并尝试使用WebView和PDFReader等解决方案,但是我找不到从recyclerview提及如何执行此解决方案的任何解决方案,因此我不确定将代码放置在何处。由于我单击了recyclerView中的某个项目,因此我认为可能需要在recyclerview适配器中而不是在主类中添加代码。
这是主类的代码:此类处理JSON解析
public class VoterInformation extends AppCompatActivity {
private static final String url = "https://www.googleapis.com/civicinfo/v2/voterinfo?address=2613+Irvington+Ave+San+Bernardino+CA+92407&electionId=2000&officialOnly=false&returnAllAvailableData=false&fields=contests(referendumBallotResponses%2CreferendumBrief%2CreferendumConStatement%2CreferendumEffectOfAbstain%2CreferendumPassageThreshold%2CreferendumProStatement%2CreferendumSubtitle%2CreferendumText%2CreferendumTitle%2CreferendumUrl)&key=AIzaSyB1B5mEKHK8PHDiNcGQ5ZU3fPIH9KWxAcQ";
private TextView refTitle, refSub, refUrl;
private LinearLayoutManager linearLayoutManager;
private List<Refs> refList;
private RecyclerView myrv;
private RecyclerView.Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voter_information);
refTitle = findViewById(R.id.ref_title); //Initialize TextViews
refSub = findViewById(R.id.ref_sub);
myrv = findViewById(R.id.ref_rv); //Initialize recycler view
refList = new ArrayList<>();
adapter = new RefRvAdapter(refList, getApplicationContext());
linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
myrv.setHasFixedSize(true);
myrv.setLayoutManager(linearLayoutManager);
myrv.setAdapter(adapter);
TextView pdf_url = findViewById(R.id.ref_url);
pdf_url.setClickable(true);
pdf_url.setMovementMethod(LinkMovementMethod.getInstance());
getData();
}
private void getData(){
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try{
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("contests");
String pdf_url = array.getJSONObject(0).getString("referendumUrl") ;
for(int i = 0; i < array.length(); i++){
JSONObject jo = array.getJSONObject(i);
Refs refs = new Refs(jo.getString("referendumTitle"),
jo.getString("referendumSubtitle"),
jo.getString("referendumUrl"));
refList.add(refs);
}
adapter = new RefRvAdapter(refList, getApplicationContext());
myrv.setAdapter(adapter);
} catch(JSONException e){
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
Log.e("Volley", error.toString());
progressDialog.dismiss();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
这是我的适配器类代码:
public class RefRvAdapter extends RecyclerView.Adapter<RefRvAdapter.MyViewHolder>{
private Context context;
private List<Refs> refsList;
public RefRvAdapter(){}
public RefRvAdapter(List<Refs> refList, Context context){
this.refsList = refList;
this.context = context;
}
@NonNull
@Override
public RefRvAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view;
LayoutInflater mInflater = LayoutInflater.from(context);
view = mInflater.inflate(R.layout.ref_card, viewGroup, false);
final MyViewHolder viewHolder = new MyViewHolder(view);
viewHolder.view_containers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent (context, SingleReferendum.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("referendumTitle", refsList.get(viewHolder.getAdapterPosition()).getRefTitle());
i.putExtra("referendumSubtitle", refsList.get(viewHolder.getAdapterPosition()).getRefSub());
i.putExtra("referendumUrl", refsList.get(viewHolder.getAdapterPosition()).getRefUrl());
context.startActivity(i);
}
});
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RefRvAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.refTitle.setText(refsList.get(i).getRefTitle());
myViewHolder.refSub.setText(refsList.get(i).getRefSub());
myViewHolder.refUrl.setText(refsList.get(i).getRefUrl());
}
@Override
public int getItemCount() {
return refsList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView refTitle, refSub, refUrl;
LinearLayout view_containers;
public MyViewHolder(View itemView){
super(itemView);
refTitle = itemView.findViewById(R.id.ref_title);
refSub = itemView.findViewById(R.id.ref_sub);
refUrl = itemView.findViewById(R.id.ref_url);
view_containers = itemView.findViewById(R.id.view_container_ref);
}
}
}
该应用程序可以很好地解析所有数据。我只想使网址可点击。当用户单击它时,将打开pdf。是否在RecyclerView适配器类或主要活动中添加代码?
答案:像这样编辑onBindViewHolder方法。
@Override
public void onBindViewHolder(@NonNull final RefRvAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.refTitle.setText(refsList.get(i).getRefTitle());
myViewHolder.refSub.setText(refsList.get(i).getRefSub());
myViewHolder.refUrl.setText(refsList.get(i).getRefUrl());
myViewHolder.view_containers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent (context, SingleReferendum.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("referendumTitle", refsList.get(myViewHolder.getAdapterPosition()).getRefTitle());
i.putExtra("referendumSubtitle", refsList.get(myViewHolder.getAdapterPosition()).getRefSub());
i.putExtra("referendumUrl", refsList.get(myViewHolder.getAdapterPosition()).getRefUrl());
context.startActivity(i);
}
});
myViewHolder.refUrl.setOnClickListener(new View.OnClickListener(){
@Override //handle pdf clicks
public void onClick(View view) {
String refUrl = refsList.get(myViewHolder.getAdapterPosition()).getRefUrl();
Intent intent = new Intent (Intent.ACTION_VIEW, Uri.parse(refUrl));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}
答案 0 :(得分:1)
如果我没有误解您的意思,只需将方法onCreateViewHolder
和onBindViewHolder
修改为:
@NonNull
@Override
public RefRvAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view;
LayoutInflater mInflater = LayoutInflater.from(context);
view = mInflater.inflate(R.layout.ref_card, viewGroup, false);
final MyViewHolder viewHolder = new MyViewHolder(view);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RefRvAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.refTitle.setText(refsList.get(i).getRefTitle());
myViewHolder.refSub.setText(refsList.get(i).getRefSub());
myViewHolder.refUrl.setText(refsList.get(i).getRefUrl());
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent (context, SingleReferendum.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("referendumTitle", refsList.get(myViewHolder.getAdapterPosition()).getRefTitle());
i.putExtra("referendumSubtitle", refsList.get(myViewHolder.getAdapterPosition()).getRefSub());
i.putExtra("referendumUrl", refsList.get(myViewHolder.getAdapterPosition()).getRefUrl());
context.startActivity(i);
}
}
myViewHolder.refUrl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String refUrl = refsList.get(myViewHolder.getAdapterPosition()).getRefUrl();
//open pdf here.
}
});
}
尝试一下,如果可以,请反馈。