简体   繁体   English

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

[英]Not Able to get Text In recycle view

I Am working on project where users recognizes text(OCR) and then see all their text(OCR) history in another activity 我正在研究一个项目,用户可以识别文本(OCR),然后在另一个活动中查看其所有文本(OCR)历史记录

App Link : https://play.google.com/store/apps/details?id=com.thetechroot.vision 应用链接: 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 工作:-用户在文本活动中识别文本(OCR),然后查看用户的扫描文本(OCR)历史记录,该历史记录显示在不同的活动中


How Can I Saved Textview From One Activity to diff activity, and show it into recycleview using shared preference 如何将Textview从一个活动保存到diff活动,并使用共享首选项将其显示到recycleview中


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

It's a position issue, you have to use setTag( ) and getTag() , check this 这是position问题,您必须使用setTag( )和getTag() ,检查一下

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

Here TEXTACTIVTY.JAVA you are using single string, so instead of appending it to previous strings in shared preference you are replacing the history. 在TEXTACTIVTY.JAVA中,您使用的是单个字符串,因此,不是在共享首选项中将其附加到以前的字符串,而是替换了历史记录。 Ideally you should save Strings array and retrieve the same. 理想情况下,您应该保存Strings数组并检索相同的数组。 Currently values have size one because it has only one string. 当前值的大小为1,因为它只有一个字符串。 First use Sting Array. 首先使用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: 然后,当您从SharedPreferences获取字符串时,只需像下面这样解析它:

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

Refer Put and get String array from shared preferences 从共享首选项中引用Put和获取String数组

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM