我正在使用一个Android应用程序,该应用程序在“回收列表视图持有者”中显示数据。当我单击Recycler View Holder中的列表项时,应用程序崩溃。
public class UserRecyclerAdapterSavedUsers extends RecyclerView.Adapter<UserRecyclerAdapterSavedUsers.UserViewHolder> {
private List<User> listUsers;
Context mContext;
ItemClickListenerLongPressed itemClickListenerLongPressed;
public UserRecyclerAdapterSavedUsers(List<User> listUsers) {
this.listUsers = listUsers;
}
@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_user_recycler_second, parent, false);
return new UserViewHolder(itemView);
}
@Override
public void onBindViewHolder(UserViewHolder holder, int position) {
holder.textViewID.setText(listUsers.get(position).getUserid());
holder.textViewName.setText(listUsers.get(position).getName());
holder.textViewPassword.setText(listUsers.get(position).getPassword());
holder.textViewRole.setText(listUsers.get(position).getRole());
}
public void setItemClickListenerLongPressed(ItemClickListenerLongPressed itemClickListenerLongPressed){
this.itemClickListenerLongPressed=itemClickListenerLongPressed;
}
@Override
public int getItemCount() {
Log.v(UserRecyclerAdapterSavedUsers.class.getSimpleName(),""+listUsers.size());
return listUsers.size();
}
/**
* ViewHolder class
*/
public class UserViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//public AppCompatTextView ID;
public AppCompatTextView textViewID;
public AppCompatTextView textViewName;
public AppCompatTextView textViewPassword;
public AppCompatTextView textViewRole;
public UserViewHolder(View view) {
super(view);
textViewID = (AppCompatTextView) view.findViewById(R.id.textViewID);
textViewName = (AppCompatTextView) view.findViewById(R.id.textViewName);
textViewPassword = (AppCompatTextView) view.findViewById(R.id.textViewPassword);
textViewRole = (AppCompatTextView) view.findViewById(R.id.textViewRole);
}
@Override
public void onClick(View v) {
if (itemClickListenerLongPressed != null) itemClickListenerLongPressed.onClick(v, getAdapterPosition());
Toast.makeText(mContext, "USMAN", Toast.LENGTH_SHORT).show();
}
}
}
这是用户列表活动
public class UsersListActivity extends AppCompatActivity implements ItemClickListenerLongPressed{
AppCompatActivity activity = UsersListActivity.this;
AppCompatTextView textViewName;
RecyclerView mRecyclerView;
AppCompatButton textViewButtonNewUser;
UserRecyclerAdapterSavedUsers userRecyclerAdapterSavedUsers;
List<User> listUsers;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_record_updated_list);
mRecyclerView= (RecyclerView) findViewById(R.id.recyclerViewUsers);
mRecyclerView.setAdapter(userRecyclerAdapterSavedUsers);
userRecyclerAdapterSavedUsers.setItemClickListenerLongPressed(this);
initViews();
initObjects();
}
@Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(UsersListActivity.this,AdminMain.class));
finish();
}
@Override
protected void onRestart() {
super.onRestart();
}
/**
* This method is to initialize views
*/
private void initViews() {
textViewName = (AppCompatTextView) findViewById(R.id.textViewName);
textViewButtonNewUser = (AppCompatButton) findViewById(R.id.btnaddnew);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerViewUsers);
textViewButtonNewUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(UsersListActivity.this,UserRecordSaveActivity.class));
}
});
}
/**
* This method is to initialize objects to be used
*/
private void initObjects() {
listUsers = new ArrayList<>();
userRecyclerAdapterSavedUsers = new UserRecyclerAdapterSavedUsers(listUsers);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(userRecyclerAdapterSavedUsers);
databaseHelper = new DatabaseHelper(activity);
String emailFromIntent = getIntent().getStringExtra("USERS");
textViewName.setText(emailFromIntent);
getDataFromSQLite();
}
/**
* This method is to fetch all user records from SQLite
*/
private void getDataFromSQLite() {
// AsyncTask is used that SQLite operation not blocks the UI Thread.
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
listUsers.clear();
listUsers.addAll(databaseHelper.getAllUser());
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
userRecyclerAdapterSavedUsers.notifyDataSetChanged();
}
}.execute();
}
@Override
public void onClick(View view, int position) {
}
}
当我单击列表项时,它崩溃了,并且该错误是由Toast引起的。当我移开烤面包时,由于使用未单击的尝试捕获项而导致错误发生。
这是错误的图片。
在删除try catch之后,它再次显示错误,但是这次错误显示在AlertDialog上。建造者。这是没有尝试捕获的错误图像。
Image after removing try and catch
ERROR BEFORE REMOVING TOAST OVER ON CLICK
添加Toast Logcat错误后的图像 Image After Adding Toast
该错误现在位于用户列表活动中 Image after eidting of code
删除点击侦听器时列表中的实际数据 Actual data in list by removing the click listener
这是我的回收站布局文件
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/Indigo"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="User ID"
android:textColor="@color/colorTextHint" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/textViewID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="User ID"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Name"
android:textColor="@color/colorTextHint" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/textViewName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="@string/hint_password"
android:textColor="@color/colorTextHint" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/textViewPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/hint_password"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Role"
android:textColor="@color/colorTextHint" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/textViewRole"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Role"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
</LinearLayout>
这是Logcat的图像
这是IMEIRecord保存活动适配器,如用户记录。
public class IMEIRecyclerAdapter extends RecyclerView.Adapter<IMEIRecyclerAdapter.ImeiViewHolder> {
private List<User> ListImei;
Context mContext;
ItemClickListenerLongPressed itemClickListenerLongPressed;
View itemView;
public IMEIRecyclerAdapter(List<User> ListImei) {
this.ListImei = ListImei;
}
@Override
public ImeiViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_user_recycler_imei, parent, false);
return new ImeiViewHolder(itemView);
}
@Override
public void onBindViewHolder(ImeiViewHolder holder, int position) {
final User user= ListImei.get(position);
holder.textViewImeiId.setText(ListImei.get(position).getImeiid());
holder.textViewImeiNo.setText(ListImei.get(position).getImei());
}
public void setItemClickListenerLongPressed(ItemClickListenerLongPressed itemClickListenerLongPressed) {
this.itemClickListenerLongPressed = itemClickListenerLongPressed;
}
@Override
public int getItemCount() {
Log.v(UsersRecyclerAdapter.class.getSimpleName(),""+ListImei.size());
return ListImei.size();
}
public void displayingAlertDialogimei() {
final User user= new User();
//displaying alert dialog box
AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext());
builder.setTitle("Choose Option");
builder.setMessage("Update or Delete?");
builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//go to update activity
goToUpdateActivity(user.getUserid());
dialog.cancel();
}
});
builder.setNeutralButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//go to update activity
dialog.cancel();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert11 = builder.create();
alert11.show();
}
private void goToUpdateActivity(String userid) {
Intent goToUpdate = new Intent(mContext, RoughUser.class);
goToUpdate.putExtra("USER_ID", userid);
mContext.startActivity(goToUpdate);
}
public class ImeiViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public AppCompatTextView textViewImeiId;
public AppCompatTextView textViewImeiNo;
LinearLayout layout;
public ImeiViewHolder(View view) {
super(view);
textViewImeiId = (AppCompatTextView) view.findViewById(R.id.textViewImeiId);
textViewImeiNo = (AppCompatTextView) view.findViewById(R.id.textViewImeiNo);
layout = (LinearLayout) view.findViewById(R.id.list_view_imei);
layout.setOnClickListener(this);
}
@Override
public void onClick(View v) {
displayingAlertDialogimei();
}
}
}
答案 0 :(得分:0)
您尚未在适配器类中声明mContext
。
在Adapter类中,构造函数可能会这样更改。
public UserRecyclerAdapterSavedUsers(List<User> listUsers,Context context) {
this.mContext= context;
this.listUsers1 = listUsers;
user= new User();
}
和您必须更改的回收视图活动类
UserRecyclerAdapterSavedUsers myAdapter = new RecyclerViewAdapter(yourList,this);
答案 1 :(得分:0)
首先以 android:id =“ @ + id / list_view”
将 id 添加到父级LinearLayout中然后更新适配器类
<entry key="processorTypes" value="#{ '${processors}'.split(',') }"/>
答案 2 :(得分:0)
像这样click here
使用“ Recyclerview项目”然后您可以访问activity
或fragment
中的界面,然后可以添加所需的任何内容。
toast
并在AlertDialog
内填充Adapter
并不是正确的编码方式