gpt4 book ai didi

android - 将选定的值传递给另一个 Activity (使用 recycleview/textView)

转载 作者:行者123 更新时间:2023-11-30 00:26:21 25 4
gpt4 key购买 nike

我正在制作一个 pokedex 应用程序,但我一直坚持将所选口袋妖怪的数据传递给另一个 Activity (以显示更多详细信息)。问题是即使我选择了不同的口袋妖怪,它也会一直向其他 Activity 发送相同的值(列表中的第一个值,在本例中为 bulbasaur)。

该列表由一个 recycleview、textView 和一个 imageView 组成。我使用 textView 的值作为数据发送到其他 Activity 。

所以我想在单击图像时将正确的名称 (textView) 从选定的 pokemon 传递给其他 Activity 。我不确定我错过了什么。

提前致谢!


适配器代码

public class PokemonListAdapter extends RecyclerView.Adapter<PokemonListAdapter.ViewHolder>{

private ArrayList<Pokemon> dataset;
private Context context;

public PokemonListAdapter(Context context){
this.context = context;
dataset = new ArrayList<>();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_pokemon, parent,false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position){
Pokemon p = dataset.get(position);
holder.pokemonTextView.setText(p.getName());

Glide.with(context).load("http://pokeapi.co/media/sprites/pokemon/" + p.getNumber() + ".png")
.centerCrop()
.crossFade()
.into(holder.pictureImageView);

}

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

public void toegevoegdePokemonList(ArrayList<Pokemon> pokemonList) {
dataset.addAll(pokemonList);
notifyDataSetChanged();
}

public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView pictureImageView;
private TextView pokemonTextView;


public ViewHolder(View itemView) {
super(itemView);
pictureImageView = (ImageView) itemView.findViewById(R.id.pictureImageView);
pokemonTextView = (TextView) itemView.findViewById(R.id.pokemonTextView);
}
}

第一个 Activity (列表)

 public class Pokedex extends AppCompatActivity {

private Retrofit retrofit;
private static final String TAG = "POKEDEX";
private RecyclerView recyclerView;
private PokemonListAdapter pokemonListAdapter;
private int offset;
private boolean loaded;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pokedex);

//recycleViewer
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
pokemonListAdapter = new PokemonListAdapter(this);
recyclerView.setAdapter(pokemonListAdapter);
recyclerView.setHasFixedSize(true);

//grid
final GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

if(dy > 0){
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int partVisible = layoutManager.findFirstVisibleItemPosition();

if(loaded){
if((visibleItemCount + partVisible) >= totalItemCount){
Log.i(TAG, "Final");
loaded = false;
offset += 20;
getDataPokemon(offset);
}
}
}
}
});

//retrofit
retrofit = new Retrofit.Builder()
.baseUrl("http://pokeapi.co/api/v2/")
.addConverterFactory(GsonConverterFactory.create())
.build();

loaded = true;
offset = 0;

getDataPokemon(offset);
}


//method
private void getDataPokemon(int offset) {
apiService service = retrofit.create(apiService.class);
Call<PokemonRequest> pokemonRequestCall = service.getPokemonList(20, offset);

pokemonRequestCall.enqueue(new Callback<PokemonRequest>() {
@Override
public void onResponse(Call<PokemonRequest> call, Response<PokemonRequest> response) {
loaded = true;
if (response.isSuccessful()) {

PokemonRequest pokemonRequest = response.body();
ArrayList<Pokemon> pokemonList = pokemonRequest.getResults();

pokemonListAdapter.toegevoegdePokemonList(pokemonList);

} else {
Log.e(TAG, "onResponse: " + response.errorBody());
}
}

@Override
public void onFailure(Call<PokemonRequest> call, Throwable t) {
loaded = true;
Log.e(TAG, "onFailure: " + t.getMessage());
}

});
}


public void pokemon_onClick(View v)
{
TextView textView = (TextView)findViewById(R.id.pokemonTextView);
Intent intent = new Intent();
intent.putExtra("pokemonName", textView.getText().toString());
intent.setClass(this, PokemonDetail.class);
startActivity(intent);
}

Xml 布局文件(第一个 Activity )

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="be.thomasmore.project_idexv2.Pokedex">

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_height="match_parent"
android:layout_width="match_parent"/>


xml布局文件(显示图片和文字)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/pictureImageView"
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_gravity="center_horizontal"
android:onClick="pokemon_onClick"/>

<TextView
android:id="@+id/pokemonTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:gravity="center_horizontal"/>
<!--android:textAllCaps="true"-->


第二个 Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pokemon_detail);

String pokemon = getIntent().getExtras().getString("pokemonName");
TextView textView = (TextView)findViewById(R.id.pokemonName);
textView.setText(pokemon);
}

最佳答案

这样不行。当调用 pokemon_onClick() 时,findViewById 将返回 ID 为 pokemonTextView 的 View 的第一次出现。这就是您在第二个 Activity 中收到相同值的原因。

最简单的解决方案是在您的 ViewHolder 中实现 onClickListener 并直接从那里启动第二个 Activity。

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView pictureImageView;
private TextView pokemonTextView;

public ViewHolder(View itemView) {
super(itemView);

itemView.setClickable(true);
itemView.setOnClickListener(this);

pictureImageView = (ImageView) itemView.findViewById(R.id.pictureImageView);
pokemonTextView = (TextView) itemView.findViewById(R.id.pokemonTextView);
}

@Override
public void onClick(View v) {
Intent intent = new Intent(context, PokemonDetail.class);
intent.putExtra("pokemonName", dataset.get(getAdapterPosition()).getName());
context.startActivity(intent);
}
}

有点复杂但更好的解决方案是创建一个界面并在您的 Activity 中处理点击回调:

在您的适配器中创建接口(interface)并提供 setter :

private PokemonClickListener listener;

public void setListener(PokemonClickListener listener) {
this.listener = listener;
}

public interface PokemonClickListener{
void onPokemonClicked(Pokemon pokemon);
}

如上所述在 ViewHolder 中实现 OnClickListener,但调用监听器方法而不是启动第二个 Activity:

@Override
public void onClick(View v) {
if(listener != null){
listener.onPokemonClicked(dataset.get(getAdapterPosition()));
}
}

在您的 Activity 中,实现 PokemonClickListener 并将监听器设置为适配器。

adapter.setListener(new PokemonClickListener() {
@Override
public void onPokemonClicked(Pokemon pokemon) {
Intent intent = new Intent(this, PokemonDetail.class);
intent.putExtra("pokemonName", pokemon.getName());
startActivity(intent);
}
});

关于android - 将选定的值传递给另一个 Activity (使用 recycleview/textView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45303247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com