mysql - 我如何实现具有两行的 RecyclerView,其中每行从不同的对话框(DIALOG_A,DIALOG_B)获取数据?

标签 mysql android-recyclerview android-viewholder

我创建了两个 xml 文件来表示两行:laptops_holder 和 console_holder.xml。 这些将用来自两个对话框 DIALOG_A 和 DIALOG_B 的数据进行扩充。 从这些对话框中,数据将保存到 sqlDatabase 但在不同的表下。 TABLE_LAPTOPS 和 TABLE_CONSOLES。 我有一行显示由笔记本电脑支架中的 DIALOG_A 提供的信息... 第二行是显示来自 DIALOG_B(到 console_holder.xml)的信息,但是当我单击 | 时应用程序崩溃了btn_save_d_console | btn_save_d_console |在(DIALOG_B)中。

我看了这个。 http://www.androidwarriors.com/2016/02/android-sqlite-database-tutorial-sqlite.htmlhttp://androidheight.blogspot.ug/?m=1 。想要更多但没有运气..

这是我尝试过的... MyMainActivity.java

public class MyMainActivity extends AppCompatActivity {
private final ArrayList<entry> mainEntries = new ArrayList<>();


DBAdapter db ;
RecyclerView rview;
RVAdapter adapter;
private EditText laptop_name, l_cost;
private EditText console_nme, c_cost;
Button btn_save_d;
 Button btn_retrive_d;

@Override
protected void onPause() {
    super.onPause( );
  getLaptopsEntries( );
         getConsolesEntries( );

   }


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


        //changed the fabs to buttons
   Button open_dialog_a = (Button)findViewById(R.id.dialog_a);
    Button open_dialog_b = (Button) findViewById(R.id.fab_dialog_b);

    rview = (RecyclerView) findViewById(R.id.rv) ;
    if (rview != null) {
        rview.setHasFixedSize(true);

    }
    LinearLayoutManager layoutManager = new LinearLayoutManager(this, 
     OrientationHelper.VERTICAL, false);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    rview.setLayoutManager(layoutManager);
    rview.setItemAnimator(new DefaultItemAnimator());
    adapter = new RVAdapter(this, mainEntries);
    rview.setAdapter(adapter);

      getLaptopsEntries( );
      getConsolesEntries( );


    if (open_dialog_b != null) {
        open_dialog_b.setOnClickListener(new View.OnClickListener( ) {
            @Override
            public void onClick(View v) {


             open_Dailog_B();


            }
        });
    }
    if (open_dialog_a != null) {
       open_dialog_a.setOnClickListener(new View.OnClickListener() {
              @Override
        public void onClick(View v) {
                                  open_Dailog_A();
                              }
                          });
              }





   private void  open_Dailog_A() {

    Dialog d = new Dialog(this);
    d.setTitle("  LAPTOPS ");
    d.setContentView(R.layout.mylaptops);
    laptop_name = (EditText) d.findViewById(R.id.lpname);
    laptop_cost = (EditText) d.findViewById(R.id.lpcost);
    btn_save_laptop = (Button) d.findViewById(R.id.btn_save_laptop);
    btn_retrive_laptop = (Button)   
   d.findViewById(R.id.btn_retrive_laptop);


    btn_save_laptop.setOnClickListener(new View.OnClickListener( ) {

        @Override
        public void onClick(View v) {

            save_laptops(laptop_name.getText( ).toString( ).trim( ),
                    laptop_cost.getText( ).toString( ).trim( ));

        }
    });

    //retrive laptop entries
    btn_retrive_laptop.setOnClickListener(new View.OnClickListener( ) {
        @Override

        public void onClick(View v) {

            getLaptopEntries( );


        }
    });
    d.show();

  }

