簡體   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