【问题标题】:how to display data of different api ids to different items of recycle view?如何将不同api id的数据显示到recyclerview的不同项?
【发布时间】:2019-12-24 20:49:38
【问题描述】:

我将简要写下我到底想要什么以及我到底卡在哪里。我正在使用改造从 api 加载 recycleview 中的列表(我已成功加载)查看此图像-->image description here

此外,我还有两个 api id(例如:https//..id=1 和 https//..id=2 等等,具体取决于此列表image description here。)

这是我的 2 个 api:(第一个 https//..id=1)

{"id":"1","title":"Android Introduction","description":"
Android is a Linux based operating system it is designed primarily for touch screen mobile devices such as smart phones and tablet computers. The operating system have developed a lot in last 15 years starting from black and white phones to recent smart phones or mini computers. One of the most widely used mobile OS   these days is android.  The android is software that was founded in Palo Alto of California in 2003.<\/p>\r\n\r\n

 \"\"<\/p>\r\n\r\n

 The android is a powerful operating system and it supports large number of applications in Smartphones. These applications are more comfortable and advanced for the users. The hardware that supports android software is based on ARM architecture platform. The android is an open source operating system means that it’s free and any one can use it. The android has got millions of apps available that can help you managing your life one or other way and it is available low cost in market at that reasons android is very popular.<\/p>\r\n\r\n

 \"\"<\/p>\r\n\r\n

 The android development supports with the full java programming language. Even other packages that are API and JSE are not supported. The first version 1.0 of android development kit (SDK) was released in 2008 and latest updated version is jelly bean.<\/p>\r\n"}

第二个:(https//..id=2)

{"id":"2","title":"Android Studio","description":"
 Meet Android Studio<\/p>\r\n\r\n

 Android Studio is the official Integrated Development Environment (IDE) for Android app development, based onIntelliJ IDEA <\/a>. On top of IntelliJ's powerful code editor and developer tools, Android Studio offers even more features that enhance your productivity when building Android apps, such as:<\/p>\r\n\r\n

   \r\n\t
  A flexible Gradle-based build system<\/li>\r\n\t
  A fast and feature-rich emulator<\/li>\r\n\t
  A unified environment where you can develop for all Android devices<\/li>\r\n\t
  Apply Changes to push code and resource changes to your running app without restarting your    app<\/li>\r\n\t
  Code templates and GitHub integration to help you build common app features and import sample code<\/li>\r\n\t
   Extensive testing tools and frameworks<\/li>\r\n\t
   Lint tools to catch performance, usability, version compatibility, and other problems<\/li>\r\n\t
  C++ and NDK support<\/li>\r\n\t
   Built-in support for Google Cloud Platform<\/a>, making it easy to integrate Google Cloud    Messaging and App Engine<\/li>\r\n<\/ul>\r\n\r\n
   This page provides an introduction to basic Android Studio features. For a summary of the latest changes, seeAndroid Studio release notes<\/a>.<\/p>\r\n"}

正如您从 apis 中看到的那样,点击项目时应该将它们各自的描述从 apis 加载到下一个活动,就像这样-->image description here

如果你愿意,我可以分享代码。告诉你具体需要什么。我会编辑

这是我的 nextactivity 适配器代码:

public class NextAndroidAdapter  extends RecyclerView.Adapter<NextAndroidAdapter.CustomViewHolder> {
DescriptionModel WAmdel;
Context context;
public NextAndroidAdapter(Context context,DescriptionModel employees) {
    this.WAmdel = employees;
    this.context=context;
}



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

    return new CustomViewHolder(itemView);
}

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

  // holder.textView.setText(String.valueOf(position+1)+". ");
   holder.textView.setText(WAmdel.getDescription());

}

@Override
public int getItemCount() {
    return 1;
}

public class CustomViewHolder extends RecyclerView.ViewHolder {
    TextView textView;

    public CustomViewHolder(View view) {
        super(view);
       // employeeName = (TextView) view.findViewById(R.id.WA2);
        textView=view.findViewById(R.id.detailswhatsandroid);
        
            }
        });

    }
}

}

这是我的回收视图项适配器(image description here):

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, final 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);
                intent.putExtra("title", WAmdel.get(getAdapterPosition()).getTitle());

                context.startActivity(intent);

            }
        });

    }
}

}

下一个活动:

