【问题标题】:How to call different api's for different Recycler View items?如何为不同的 Recyclerview 项目调用不同的 api?
【发布时间】:2019-12-24 19:42:23
【问题描述】:

我有 RecyclerView 使用 Call<list<model>> 改造加载的项目。现在我希望当我单击每个回收站视图项目时应该从不同的 api id 加载数据。例如:

  1. 当我点击第 1 项时:应该加载“http....id=1”

  2. 当我点击第 2 项时:应该加载“http....id=2”

  3. 当我点击第 3 项时:应该加载“http....id=3”..等等

以下是我的加载回收视图项目的适配器(我应该在这里做什么?):

public class WhatsAndroidAdapter extends RecyclerView.Adapter<WhatsAndroidAdapter.CustomViewHolder> {
    List<WhatsAndroid.WhatsAndroidModel> WAmdel;
    Context context;

    public WhatsAndroidAdapter(Context context,List<WhatsAndroid.WhatsAndroidModel> employees) {
        this.WAmdel = employees;
        this.context=context;
    }

    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.whatsandroid_item , parent, false);

        return new CustomViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(CustomViewHolder holder, int position) {

        holder.textView.setText(String.valueOf(position+1)+". ");
        holder.employeeName.setText(WAmdel.get(position).getTitle());
    }

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

    public class CustomViewHolder extends RecyclerView.ViewHolder {
        public TextView employeeName;
        TextView textView;

        public CustomViewHolder(View view) {
            super(view);
            employeeName = (TextView) view.findViewById(R.id.WA2);
            textView=view.findViewById(R.id.WA1);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent =  new Intent(context, NextActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    context.startActivity(intent);

                }
            });

        }
    }}

WhatsAndoid 活动:

public class WhatsAndroid extends AppCompatActivity {
private RecyclerView recyclerView;
private WhatsAndroidAdapter WAAdapter;
ProgressDialog progressDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.whatsandroid_layout);
    progressDialog = new ProgressDialog(WhatsAndroid.this);
    progressDialog.setMessage("Loading....");
    progressDialog.show();
    Toolbar toolbar = (Toolbar) findViewById(R.id. toolbar );
    setSupportActionBar( toolbar );
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
    WhatsAndroidApiService service = WhatsAndroidApiClient.getRetrofitInstance().create(WhatsAndroidApiService.class);
    Call<List<WhatsAndroid.WhatsAndroidModel>> call = service.getWhatsAndroid();

    call.enqueue(new Callback<List<WhatsAndroid.WhatsAndroidModel>>() {
        @Override
        public void onResponse(Call<List<WhatsAndroid.WhatsAndroidModel>> call, Response<List<WhatsAndroid.WhatsAndroidModel>> response) {
           progressDialog.dismiss();
            generateDataList(response.body());

        }

        @Override
        public void onFailure(Call<List<WhatsAndroid.WhatsAndroidModel>> call, Throwable t) {
           progressDialog.dismiss();
            Toast.makeText(getApplicationContext(), "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();


        }
    });
}
private void generateDataList(List<WhatsAndroid.WhatsAndroidModel> employeeList) {
    recyclerView = findViewById(R.id.WArecycle);
    LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(manager);
    recyclerView.setHasFixedSize(true);
    WAAdapter = new WhatsAndroidAdapter(getApplicationContext(),employeeList);
    recyclerView.setAdapter(WAAdapter);
}
public class WhatsAndroidModel {
    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("title")
    @Expose
    private String title;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (item.getItemId() ==android.R.id.home) {
        finish();
    }
    return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
    super.onBackPressed();
}}

【问题讨论】:

    标签: android retrofit retrofit2


    【解决方案1】:

    在这里改变

     public class CustomViewHolder extends RecyclerView.ViewHolder {
            public TextView employeeName;
            TextView textView;
    
            public CustomViewHolder(View view) {
                super(view);
                employeeName = (TextView) view.findViewById(R.id.WA2);
                textView=view.findViewById(R.id.WA1);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent =  new Intent(context, NextActivity.class);
    
    if(WAmdel.get(getAdapterPosition()==0)
    {
     intent.putExtra("url","http://mapi.trycatchtech.com/v1/android_tutorials/single_tutorial?tutorial_id=1");
    }
    else if(WAmdel.get(getAdapterPosition()==1)
    {
     intent.putExtra("url","http://mapi.trycatchtech.com/v1/android_tutorials/single_tutorial?tutorial_id=2");
    }
    
    
    
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
                        context.startActivity(intent);
    
                    }
                });
    
            }
    

    现在在您的 NextActivity 中获取您的网址,如下所示

    String url = getIntent().getStringExtra("url");
    

    【讨论】:

    • 谢谢,但你能用代码详细说明一下吗..我不明白..
    • 首先发布您启动此适配器的活动或片段,然后告诉我您希望在加载每个链接后,将该数据替换为您当前的适配器数据?
    • 对不起,我没有提到..当点击项目时,它应该在下一个活动中显示数据(当前 api)
    • 查看编辑后的答案。只需在 getAdapterPosition 前面添加 () ,然后接受我的回答
    • 只接受我的编辑请求...我不想公开 http 链接
    【解决方案2】:
    public interface ExampleService { 
    
      String BASE_URL = "https://api.demo.com/";
    
      @GET("Login") //i.e https://api.demo.com/Login? 
      Call<Products> getUserDetails(@Query("email") String emailID, @Query("password") String password)
    
    } 
    

    它将以这种方式调用。考虑到您已经完成了其余的代码。

    Call<Results> call = service.getUserDetails("abc@gmail.com", "Password@123");
    

    例如,当一个查询被返回时,它看起来像这样。

    https://api.demo.com/Login?email=abc@gmail.com&password=Password@123

    【讨论】:

    • 谢谢,但这不是我想要的。你要我添加更多解释吗?
    猜你喜欢
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-09
    • 1970-01-01
    相关资源
    最近更新 更多