我创建了一个具有点击监听器的应用程序,可以打开一个新活动。在该新活动中,它具有一个模板,该模板根据我编写的ArrayList值来更改描述。但是,我似乎无法更改它。它显示的只是一个空的文本视图。
这是我的MainActivity,
package android.com.example.weddingappfinale;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import Adapters.MuaAdapter;
import CustomerActivities.Customer1Activity;
public class MuaActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private MuaAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mua_list);
getSupportActionBar().setTitle("Make Up Artists");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final ArrayList<MuaView> muaView = new ArrayList<>();
muaView.add(new MuaView(R.drawable.mua_image, "Shima Matin Bridal Services", "Shima Matin started in 2012"));
// ArrayList
mRecyclerView = findViewById(R.id.recycler_view_list);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new MuaAdapter(muaView);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
// Item Click listener for CardView and Parcel CardView to new Intent
mAdapter.setOnItemClickListener(new MuaAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
Intent intent = new Intent(MuaActivity.this, Customer1Activity.class);
intent.putExtra("Customer's Details", muaView.get(position));
startActivity(intent);
}
});
}
// Filter/Search Bar
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
mAdapter.getFilter().filter(newText);
return false;
}
});
return true;
}
}
这是我的适配器,
package Adapters;
import android.com.example.weddingappfinale.MuaView;
import android.com.example.weddingappfinale.R;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MuaAdapter extends RecyclerView.Adapter<MuaAdapter.MuaViewHolder> implements Filterable {
private ArrayList<MuaView> mMuaView;
private ArrayList<MuaView> mMuaViewFull;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public static class MuaViewHolder extends RecyclerView.ViewHolder {
public ImageButton mImageButton;
public TextView mTextView1;
public TextView mDescription;
public MuaViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
mImageButton = itemView.findViewById(R.id.mua_imageButton);
mTextView1 = itemView.findViewById(R.id.mua_title);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position);
}
}
}
});
mImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position);
}
}
}
});
}
}
public MuaAdapter(ArrayList<MuaView> muaView) {
mMuaView = muaView;
mMuaViewFull = new ArrayList<>(muaView);
}
@Override
public MuaViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.mua_view, parent, false);
MuaViewHolder mvh = new MuaViewHolder(v, mListener);
return mvh;
}
@Override
public void onBindViewHolder(@NonNull MuaViewHolder holder, int position) {
MuaView currentView = mMuaView.get(position);
holder.mImageButton.setImageResource(currentView.getImageResource());
holder.mTextView1.setText(currentView.getText1());
}
@Override
public int getItemCount() {
return mMuaView.size();
}
public Filter getFilter() {
return MuaFilter;
}
private Filter MuaFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<MuaView> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(mMuaViewFull);
} else {
String filterPattern = constraint.toString().toLowerCase().trim();
for (MuaView item : mMuaViewFull) {
if (item.getText1().toLowerCase().contains(filterPattern)) {
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mMuaView.clear();
mMuaView.addAll((ArrayList) results.values);
notifyDataSetChanged();
}
};
}
这是我的ModelView,
package android.com.example.weddingappfinale;
import android.os.Parcel;
import android.os.Parcelable;
public class MuaView implements Parcelable {
private int mImageResource;
private String mText1;
private String mDescription;
private String mServices1;
private String mServices2;
private String mContact;
private String mAddress;
public MuaView(int imageResource, String text1, String description) {
mImageResource = imageResource;
mText1 = text1;
mDescription = description;
}
protected MuaView(Parcel in) {
mImageResource = in.readInt();
mText1 = in.readString();
}
public static final Creator<MuaView> CREATOR = new Creator<MuaView>() {
@Override
public MuaView createFromParcel(Parcel in) {
return new MuaView(in);
}
@Override
public MuaView[] newArray(int size) {
return new MuaView[size];
}
};
public MuaView(int mua_image, String shima_matin_bridal_services) {
}
public int getImageResource() {
return mImageResource;
}
public String getText1() {
return mText1;
}
public String getmDescription() {
return mDescription;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mImageResource);
dest.writeString(mText1);
}
}
这里是模板xml。
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/image_customer"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="4dp"
android:adjustViewBounds="true"
android:background="@drawable/ic_launcher_background"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/title_customer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/image_customer"
android:layout_centerHorizontal="true"
android:layout_margin="4dp"
android:fontFamily="@font/open_sans_semibold"
android:text="" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description:"
android:layout_below="@id/title_customer"
android:layout_centerHorizontal="true"
android:fontFamily="@font/open_sans_extrabold"/>
<TextView
android:id="@+id/descriptionVendor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description of Vendors"
android:layout_below="@id/description"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/descriptionVendor"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:fontFamily="@font/open_sans_extrabold"
android:text="@string/services" />
<TextView
android:id="@+id/services1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/services"
android:layout_centerHorizontal="true"
android:layout_toEndOf="@id/services"
android:layout_toRightOf="@id/services"
android:text="Services1" />
<TextView
android:id="@+id/services2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/services1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="4dp"
android:layout_toRightOf="@id/services"
android:text="Services2" />
<TextView
android:id="@+id/gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/services2"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:fontFamily="@font/open_sans_extrabold"
android:text="@string/gallery" />
<com.denzcoskun.imageslider.ImageSlider
android:id="@+id/imageslider"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_below="@id/gallery"
android:layout_centerHorizontal="true"
android:layout_margin="4dp"
app:delay="0"
app:placeholder="@drawable/mua_image" />
<TextView
android:id="@+id/contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageslider"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:fontFamily="@font/open_sans_extrabold"
android:text="Contact:"/>
<TextView
android:id="@+id/contactVendor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/contact"
android:layout_centerHorizontal="true"
android:text="ContactVendor" />
<TextView
android:id="@+id/address"
android:layout_below="@id/contactVendor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address:"
android:layout_centerHorizontal="true"
android:fontFamily="@font/open_sans_extrabold"/>
<TextView
android:id="@+id/addressvendor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/address"
android:text="AddressVendor"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
这是模板xml的活动。
package CustomerActivities;
import android.com.example.weddingappfinale.MuaView;
import android.com.example.weddingappfinale.R;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.denzcoskun.imageslider.ImageSlider;
import com.denzcoskun.imageslider.models.SlideModel;
import java.util.ArrayList;
import java.util.List;
public class Customer1Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer1_mua);
Intent intent = getIntent();
MuaView muaView = intent.getParcelableExtra("Customer's Details");
int imageRes = muaView.getImageResource();
String line1 = muaView.getText1();
String description = muaView.getmDescription();
ImageButton imageButton = findViewById(R.id.image_customer);
imageButton.setImageResource(imageRes);
TextView textView = findViewById(R.id.descriptionVendor);
textView.setText(description);
TextView textView1 = findViewById(R.id.title_customer);
textView1.setText(line1);
ImageSlider imageSlider = findViewById(R.id.imageslider);
List<SlideModel> slideModels = new ArrayList<>();
slideModels.add(new SlideModel(R.drawable.catering_image));
slideModels.add(new SlideModel(R.drawable.entertainment_image));
imageSlider.setImageList(slideModels, true);
}
}
更改此:
public class MuaView implements Parcelable {
至此:
public class MuaView implements Serializable{
并更改此
MuaView muaView = intent.getParcelableExtra("Customer's Details");
至此:
MuaView muaView = intent.getSerializableExtra("Customer's Details");