导航到sqlite数据库中的下一个条目-Android

时间:2018-11-20 19:34:44

标签: java android android-sqlite

我正在开发一个应用程序,并且具有以下文件:

MainActivity.java -填充列表视图

//...
mDBHelper = new DatabaseHelperArchitects(this);
File database = getApplicationContext().getDatabasePath(DatabaseHelperArchitects.DBNAME);

AZModelArchitectsList = mDBHelper.getListWord("");
word_adapter = new Architects_WordAdapter();
word_adapter.setData(AZModelArchitectsList);
rvWord.setAdapter(word_adapter);
//...

DatabaseHelperArchitects.java -用于打开数据库,查询数据并在模型列表中添加数据,升级数据库方法,关闭数据库方法等。

public List<AZModelArchitects> getListWord(String wordSearch) {
        AZModelArchitects dictionaryModel = null;
        List<AZModelArchitects> dictionaryModelList = new ArrayList<>();

        openDatabase();
        String[] args = {"%" + wordSearch + "%", "%" + wordSearch + "%"};

        cursor = mDatabase.rawQuery("Select * From architects_az Where architect_name Like ? OR architect_period Like ?", args);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            dictionaryModel = new AZModelArchitects(
                    cursor.getString(1),
                    cursor.getString(2),
                    cursor.getString(3),
                    cursor.getString(4),
                    cursor.getString(5)
            );
            dictionaryModelList.add(dictionaryModel);
            cursor.moveToNext();


        }

        cursor.close();
        closeDatabase();
        return dictionaryModelList;

    }

AZModelArchitects -模型

public class AZModelArchitects {

    private String id;
    private String architect_image;
    private String architect_description;
    private String architect_period;
    private String architect_source;

    public AZModelArchitects(String id, String architect_image, String architect_description, String architect_period, String architect_source) {
        this.id = id;
        this.architect_image = architect_image;
        this.architect_description = architect_description;
        this.architect_period = architect_period;
        this.architect_source = architect_source;
    }


    public String getId() {
        return id;
    }

    public String getArchitect_image() {
        return architect_image;
    }

    public String getArchitect_description() {
        return architect_description;
    }

    public String getArchitect_period() {
        return architect_period;
    }

    public String getArchitect_source() {
        return architect_source;
    }


    public void setId(String id) {
        this.id = id;
    }

    public void setArchitect_image(String architect_image) {
        this.architect_image = architect_image;
    }

    public void setArchitect_description(String architect_description) {
        this.architect_description = architect_description;
    }

    public void setArchitect_period(String architect_period) {
        this.architect_period = architect_period;
    }

    public void setArchitect_source(String architect_source) {
        this.architect_source = architect_source;
    }
}

Architects_WordAdapter -使用RecyclerView创建ViewViewHolder和BindViewHolder,我将数据提供给新的Intent(这是我的 ArchitectsDetailsActivity.java

public class Architects_WordAdapter extends RecyclerView.Adapter<Architects_WordAdapter.ViewHolder> {

    public List<AZModelArchitects> data;

    public Architects_WordAdapter() {
    }

    public void setData(List<AZModelArchitects> data) {
        this.data = data;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        View wordView = inflater.inflate(R.layout.entry_list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(wordView, context);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        AZModelArchitects dictionaryModel = data.get(position);
        holder.Text1.setText(dictionaryModel.getId());
        holder.Text2.setText(dictionaryModel.getArchitect_period());

    }


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


    // int position = getAdapterPosition()-1;



    public class ViewHolder extends RecyclerView.ViewHolder {
        public Context context;
        public TextView Text1;
        public TextView Text2;

        public ViewHolder(View itemView, final Context context) {
            super(itemView);
            this.context = context;
            Text1 = itemView.findViewById(R.id.textlista);
            Text2 = itemView.findViewById(R.id.textlista_cat);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = getAdapterPosition();
                    AZModelArchitects dictionaryModel = data.get(position);

                    Intent intent = new Intent(context, ArchitectsDetailsActivity.class);
                    intent.putExtra("architect_name", dictionaryModel.getId());
                    intent.putExtra("architect_image", dictionaryModel.getArchitect_image());
                    intent.putExtra("architect_description", dictionaryModel.getArchitect_description());
                    intent.putExtra("architect_period", dictionaryModel.getArchitect_period());
                    intent.putExtra("architect_source", dictionaryModel.getArchitect_source());

                    context.startActivity(intent);
                }
            });


        }

    }
}

ArchitectsDetailsActivity.java -从Architects_WordAdapter.java获取我的StringExtra

//...
String stringPoza = getIntent().getStringExtra("architect_image");
        String stringMainWord = getIntent().getStringExtra("architect_name");

        String stringDescription = getIntent().getStringExtra("architect_description");
        String stringPeriod = getIntent().getStringExtra("architect_period");
        String stringSource = getIntent().getStringExtra("architect_source");


        architect_name_Text = findViewById(R.id.mainTextPagina);
        viewPoza = findViewById(R.id.poza_pagina_detaliu);

        architect_description_Text = findViewById(R.id.architect_description_textafisat);
        architect_period_Text = findViewById(R.id.architect_period_textafisat);
        architect_source_Text = findViewById(R.id.architect_source_textafisat);

        architect_description_Text.setText(Html.fromHtml(stringDescription));
        architect_period_Text.setText(Html.fromHtml(stringPeriod));
        architect_source_Text.setText(Html.fromHtml(stringSource));

        architect_name_Text.setText(Html.fromHtml(stringMainWord));
        architect_name_Text.setSelected(true);

        toolbar.setTitle(stringMainWord);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        mDBHelper = new DatabaseHelperArchitects(this);



        findViewById(R.id.left_action_button_location).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //move to previous entry
                    //populate the intent with the data from the previous row of the database
                }
            });

    findViewById(R.id.right_action_button_location).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //move to next entry
                    //populate the intent with the data from the next row of the database
                }
            });

        try {
            InputStream is = assetManager.open("architects/" + stringPoza);
            b = BitmapFactory.decodeStream(is);
            viewPoza.setImageBitmap(b);

        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        }

一切正常,但我想在我的 ArchitectsDetailsActivity.java

上放两个按钮
findViewById(R.id.left_action_button_location).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //move to previous entry
                //populate the intent with the data from the previous row of the database
            }
        });

findViewById(R.id.right_action_button_location).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //move to next entry
                //populate the intent with the data from the next row of the database
            }
        });

我该怎么办?

0 个答案:

没有答案