  private void save_laptops(String laptop, String cost) {

    DBAdapter db = new DBAdapter(this);
    db.openDB( );

    //laptop and cost objects are from our dbAdapter
    if (db.insert_Laptops(laptop, cost)) {

        laptop_name.setText("");
        laptop_cost.setText("");
        Snackbar.make(entry_one, "saved ", Snackbar.LENGTH_SHORT).show();

    } else {

        Toast.makeText(this, "error saving cash entries",  
  Toast.LENGTH_SHORT).show( );
     }
     db.closeDB( );
   }

   public void getLaptopsEntries( ) {


    mainEntries.clear( );
   db = new DBAdapter(this);
    db.openDB( );
    Cursor c = db.retrieve( );

    while (c.moveToNext( )) {
        int id = c.getInt(0);
        String laptop = c.getString(1);
        String cost = c.getString(2);
        DateFormat dateFormat = 
     java.text.SimpleDateFormat.getDateTimeInstance( );
        String time =
                dateFormat.format
                        (new   
    Date(c.getLong(c.getColumnIndex(Constants.DATE_NAME))).getTime( ));

    //entry is our pojo with all setters and getters needed
        entry laptops = new entry();

       laptops.setLaptopName(laptop);
        laptops.setLaptopCost(cost);
       laptops.setTime_date(time);
        laptops.setId(id);
        laptops.add(laptops);

    }
    db.closeDB( );
    if (!(mainEntries.size( ) > 0)) {
        rview.setAdapter(adapter); //
    }
  }


 private void open_Dailog_B(){

Dialog console_dialog = new Dialog(this);

console_dialog.setTitle("Console sales entry");
console_dialog.setContentView(R.layout.consoles_dialog);

 entry_one_exp =   
(EditText)console_dialog.findViewById(R.id.entry_one_exp);
    entry_two_exp = (EditText)   
  console_dialog.findViewById(R.id.entry_two_exp);
   Button save_console_name = (Button)   
 console_dialog.findViewById(R.id.btn_save_dialog_console);
   Button btn_retrive_console = (Button) 
  console_dialog.findViewById(R.id.btn_retrive_console);

   btn_save_d_console.setOnClickListener(new View.OnClickListener( ) {
       @Override
       public void onClick(View v) {


           saveConsoles(console_name.getText().toString().trim(),
                  c_cost.getText().toString().trim());
       }
   });

   btn_retrive_console.setOnClickListener(new View.OnClickListener( ) {
       @Override
       public void onClick(View v) {
           try {
               getConsolesEntries();
           } catch (Exception e) {
               e.printStackTrace( );
           }
       }
   });

  console_dialog.show();

}
  //this pulls data from the sqldatabase
private void  saveConsoles(String console, String cost) {
    DBAdapter db = new DBAdapter(this);
    db.openDB();

    if (db.insertConsole(console, cost)) {

       console_name.setText("");
           c_cost.setText("");
        Toast.makeText(this, "saved", Toast.LENGTH_SHORT).show();

    } else {
        Toast.makeText(this, "error saving", Toast.LENGTH_SHORT).show();
    }
    db.closeDB( );

getConsolesEntries( );

}


public void getConsolesEntries( ) {

  db = new DBAdapter(this);
 db.openDB();
    mainEntries.clear();
   Cursor c = db.retrieve_Consoles();
   while(c.moveToLast()) {
     int id = c.getInt(0);
     String console = c.getString(1);
     String cost = c.getString(2);

     java.text.DateFormat dateFormat =       
  java.text.SimpleDateFormat.getDateTimeInstance();
     String c_time = dateFormat.format(new 
  Date(c.getLong(c.getColumnIndex(Constants.CONSOLE_DATE))).getTime());

     entry console_entry = new entry();
    console_entry.setCOnsole(console);
     console_entry.setCost(cost);
     console_entry.setExp_date(c_time);
    console_entry.setExp_id(id);
     console_entrys.add(console_entry);
 }

 db.closeDB( );
 if (mainEntries.size( ) > 0) {
     rview.setAdapter(console_entry);
     }

  }




