来自另一个布局的findViewById()不更新数据

时间:2018-10-27 16:30:07

标签: android layout findviewbyid

我有mainActivity,它的xml包含另一个xml。这两个activitymain布局都有一个按钮,而随附的布局有另一个按钮。两者都应显示在主要活动中。 现在,我需要更改两个按钮的文本。我可以通过findviewByID获得对第一个按钮的引用,并且它可以正常工作。对于进入第二布局的第二个按钮,我通过扩大布局来获取视图,它正确地引用了该视图,并且findviewbyID获得了对此按钮的引用。但是当更改文本时,它不会更改。

activityMain.xml

var populateTable = function(tableData){
     //whatever you do to populate your table
}

$(document).ready(function(){
     $.get("www.your.site.com/getData", function(data){
          populateTable(data);
     })
})

const submitEdits = function(){
     $.ajax({
          method: "POST", //Or whichever method you use
          url: "www.your.site.com/sendData",
          data: <your data here>
     })
     .done(function( response ) {
         populateTable(response.data);
     });
}

layouttest.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
    android:orientation="vertical"
        xmlns:tools="http://schemas.android.com/tools">
<Button android:id="@+id/buttonvvv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    xmlns:android="http://schemas.android.com/apk/res/android" />
  <include
            layout="@layout/layouttest"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</LinearLayout>

结果

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/buttonaaa"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />
</LinearLayout>
---Mainactivity.java----------------------------------
 import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  View view=LayoutInflater.from(getApplication()).inflate(R.layout.layouttest, null);

        Button b =(Button)  findViewById(R.id.buttonvvv);
        Button bb =(Button) view.findViewById(R.id.buttonaaa);
         b.setText("ssssssss");
         bb.setText("ssssssss");

    }

2 个答案:

答案 0 :(得分:1)

无需夸大包含的布局,您可以像使用findViewById一样b button它已经包含在activity_main.xml布局中

替换此:

    View view=LayoutInflater.from(getApplication()).inflate(R.layout.layouttest, null);
    Button b =(Button)  findViewById(R.id.buttonvvv);
    Button bb =(Button) view.findViewById(R.id.buttonaaa);
    b.setText("ssssssss");
    bb.setText("ssssssss");

使用:

    Button b =(Button)  findViewById(R.id.buttonvvv);
    Button bb =(Button) findViewById(R.id.buttonaaa);
    b.setText("ssssssss");
    bb.setText("ssssssss");

来自文档include

  

根视图应该恰好是您希望其出现在每个视图中的方式   要向其中添加此布局的布局。

顺便说一句,,选中此answer

答案 1 :(得分:0)

您不必夸大布局,只需找到它即可:

LinearLayout layout =(LinearLayout)  findViewById(R.id.layouttest);

然后

Button bb =(Button) layout.findViewById(R.id.buttonaaa);