如何在片段中更新列表视图并控制数据库呢?

时间:2018-08-02 07:09:52

标签: android-studio listview android-fragments android-cardview

我在列表视图和片段中需要帮助。 我的项目有点像“粘滞便笺”,它包含ID,标题和描述。 在花费了很多时间之后,我能够根据需要正确列出所有注释,但是现在的问题是我无法更新列表视图。

即,如果用户单击其中一个便笺,则必须在卡片视图中打开该便笺,用户才能编辑便笺或将其删除。

如果用户选择删除注释,则必须将其从列表视图中删除。如果用户更改了该注释的标题,则必须也必须在列表视图中反映出相同的含义。

我正在使用SQLIte记录笔记,因此我可以毫无问题地更新数据库,但是列表视图根本没有更新。

对我来说,这是我完全困惑于更新列表视图的问题。

MainActivity

ISNUMBERIC

MyItemRecyclerViewAdapter

  SELECT
  DISTINCT
  S.FORM_NO,
  S.ARTICLEID_FK,
  S.BOX_SERIAL_NO,
  S.ACTUAL_WEIGHT,
  TO_CHAR(S.DATEADDED, 'DD-MON-YYYY HH24:MI:SS') AS DATEADDEDS,
  S.ADDEDBY,
  B.BOX_SERIAL_NO,
  B.ARTICLEID_FK,
  B.DATE_CODE,
  B.SO_NO
  FROM WA_LA_TBL_ARTICLES_SCAN S 
  JOIN WA_LA_TBL_ARTICLES_BOX_SN B ON S.BOX_SERIAL_NO = B.BOX_SERIAL_NO AND
  TO_CHAR(S.DATEADDED, 'YYYY-MM-DD HH24:MI') BETWEEN '2018-08-01 08:00' AND '2018-08-02 07:59'

  JOIN  WA_GA_TBL_ARTICLES C ON C.ARTICLEID = B.ARTICLEID_FK 

  ORDER BY S.ARTICLEID_FK, B.BOX_SERIAL_NO ASC

ItemFragment

package com.example.blackhand.myapplication;

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.example.blackhand.myapplication.Database.Notes_DataBase;
import com.example.blackhand.myapplication.dummy.DummyContent;

import java.io.IOException;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, ItemFragment.OnListFragmentInteractionListener {

    Notes_DataBase Record_Notes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Record_Notes = new Notes_DataBase(this, getFilesDir().getAbsolutePath());
        try {
            Record_Notes.prepareDataBase();
        } catch (IOException io) {
            throw new Error("Unable to create Database");
        }
/*
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
*/
        Cursor ID_NC = Record_Notes.Get_Notes_N();
        if (ID_NC.getCount() > 0) {

            util.Assignment_note = true;
            for (int i = 0; i < ID_NC.getCount(); i++) {
                ID_NC.moveToNext();
                DummyContent.addItem(DummyContent.createDummyItem(ID_NC.getString(0), ID_NC.getString(0) + "*" + ID_NC.getString(1), ID_NC.getString(2)));
            }
        } else
            util.Assignment_note = false;

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    public void onListFragmentInteraction(DummyContent.DummyItem item){
        //you can leave it empty
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        displaySelectedScreen(item.getItemId());
        return true;
    }

    private void displaySelectedScreen(int itemId) {

        Fragment fragment = null;

        switch (itemId) {

            //This doesn't matter to open fragment, I set my fragment on camera icon is just for testing purpose
            case R.id.nav_camera:
                fragment = new ItemFragment();
                break;
            case R.id.nav_gallery:

                break;
            case R.id.nav_manage:

                break;
            case R.id.nav_send:

                break;
            case R.id.nav_share:

                break;
            case R.id.nav_view:

                break;
        }

        if (fragment != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_frame, fragment);
            ft.commit();
        }

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
    }
}

DummyContent

package com.example.blackhand.myapplication;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.blackhand.myapplication.ItemFragment.OnListFragmentInteractionListener;
import com.example.blackhand.myapplication.dummy.DummyContent.DummyItem;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Random;

/**
 * {@link RecyclerView.Adapter} that can display a {@link DummyItem} and makes a call to the
 * specified {@link OnListFragmentInteractionListener}.
 * TODO: Replace the implementation with code for your data type.
 */
public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> {

    private final List<DummyItem> mValues;
    private final OnListFragmentInteractionListener mListener;
    private final Context contex;
    private final String[] Back;

    int Random_no;
    public MyItemRecyclerViewAdapter(Context cont, String[] Background, List<DummyItem> items, OnListFragmentInteractionListener listener) {
        mValues = items;
        mListener = listener;
        contex = cont;
        Back = Background;
    }

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

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);

        Random generator = new Random();
        Random_no = generator.nextInt(12);

        AssetManager assetManager = contex.getAssets();
        try {
            InputStream istr = assetManager.open("shape/" + Back[Random_no]);
            Bitmap bitmap = BitmapFactory.decodeStream(istr);
            holder.mImgView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String First_Char = mValues.get(position).title_.substring(mValues.get(position).title_.indexOf("*") + 1, mValues.get(position).title_.indexOf("*") + 2);
        holder.mIdView.setText(First_Char);

        String Title_first = mValues.get(position).title_.substring(mValues.get(position).title_.indexOf("*") + 1, mValues.get(position).title_.indexOf("."));
        holder.mTitleView.setText(Title_first);

        if(mValues.get(position).details_.length() > 15)
            holder.mContentView.setText(mValues.get(position).details_.substring(0, 15));
        else
            holder.mContentView.setText(mValues.get(position).details_);

        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    // Notify the active callbacks interface (the activity, if the
                    // fragment is attached to one) that an item has been selected.
                    mListener.onListFragmentInteraction(holder.mItem);

                    String D = holder.mItem.details_;
                    String T = holder.mItem.title_;
                }
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView mIdView;
        public final TextView mTitleView;
        public final TextView mContentView;
        public final ImageView mImgView;
        public DummyItem mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mImgView = view.findViewById(R.id.img);
            mTitleView = view.findViewById(R.id.txt1);
            mIdView = view.findViewById(R.id.txt3);
            mContentView = view.findViewById(R.id.txt2);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mContentView.getText() + "'";
        }
    }
}

MyItemRecyclerViewAdapter 文件中,OnClickItem执行了 mListener.onListFragmentInteraction(holder.mItem); ,一旦单击此处,如何打开卡片视图。还是有其他方法可以在片段或活动中实现?

尚未在代码中实现卡片视图。 enter image description here

请帮助我,以便我编辑或删除注释和更新列表视图。

我希望我也很好地解释了我的问题,也希望在详细解答中也是如此。

谢谢。

0 个答案:

没有答案