 @Override
 protected void onPostResume() {
    super.onPostResume( );

   getLaptopsEntries( );

  getConsolesEntries( );

 }
    }

在recyclerview中插入数据的适配器(RVAdapter.java)

  public class RVAdapter extends RecyclerView.Adapter{

    //these are to holder infomation in the recyclerViewHolder
    public LapTopsHolder laptops_holder;
    public ConsoleHolder console_holder;

  public  static final int ITEM_LAPTOP = 0;
  public  static final int ITEM_CONSOLE = 1;

  Context c;
  ArrayList<entry> main_entries;

    public RVAdapter(Context c, ArrayList<entry> main_entries) {
        this.c = c;
    this.main_entries = main_entries;
   }


   @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,   
      int viewType) {
     if ( viewType  == ITEM_LAPTOP) {

       View v = LayoutInflater.from(parent.getContext(     

         )).inflate(R.layout.laptop_card, parent, false);
       holder = new LapTopsHolder(v);
       return holder;

     } else if (viewType == ITEM_CONSOLE){

        View v2 = LayoutInflater.from(parent.getContext( )).inflate(
          R.layout.console_card,  parent, false);
       console_holder   = new ConsoleHolder(v2);
        return console_holder;

    }

    return null;
    }

      /*public void setAdapter(RVAdapter adapter) {
      // bail out if layout is frozen
      setLayoutFrozen(false);
      setAdapterInternal(adapter, false, true);
      requestLayout();
     }*/


     @Override
     public void onBindViewHolder(RecyclerView.ViewHolder holder, int 
        position) {

    entry curEntry = main_entries.get(position);

    switch (position){
        case ITEM_LAPTOP:

           LapTopsHolder holder1 = (LapTopsHolder) holder;

            holder1.laptop_name.setText(curEntry.getLaptopName());
            holder1.laptop_cost.setText(curEntry.getLapTopAmount());
            holder1.ntryTime.setText(curEntry.getTime_date());

            break;

           case ITEM_CONSOLE:
           ConsoleHolder view_holdr = (ConsoleHolder) holder;

           view_holdr.console_name.setText(curEntry.getConsoleName());
           view_holdr.console_cost.setText(curEntry.getConsoleCost());
           view_holdr.console_time.setText(curEntry.getConsole_date());

            break;

        }

      }

    @Override
    public int getItemCount() {

     //  return main_entries.size();
      return main_entries == null ? 0 :main_entries.size();

     }

  @Override
  public int getItemViewType(int position) {

  switch (position) {
        case 0:

            return ITEM_LAPTOP;
      case 1:

          return ITEM_CONSOLE;
      default:


  }
     return position;
 }

   public void deleteEntries(int pos) {
   entry entry_delete = main_entries.get(pos);

    int id_laptop = entry_delete.getId();
    int id_console = entry_delete.getConsole_id();
    DBAdapter db =  new DBAdapter(c);

    db.openDB();
    if(db.delete(id_laptop)) {

        main_entries.remove(pos);
    } else {

        Toast.makeText(c, "Unable To Delete", Toast.LENGTH_SHORT).show();
    } if (db.delete(id_console)) {

        main_entries.remove(pos);
    }
    db.closeDB();

    this.notifyItemRemoved(pos);
      }
  }

consolesHolder.java

    public class  ConsoleHolder  extends  RecyclerView.ViewHolder {
    TextView c_name;
   TextView c_cost;
   TextView c_id;
      TextView c_time;


  public  ConsoleHolder (View itemView) {
    super(itemView);
    c_name = (TextView) itemView.findViewById(R.id.console_name);
    c_cost = (TextView) itemView.findViewById(R.id.console_cost);
    c_id = (TextView) itemView.findViewById(R.id.console_id);
    c_time = (TextView) itemView.findViewById(R.id.console_time);

 }

    }

LapTopsHolder.java

 public class LapTopsHolder extends RecyclerView.ViewHolder  {

public TextView lp_name;
TextView     lp_time;
TextView    lp_cost;
TextView    lp_id;



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

    mView = itemView;
    lp_id = (TextView) itemView.findViewById(R.id.laptop_id);
    lp_name = (TextView) itemView.findViewById(R.id.laptop_name);
    lp_cost = (TextView) itemView.findViewById(R.id.laptop_cost);
    lp_time = (TextView) itemView.findViewById(R.id.laptop_time);
   }
   }

这是我的日志猫..

     java.lang.NullPointerException
 at
android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5483) 
- at
android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4707)
                                                                                          at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
                                                                                          at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
                                                                                          at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
                                                                                          at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
                                                                                          at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
                                                                                          at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
                                                                                          at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906)
                                                                                          at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
                                                                                          at android.view.View.layout(View.java:15125)
                                                                                          at android.view.ViewGroup.layout(ViewGroup.java:4862)
                                                                                          at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
                                                                                          at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
                                                                                          at android.widget.TableLayout.onLayout(TableLayout.java:448)
                                                                                          at android.view.View.layout(View.java:15125)
                                                                                          at android.view.ViewGroup.layout(ViewGroup.java:4862)
                                                                                          at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1160)
                                                                                          at android.view.View.layout(View.java:15125)
                                                                                          at android.view.ViewGroup.layout(ViewGroup.java:4862)
                                                                                          at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1187)
                                                                                          at android.view.View.layout(View.java:15125)
                                                                                          at android.view.ViewGroup.layout(ViewGroup.java:4862)
                                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
                                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
                                                                                          at android.view.View.layout(View.java:15125)
                                                                                          at android.view.ViewGroup.layout(ViewGroup.java:4862)
                                                                                          at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
                                                                                          at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
                                                                                          at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
                                                                                          at android.view.View.layout(View.java:15125)
                                                                                          at android.view.ViewGroup.layout(ViewGroup.java:4862)
                                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
                                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
                                                                                          at android.view.View.layout(View.java:15125)
                                                                                          at android.view.ViewGroup.layout(ViewGroup.java:4862)
                                                                                          at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
                                                                                          at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
                                                                                          at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
                                                                                          at android.view.View.layout(View.java:15125)
                                                                                          at android.view.ViewGroup.layout(ViewGroup.java:4862)
                                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
                                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
                                                                                          at android.view.View.layout(View.java:15125)
                                                                                          at android.view.ViewGroup.layout(ViewGroup.java:4862)
                                                                                          at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2323)
                                                                                          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2029)
                                                                                          at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192)
                                                                                          at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231)
                                                                                          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
                                                                                          at android.view.Choreographer.doCallbacks(Choreographer.java:591)
                                                                                          at android.view.Choreographer.doFrame(Choreographer.java:560)
                                                                                          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
                                                                                          at android.os.Handler.handleCallback(Handler.java:808)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                                          at android.os.Looper.loop(Looper.java:193)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5333)
                                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                          at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)

最佳答案

我通过创建一个 tablayout 和一个 viewpager 来保存两个 recyclerviews,一个用于显示对话框 A 中的值,另一个用于显示对话框 B 中的值来解决它

关于mysql - 我如何实现具有两行的 RecyclerView,其中每行从不同的对话框(DIALOG_A,DIALOG_B)获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39385289/

相关文章:

android - CardView 中的 Google Map Lite

android - 这是在 Android 中实现 RecyclerView 的错误方法吗?

Mysql 查询 Left Join with Not in

RecyclerView Adapter 中的 Android ViewModel 用于惰性数据库下载

java - 如何跟踪 Android 中 RecyclerView 中的选定项目?

android - 如何避免装饰recyclerview的第一行

mysql - 中止更改后出现重复列名错误

php - 使用数据库时表结构应该如何工作?

mysql - 删除数据库的前 X 行

Android ArrayAdapter 无法正确转换 View