在动态RecyclerView

时间:2018-03-19 05:12:07

标签: android sqlite

我是Android的新手,我在assets文件夹中有一个数据库,它有六个表,每个表共有六列,一列有BLOB数据类型。

在一项活动中,我放置了一个Spinner。 Spinner的工作是,如果用户选择一个选项,将打开一个新的Activity,它显示用户在Spinner中选择的表的内容。

例如,如果您在Spinner中选择好莱坞明星,表格“hollywoodstars”(明星和名称的照片)的内容将显示在动态RecyclerView中的另一个活动中。

现在我的问题是:

  1. 如何根据用户的Spinner选择和
  2. 从数据库中读取内容
  3. 如何在RecyclerView中显示它?
  4. Registration.java -

    public class Registration extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
        EditText t1,t2,t3;
        Spinner sp;
        String sploc,loc;
        DbMethods dbMethods;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);
        t1=(EditText)findViewById(R.id.reguser);
        t2=(EditText)findViewById(R.id.regemail);
        t3=(EditText)findViewById(R.id.regpass);
    
        sp=(Spinner)findViewById(R.id.spinner);
        ArrayAdapter ad=ArrayAdapter.createFromResource(this,R.array.locations,android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(ad);
        sp.setOnItemSelectedListener(this);
    
        dbMethods=new DbMethods(this);
    }
    
    public void registerme(View v)
    {
        String s1=t1.getText().toString();
        String s2=t2.getText().toString();
        String s3=t3.getText().toString();
    
        if(s1.isEmpty() || s2.isEmpty() || s3.isEmpty())
            Ding.ding(getApplicationContext(),"Enter all details!");
        else
        {
            long id=dbMethods.insertUser(s1,s2,s3);
            if(id<=0)
            {
                Ding.ding(getApplicationContext(),"Unsuccessful, Try Again!");
                t1.setText("");
                t2.setText("");
            }
            else
            {
                Ding.ding(getApplicationContext(),"Welcome "+t1.getText()+", Please Sign In!");
                new Handler().postDelayed(new Runnable()
                {
                    public void run()
                    {
                        Intent i=new Intent(Registration.this,Login.class);
                        startActivity(i);
                    }
                },500);
            }
        }
    }
    
    public void regclr(View v)
    {
        t1.setText("");
        t2.setText("");
        t3.setText("");
    }
    
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
    {
        TextView t=(TextView)view;
        loc=t.getText().toString();
    }
    
    @Override
    public void onNothingSelected(AdapterView<?> adapterView) 
    {
    
    }
    }
    

    HomeDbMethods.java -

    public class HomeDbMethods {
    
    static class HomeDbHelper extends SQLiteOpenHelper
    {
    
        public HomeDbHelper(Context context)
        {
    
        }
    
        @Override
        public void onCreate(SQLiteDatabase db)
        {
    
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int i, int i1)
        {
    
        }
    }
    }
    

    我无法在互联网上找到合适的解决方案,既不是谷歌也不是Stack OverFlow,现在我不知道如何处理上述两个类。

1 个答案:

答案 0 :(得分:0)

您使用下面的代码显示精简器控件的选定值的显示记录并从数据库中获取数据。 我希望你能从数据库插入数据,然后我只提供如何获取记录并显示它..

下面用于从所选的微调器值中获取数据的代码。

public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.MyViewHolder> {
private List<Food> mFood;
public int mImpId;
private Context context;
public byte[] image;

public FoodAdapter(Context context, List<Food> mFood) {
    this.context = context;
    this.mFood = mFood;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardlayout, parent, false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(FoodAdapter.MyViewHolder holder, int position) {
    Food mObjFood = mFood.get(position);
    image = mObjFood.getmFoodImage();
    Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
    holder.mIvFoodImage.setImageBitmap(bitmap);
    holder.mTvName.setText(mObjFood.getmFoodName());
    holder.mTvDes.setText(mObjFood.getmFoodDes());
}

@Override
public int getItemCount() {
    return mFood.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    public ImageView mIvFoodImage;
    public TextView mTvName, mTvDes;

    public MyViewHolder(View itemView) {
        super(itemView);
        mIvFoodImage = (ImageView) itemView.findViewById(R.id.dsIvFoodImage);
        mTvName = (TextView) itemView.findViewById(R.id.dsTvFoodName);
        mTvDes = (TextView) itemView.findViewById(R.id.dsTvFoodDes);
    }
}

然后在下面制作回收器视图适配器......

 recyclerView.setLayoutManager(new LinearLayoutManager(this));
    List<Food> mFoodList=new ArrayList<>();
    FoodAdapter adpater;
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            mFoodList.clear();
            mFoodList=displayRecord(spinner.getSelectedItem().toString());
            adpater=new FoodAdapter(this,mFoodList);
            recyclerView.setAdapter(adapter);
            adpater.notifyDataSetChanged();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

}

适配器定义布局创建布局并拍摄图像视图,以及两个textview。 请注意在wrape内容中的xml父布局高度..

然后在主要活动后定义回收者视图并在其中使用下面的代码.. 当微调器项用户选择时使用此代码..

{{1}}