我想顯示alertdialog preferecactivity,但recyclerview顯示是這樣的。 RecyclerView查看錯誤On PreferanceActivity
我的代碼....
cata_manage.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(final Preference preference) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(preference.getContext());
LayoutInflater inflater = (LayoutInflater)preference.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View dialogView = inflater.inflate(R.layout.cata_manage, null);
dialogBuilder.setView(dialogView);
final AlertDialog alertDialog2 = dialogBuilder.create();
final RecyclerView cata_list = (RecyclerView) dialogView.findViewById(R.id.cata_manage_recycler);
update_recycler_cata("Expenses", cata_list,preference.getContext());
alertDialog2.show();
return false;
}
});
private void update_recycler_cata(String cata, RecyclerView catalist,Context context) {
DBHelper mydb = new DBHelper(this);
Cursor catas = null;
if (cata == null || Objects.equals(cata, "Expenses")) {
catas = mydb.fetchall_cata_ex_names();
} else if (Objects.equals(cata, "Income")) {
catas = mydb.fetchall_cata_in_names();
}
CataRecyclerAdapter adapter = new CataRecyclerAdapter(catas, context, catalist);
RecyclerView.LayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(context);
catalist.setLayoutManager(mLayoutManager);
// recyclerView.setHasFixedSize(true);
catalist.setAdapter(adapter);
}
CataRecyclerAdapter.java
public class CataRecyclerAdapter extends RecyclerView.Adapter<CataRecyclerAdapter.RecyclerViewHolder> {
private Cursor cursor;
private Context context;
private RecyclerView catalist;
CataRecyclerAdapter(Cursor cursor, Context context, RecyclerView catalist) {
this.cursor = cursor;
this.context = context;
this.catalist = catalist;
}
@Override
public CataRecyclerAdapter.RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.catamanage_singleitem, parent, false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
@Override
public void onBindViewHolder(CataRecyclerAdapter.RecyclerViewHolder holder, final int position) {
cursor.moveToPosition(position);
holder.item.setText(cursor.getString(1));
}
@Override
public int getItemCount() {
return cursor.getCount();
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
ImageButton edit, delete;
TextView item;
@TargetApi(Build.VERSION_CODES.KITKAT)
public RecyclerViewHolder(View itemView) {
super(itemView);
item = (TextView) itemView.findViewById(R.id.textView34);
edit = (ImageButton) itemView.findViewById(R.id.imageButton2);
delete = (ImageButton) itemView.findViewById(R.id.imageButton3);
}
}
}
XML代碼(catamanage_singleitem.xml)。 ..
<LinearLayout 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="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/textView34"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text="TextView"
android:textSize="18sp" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
app:srcCompat="@android:drawable/ic_menu_edit" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
app:srcCompat="@android:drawable/ic_menu_delete" />
</LinearLayout>
</android.support.v7.widget.CardView>
我試過很多方法,我怎麼能解決這個問題?我在catamanage_singleitem
裏添加了一個imageview,但是沒有顯示imageview。我如何解決這個問題,這是由於我所採用的上下文值。
什麼是您預期的輸出? –
我已更新我的問題 – Tom
您可以發佈recyclerview的列表item.xml和update_recycler_cata方法 – Raghavendra