无法在回收视图中获取文本

时间:2018-11-29 06:28:45

标签: java android android-recyclerview

我正在研究一个项目,用户可以识别文本(OCR),然后在另一个活动中查看其所有文本(OCR)历史记录

应用链接:https://play.google.com/store/apps/details?id=com.thetechroot.vision

我添加了共享首选项和回收视图,

但是我只得到第一个被识别的文本

工作:- 用户在Textactivity中识别文本(OCR),然后要查看用户的扫描文本(OCR)历史,该历史将显示在不同的活动中


如何将Textview从一个活动保存到diff活动,并使用共享的首选项将其显示到recycleview中


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();
    }
}

2 个答案:

答案 0 :(得分:0)

这是一个position问题,您必须使用setTag()和getTag(),选中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());**
    }
});

答案 1 :(得分:0)

此处TEXTACTIVTY.JAVA使用的是单个字符串,因此,您不是在共享首选项中将其附加到以前的字符串中,而是替换了历史记录。理想情况下,您应该保存Strings数组并检索相同的数组。当前值的大小为1,因为它只有一个字符串。 首先使用Sting Array。要将字符串数组保存在共享首选项中,请执行以下操作

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

然后,当您从SharedPreferences获取字符串时,只需像这样解析它:

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

引用Put and get String array from shared preferences

还有https://blog.fossasia.org/storing-a-data-list-in-phimpme-android/