繁体   English   中英

如何将不同api id的数据显示到回收视图的不同项目?

[英]how to display data of different api ids to different items of recycle view?

我将简要地写下我真正想要的内容以及我确切的位置。我正在使用改造(我已成功加载)从 api 加载回收视图中的列表(我已成功加载),请参阅此图像-->此处的图像描述

此外,我有两个 api id(例如:https//..id=1 和 https//..id=2 等等,具体取决于此处的列表图像描述。)

这是我的 2 个 apis:(第一个 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 加载到下一个活动,如下所示-->此处的图像描述

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

这是我的 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);
        
            }
        });

    }
}

}

这是我的回收视图项目适配器(此处图像描述):

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 时,它进入下一个屏幕显示与前一个相同的描述..我应该在界面方法中做什么改造?

需要帮助...提前致谢

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

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);

在适配器中,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);
     }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM