如何在edittext中显示listview中的数据而无需重新加载页面

时间:2017-11-18 11:46:30

标签: android listview

例如,我有两个班级。 Android项目中的A类和B类。

在A类中,我有Edittext,点击后会打开B类,其中有listview。

单击listview项时,它将返回到A类并从A类填充edittext数据。无需重新加载页面。

请帮帮我......

B类

  list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final String kdx = itemList.get(position).getNameAkun();

            Intent i = new Intent(getApplicationContext(), alurkastfadds.class);
            i.putExtra("akun", kdx);
            startActivity(i);

        }
    });

A类

Intent i2 = getIntent();
    String data = i2.getStringExtra("akun");
    txttotalTransaksitf.setText(data);

但没有重新加载页面

2 个答案:

答案 0 :(得分:0)

从A类编辑文本点击事件调用此方法

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

从B类 listview 项目中点击调用此方法

  list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          final String kdx = itemList.get(position).getNameAkun();

          Intent returnIntent = new Intent();
          returnIntent.putExtra("akun", kdx);
          setResult(Activity.RESULT_OK,returnIntent);
          finish();

    }
});

然后在Activity A中实现 onActivityResult()覆盖方法并调用此方法。

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 1) {
    if(resultCode == Activity.RESULT_OK){
        String result=data.getStringExtra("akun");
        txttotalTransaksitf.setText(data);
    }

}

}

答案 1 :(得分:0)

您必须使用StartActivityForResult。 这是你的第一个活动。

 public class FirstActivity  extends AppCompatActivity implements View.OnClickListener {
private static EditText inputMessage;
private static Button next;
public static final int IntentId =1; //Intent Request Code
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    inputMessage = (EditText)findViewById(R.id.inputMessage);//EditText

    next = (Button)findViewById(R.id.next);//Click button to go next activity


     next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Open second activity with request code
            Intent in = new Intent(FourthLActivity.this, ThirdActivity.class);
            startActivityForResult(in, IntentId);
        }
    });

}

//onActivityResult get result(listItem) from second activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // check if the request code is same as what is passed  here it is IntentId
    if (requestCode == IntentId) {
        if (resultCode == RESULT_OK) {

            //If result code is OK then get String extra and set message
            String listItem = data.getStringExtra("listItem");
            inputMessage.setText(listItem);


        }


}



}

这是你的第二个活动。

public class SecondActivity extends AppCompatActivity {

ArrayList<String> list=new ArrayList<>();
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fourth_activity);


    list.add("India");
    list.add("America");
    list.add("England");

    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_expandable_list_item_1, list);



    ListView listView = (ListView) findViewById(R.id.mobile_list);

   listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String message=list.get(position);

                Intent intent = new Intent();
                intent.putExtra("listItem",message);//Put Message to pass over intent
                setResult(RESULT_OK,intent);//Set result OK
                finish();

        }
    });



}


}