Ich habe eine Textansicht in meiner Adapterklasse und muss die Textansicht in Aktivität aktualisieren, da ich das Ergebnis in Aktivität erhalte. Wie kann ich das machen? Dies ist meine Aktivitätsklasse. Ich möchte textview tv2 mit "etwas Text" aktualisieren. Ich habe sogar versucht, die Textansicht im Adapter zu aktualisieren, aber das war mir nicht erfolgreich.

public class TextviewActivity extends AppCompatActivity {
ListView lvText;
ArrayList<TextviewPojo> textviewPojos = new ArrayList<>();
TextviewAdapter textviewAdapter;
TextView tv2;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_textview);

    lvText = findViewById(R.id.lvText);
    textviewAdapter = new TextviewAdapter(textviewPojos,this);
    lvText.setAdapter(textviewAdapter);
    textviewPojos.add(new TextviewPojo("first text","second text"));
    textviewPojos.add(new TextviewPojo("first text","second text"));


  }
}

Dies ist meine Adapterklasse.

public class TextviewAdapter extends BaseAdapter {
  ArrayList<TextviewPojo> textviewPojos = new ArrayList<>();
  Context context;

  public TextviewAdapter(ArrayList<TextviewPojo> textviewPojos, Context context) {
    this.textviewPojos = textviewPojos;
    this.context = context;
  }

  @Override
  public int getCount() {
    return textviewPojos.size();
  }

  @Override
  public Object getItem(int position) {
    return null;
  }

  @Override
  public long getItemId(int position) {
    return 0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null){
      convertView = LayoutInflater.from(context).inflate(R.layout.item_textview,parent,false);
    }
    TextView tv1,tv2;
    TextviewPojo textviewPojo = textviewPojos.get(position);
    tv1 = convertView.findViewById(R.id.tv1);
    tv2 = convertView.findViewById(R.id.tv2);

    tv1.setText(textviewPojo.getText1());
    tv2.setText(textviewPojo.getText2());
    return convertView;
  }
}

Unten ist mein Artikellayout. - item_textview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv1"
        android:textSize="30sp"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="filename1"/>
    <TextView
        android:id="@+id/tv2"
        android:layout_weight="1"
        android:textSize="30sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="filename2"/>
</LinearLayout>
1
Shriraksha bhat 24 Feb. 2020 im 08:33

3 Antworten

Beste Antwort

Versuchen Sie, diesen Code nicht erneut auf tvDocName in der ausgewählten Datei zu verweisen. Ändern Sie einfach diesen Code in der ausgewählten Datei

    public class DocumentActivity extends AppCompatActivity {

    ImageView toolbar_back;
    TextView next;
    RecyclerView recyclerView;
    ArrayList<Survey_vehiclepojo> mylist = new ArrayList();
    My_document_adapter adapter;
    TextView tvDocName;
    View view;
    private int position;
    String displayName = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_document);

        view = getLayoutInflater().inflate(R.layout.activity_document, null);
        tvDocName = findViewById(R.id.tvDocName);

//        toolbar_back=(ImageView)findViewById(R.id.toolbar_back);
//        toolbar_back.setOnClickListener(this);

//        next=(TextView)findViewById(R.id.next);
//        next.setOnClickListener(this);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        mylist.add(new Survey_vehiclepojo("Pay Slip"));
        mylist.add(new Survey_vehiclepojo("Insurance"));
        mylist.add(new Survey_vehiclepojo("NA Certificate"));
        adapter = new My_document_adapter(DocumentActivity.this, mylist);
        recyclerView.setAdapter(adapter);
    }

    /*  @Override
      public void onClick(View v) {
          switch (v.getId()){
              *//*case R.id.toolbar_back:
                finish();
                break;
            case R.id.next:
                Intent n=new Intent(DocumentActivity.this,Loan_checklistActivity.class);
                startActivity(n);
                break;*//*
        }
    }*/
    public void Document(int pos) {
        position = pos;
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        startActivityForResult(intent, 100);
        tvDocName.setText(mylist.get(pos).getPay_slip());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        selectedFile(data);
    }

    private void selectedFile(Intent data) {
        if (data != null) {
            Uri uri = data.getData();
            String uriString = null;

            if (uri != null) {
                uriString = uri.toString();
            }
            File myFile = new File(uriString);
            String path = myFile.getAbsolutePath();

            if (uriString != null) {
                if (uriString.startsWith("content://")) {
                    try (Cursor cursor = DocumentActivity.this.getContentResolver().query(uri, null, null, null, null)) {
                        if (cursor != null && cursor.moveToFirst()) {
                            displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                            Toast.makeText(this, "display" + displayName, Toast.LENGTH_SHORT).show();
                            //                    Toast.makeText(this, "path is" +path, Toast.LENGTH_SHORT).show();
                            //   My_document_adapter.Filename(displayName, position);
//                            ViewGroup v = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_document, null);
//                            tvDocName = v.findViewById(R.id.tvDocName);
                            tvDocName.setText(displayName);
                            adapter.notifyDataSetChanged();
                        }
                    }
                }
            } else if (uriString.startsWith("file://")) {
                displayName = myFile.getName();
                Toast.makeText(this, "display start " + displayName, Toast.LENGTH_SHORT).show();
             //   ViewGroup v = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_document, null);
            //    tvDocName = v.findViewById(R.id.tvDocName);
                tvDocName.setText(displayName);
                adapter.notifyDataSetChanged();
            }
        }
    }
}
0
Silversky Technology 24 Feb. 2020 im 06:17

Rufen Sie dies an, wo Sie aktualisieren und über Ihren Adapter abhören

public void refresh(ArrayList<TextviewPojos> textviewPojos){
this.textviewPojo=textviewPojos;
notifyDataSetChanged();
}
0
Amit pandey 24 Feb. 2020 im 07:31

Ändern Sie in Ihrer Adapterklasse diese Zeile

View v = LayoutInflater.from(context).inflate(R.layout.item_document, viewGroup, false);
0
Durai 24 Feb. 2020 im 05:44