0

I Am working on project where users recognizes text(OCR) and then see all their text(OCR) history in another activity

App Link : https://play.google.com/store/apps/details?id=com.thetechroot.vision

I added shared preference and recycle view,

but i am only getting the first Text that was recognized

Working :- Users Recognize Text(OCR) in Textactivity, then to view users history of scanned text(OCR) the history is shown in different activity


How Can I Saved Textview From One Activity to diff activity, and show it into recycleview using shared preference


TEXTACTIVTY.JAVA

 textRecognizer.processImage(image)
                .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                    @Override
                    public void onSuccess(final FirebaseVisionText firebaseVisionText) {


                        translatelan(firebaseVisionText);



                        cd_text_re.setVisibility(View.VISIBLE);
                        spinnerlan.setVisibility(View.VISIBLE);
                         txtrecog.setText(firebaseVisionText.getText());


                        String th = SharedCommon.getSharedPreferencesString(getApplicationContext(), texthistory,firebaseVisionText.getText());


                        //int i = SharedCommon.getPreferencesInt(getApplicationContext(), key1,50);


                        final SharedCommon scth = new SharedCommon();

                        if (txtrecog.equals("")){

                            Toast.makeText(TextActivity.this, "Text: "+th, Toast.LENGTH_SHORT).show();

                        }

                        else {

                            Toast.makeText(TextActivity.this, "Text: "+th, Toast.LENGTH_SHORT).show();


                            scth.putSharedPreferencesString(getApplicationContext(), SharedCommon.texthistory, th);

                        }
                      /*  SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
                        editor.putString("name", String.valueOf(txtrecog.getText()));
                        editor.putInt("idName", 1);
                        editor.apply();*/

                        drawtextvision(firebaseVisionText);
                    }
                }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<String> values;

    ArrayList personNames;
    Context context;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public class ViewHolder extends RecyclerView.ViewHolder {


        // each data item is just a string in this case
        public TextView txtHeader;
        public TextView txtFooter;
        public View layout;


        public ViewHolder(View v) {
            super(v);
            layout = v;
            txtHeader = (TextView) v.findViewById(R.id.firstLine);
            txtFooter = (TextView) v.findViewById(R.id.secondLine);
        }
    }

    public void add(int position, String item) {
        values.add(position, item);
        notifyItemInserted(position);
    }

    public void remove(int position) {
        values.remove(position);
        notifyItemRemoved(position);
    }

    public MyAdapter(Context context, ArrayList personNames) {
        this.context = context;
        this.personNames = personNames;
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(List<String> myDataset) {
        values = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view

        /*final String th = SharedCommon.getSharedPreferencesString(getApplicationContext(), texthistory,"");
*/
        LayoutInflater inflater = LayoutInflater.from(
                parent.getContext());
        View v =
                inflater.inflate(R.layout.layout_history_text, parent, false);
        // set the view's size, margins, paddings and layout parameters
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element



        final String name = values.get(position);
        holder.txtHeader.setText(name);
        holder.txtHeader.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                holder.txtFooter.setText("Footer: " + name);
            }
        });

        //holder.txtFooter.setText("Footer: " + name);
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return values.size();
    }
    
    }

HISTORYACTIVITY.JAVA

public class AboutActivity extends AppCompatActivity {

   /* WebView webView;*/

    ProgressDialog mprogreeinternet;

    String apppackagename = "com.thetechroot.vision";

    int versionCode = BuildConfig.VERSION_CODE;
    String versionName = BuildConfig.VERSION_NAME;

    String appid = BuildConfig.APPLICATION_ID;


    Button btnlimit;
    WebView webview;

   /* private RecyclerView recyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager layoutManager;*/

    private RecyclerView recyclerView;
    private MyAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;


    LinearLayout layouthide,layoutcredit;

    int[] photos={R.drawable.logoam, R.drawable.iconshandwrit52,R.drawable.productsearch52,R.drawable.iconsqrcode52};


    ImageButton arrdown,arrup,arrcre,arrcreup;

    TextView txthistory;


    TextView mItemDescription;
    ImageButton mDescriptionImg,mupImg;

    CardView cdhistory;


    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        /* btnlimit = (Button) findViewById(R.id.btnlimit);*/

        final String th = SharedCommon.getSharedPreferencesString(getApplicationContext(), texthistory,"");

       // Toast.makeText(this, ""+th, Toast.LENGTH_SHORT).show();
        recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        // recyclerView.setHasFixedSize(true);

        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(mLayoutManager);
        // Use the default animator
        // recyclerView.setItemAnimator(new DefaultItemAnimator());
        // you could add item decorators
        // RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
        // recyclerView.addItemDecoration(itemDecoration);

        ArrayList<String> values = new ArrayList<String>();
        /*for (int i = 0; i < 100; i++) {
            values.add("Test" + i);
        }*/

        Toast.makeText(this, ""+String.valueOf(th), Toast.LENGTH_SHORT).show();
        values.add(""+String.valueOf(th));

        // specify an adapter (see also next example)
        mAdapter = new MyAdapter(values);
        recyclerView.setAdapter(mAdapter);

        ItemTouchHelper.SimpleCallback simpleItemTouchCallback =
                new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
                    @Override
                    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder
                            target) {
                        return false;
                    }
                    @Override
                    public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
                       // input.remove(viewHolder.getAdapterPosition());
                        mAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
                    }
                };
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
        itemTouchHelper.attachToRecyclerView(recyclerView);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

        Intent startIntent = new Intent(AboutActivity.this, TextActivity.class);
        startActivity(startIntent);
        finish();
    }
}
Shahriyar Aghajani
  • 431
  • 1
  • 6
  • 22
Harshil Patel
  • 127
  • 10

2 Answers2

0

It's a position issue, you have to use setTag() and getTag(), check this

final String name = values.get(position);
**holder.txtFooter.setTag(name);**
    holder.txtHeader.setText(name);
    holder.txtHeader.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

        **holder.txtFooter.setText("Footer: " + v.getTag());**
    }
});
Adil
  • 812
  • 1
  • 9
  • 29
0

Here TEXTACTIVTY.JAVA you are using single string, so instead of appending it to previous strings in shared preference you are replacing the history. Ideally you should save Strings array and retrieve the same. Currently values have size one because it has only one string. First use Sting Array. To save String Array in shared preferences do the following

StringBuilder sb = new StringBuilder();
for (int i = 0; i < playlists.length; i++) {
    sb.append(playlists[i]).append(",");
}
prefsEditor.putString(PLAYLISTS, sb.toString());

Then when you get the String from SharedPreferences simply parse it like this:

String[] playlists = playlist.split(",");

Refer Put and get String array from shared preferences

And https://blog.fossasia.org/storing-a-data-list-in-phimpme-android/ for more.

TheAnkush
  • 885
  • 7
  • 10