public class NextActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private NextAndroidAdapter adapter;
private DescriptionModel DescriptList;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nextwhatsandroid_layout);
    Toolbar toolbar = (Toolbar) findViewById(R.id. toolbar );
    setSupportActionBar( toolbar );
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
    Intent intent = getIntent();
    String title = intent.getStringExtra("title");
  String hello=intent.getStringExtra("hii");

    getSupportActionBar().setTitle(title);
    /*Create handle for the RetrofitInstance interface*/
    DescriptService service = DescriptClientInstance.getRetrofitInstance().create(DescriptService.class);
    Call<DescriptionModel> call = service.getAllPhotos();
    call.enqueue(new Callback<DescriptionModel>() {
        @Override
        public void onResponse(Call<DescriptionModel> call, Response<DescriptionModel> response) {
           // progressDialog.dismiss();
            DescriptList=response.body();
            generateDataList(DescriptList);
        }

        @Override
        public void onFailure(Call<DescriptionModel> call, Throwable t) {
           // progressDialog.dismiss();

            Toast.makeText(getApplicationContext(), "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
        }
    });
}
private void generateDataList(DescriptionModel photoList) {
    recyclerView = findViewById(R.id.recyclenext);
    LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(manager);
    recyclerView.setHasFixedSize(true);
    adapter = new NextAndroidAdapter(getApplicationContext(),photoList);
    recyclerView.setAdapter(adapter);
}
@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();
}

}

但现在我像这样出去了: 当我点击 item0 时,它进入下一个屏幕并显示 item1 的描述,但是当我点击 item1 时,它进入下一个屏幕显示与上一个相同的描述......我应该在改造的界面方法中做什么?

需要帮助...提前致谢

【问题讨论】:

  • 请检查WAmdelWhatsAndroidAdapter 是否有正确的数据。
  • 这是我通过的改造界面 --> @GET("/v1/android_tutorials/single_tutorial?tutorial_id=1") Call getAllPhotos();..这就是我得到的原因两个回收视图项中的相同描述
  • id=2,3等怎么办?
  • 这里在v1/android_tutorials/single_tutorial?tutorial_id=1 中设置了tutorial_id=1。所以它总是会获取 id=1 的数据。这就是为什么您总是看到 id=1 的数据。您必须获取不同 ID 的数据。您必须按以下方式调用 api:@GET("/v1/android_tutorials/single_tutorial") Call&lt;DescriptionModel&gt; getAllPhotos(@Query("tutorial_id") int id);
  • @shafayathossain 我在适配器(下一个活动)和活动(下一个活动)中做了哪些更改?...我在上面添加了我的活动代码(下一个活动)......请帮助

标签: android retrofit retrofit2


【解决方案1】:

据我了解您想要做什么,据此我假设您需要进行以下更改:

WhatsAndroidAdapter

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);
        intent.putExtra("title", WAmdel.get(getAdapterPosition()).getTitle());
        intent.putExtra("id", WAmdel.get(getAdapterPosition()).getId());

        context.startActivity(intent);

    }
});

NextActivity

Intent intent = getIntent();
String title = intent.getStringExtra("title");
String id = intent.getStringExtra("id");
String hello=intent.getStringExtra("hii");

getSupportActionBar().setTitle(title);
/*Create handle for the RetrofitInstance interface*/
DescriptService service = DescriptClientInstance.getRetrofitInstance().create(DescriptService.class);
Call<DescriptionModel> call = service.getAllPhotos(id);
call.enqueue(new Callback<DescriptionModel>() {
    @Override
    public void onResponse(Call<DescriptionModel> call, Response<DescriptionModel> response) {
       // progressDialog.dismiss();
        DescriptList=response.body();
        generateDataList(DescriptList);
    }

    @Override
    public void onFailure(Call<DescriptionModel> call, Throwable t) {
       // progressDialog.dismiss();

        Toast.makeText(getApplicationContext(), "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
    }
});

DescriptService

@GET("/v1/android_tutorials/single_tutorial") 
Call<DescriptionModel> getAllPhotos(@Query("tutorial_id") String id);

【讨论】:

  • 请检查WAmdel.get(getAdapterPosition()).getId()是否返回int类型的任何有效id?
  • int的类型吗?
  • 根据你的帖子idString,请改成String
  • 我已经更新了我的答案。在DescriptionModel 中将id 更改为String
  • 我改变了我的答案。会是String id = intent.getStringExtra("id");
【解决方案2】:

在适配器中,onBinding 方法添加项目点击列表并在位置基础上发送描述,并通过意图或通过参数传递描述,如下所示:

holder.itemView.setonClickListener(new OnItemClickListner{

      override
      onClick(View view){
           intent i=new Intent(context,YOUR_ACTIVITY);
           i.putString("description",you_data_list.get(postion)
           .getString("description");
           context.startActivity(i);
     }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    相关资源
    最近更新 更多