我正在制作一个pokedex应用程序而且我坚持将所选择的精灵宝贝的数据传递给另一个活动(以显示更多细节)。问题是,即使我选择了一个不同的口袋妖怪,它仍然会向另一个活动(列表中的第一个值,在本例中为bulbasaur)发送相同的值。
该列表由recycleview,textView和imageView组成。我使用textView的值作为数据发送到其他活动。
因此,当我点击图片时,我想将所选口袋妖怪的正确名称(textView)传递给其他活动。我不确定我错过了什么。
提前致谢!
适配器代码
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);
}
}
第一项活动(名单)
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布局文件(第一个活动)
<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"-->
第二项活动
@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);
}
答案 0 :(得分:1)
它不会以这种方式工作。当调用pokemon_onClick()时,findViewById将返回id pokemonTextView 的第一个视图。这就是你在第二个活动中收到相同价值的原因。
最简单的解决方案是在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中创建一个界面并处理点击回调:
在适配器中创建接口并提供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()));
}
}
在您的活动中,实现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);
}
});
答案 1 :(得分:0)
在第二项活动中,替换:
String pokemon = getIntent().getExtras().getString("pokemonName");
by:
String pokemon = getIntent().getStringExtra("pokemonName");
解释:
在第一个活动中,您在Intent中传递一个String,因此您必须通过getIntent().getStringExtra
检索它。
getIntent().getExtras()
返回传递的Bundle:
Bundle b = new Bundle();
b.putString("key", "value"); // put String into the Bundle, not the Intent
intent.putExtras(b); // Put the Bundle into the Intent
答案 2 :(得分:0)
在您的适配器ViewHolder类中设置itemView.OnClickListner(),如
itemView.SetOnClickListner(new OnClickListner)
{
@override
OnClick()
{
String txt_value = yourText.getText().toString();
Intent i = new Intent(context, NewActivity.class);
i.putExtra("txt_value",txt_value);
startActivity(i);
}
}