简体   繁体   English

如何将值保存到Edittext?

[英]How to save values into Edittext?

I have created source and destination as a two edit-text in which i am placing the source and destination address from another activity but the first or second value is removed when coming back for placing second value.我已经将源和目标创建为两个编辑文本,我在其中放置来自另一个活动的源地址和目标地址,但是当返回放置第二个值时,第一个或第二个值被删除。 When first value is placed into first edit-text and going to place another value from another activity the first value is removed.当第一个值被放置到第一个编辑文本中并从另一个活动中放置另一个值时,第一个值被删除。

I have tried the Sharedpreferences but it is not working well any idea for this.我已经尝试过 Sharedpreferences ,但对此没有任何想法。

Here is my code.这是我的代码。

    Intent intent = getIntent();
    String source = intent.getStringExtra("place");
    search.setText(source);
    Intent intent1 = getIntent();
    String dest = intent1.getStringExtra("my1dest");
    desti.setText(dest);


    swap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String s1 = search.getText().toString();
            String s2 = desti.getText().toString();
            if (!s1.isEmpty() && !s2.isEmpty()) {
                desti.setText(s1);
                search.setText(s2);// this for swap the values source to destination viceversa



            }

        }
    });

and from this adpter class i am passing the values从这个适配器 class 我传递值

  Intent intent=new Intent(context,MainActivity.class);

            intent.putExtra("place",searchlist.getPlaces());
            intent.putExtra("dest",searchlist.getPlaces());
            intent.putExtra("lat",searchlist.getLat());
            intent.putExtra("lang",searchlist.getLang());
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_CLEAR_TASK);
            context.startActivity(intent);

please give idea for this concept thank you..请给出这个概念的想法谢谢..

see this image看到这张图片

If you are trying to save values that are passed from two different activities than you should have a mechanism to save the first value and then should get other value from another activity.如果您尝试保存从两个不同活动传递的值,那么您应该有一种机制来保存第一个值,然后应该从另一个活动中获取其他值。

The issue is that you are replacing the values with the same intent (intent from same activity):问题是您要替换具有相同意图的值(来自同一活动的意图):

Intent intent = getIntent();  //if this is intent from your MainActivity
String source = intent.getStringExtra("place");  //you will get this value
search.setText(source);
Intent intent1 = getIntent(); //and this as well is intent from same activity (MainActivity)
String dest = intent1.getStringExtra("my1dest"); //you will not get this value as this is not passed from MainActivity
desti.setText(dest);

The solution could be saving one value to SharedPreference or any other mean(db or network) and get other value from other intent: you should check if the value is null then don't replace it.解决方案可能是将一个值保存到 SharedPreference 或任何其他平均值(数据库或网络)并从其他意图获取其他值:您应该检查该值是否为 null 然后不要替换它。 Like:喜欢:

Intent intent = getIntent();  //if this is intent from your MainActivity
String source = intent.getStringExtra("place");  //you will get this value
if(source.isNotEmpty){
//save to preferences and set text 
search.setText(source);
}

Intent intent1 = getIntent(); //if this is intent from your otherActivity 
String dest = intent1.getStringExtra("my1dest"); 
if(dest.isNotEmpty){
//save to preferences and set text 
desti.setText(dest);
}

Here is the working code这是工作代码

Edittext source,destination;
    swap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String s1 = search.getText().toString();
            String s2 = desti.getText().toString();

            if (s1.contains(s1) && s2.contains(s2)) {
                desti.setText(s1);
                search.setText(s2);
            } else {
                desti.setText(s2);
                search.setText(s1);
            }


        }
    });//for setting values from one edittext to another


    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Intent intent = new Intent(MainActivity.this, Added.class);
            startActivityForResult(intent,1);
        }
    });

    desti.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this, Destinationsearch.class);
            startActivityForResult(intent,2);
        }
    });// here i have started Activity For result to my Adapter class

getting values from Adapter class从适配器 class 获取值

 @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == RESULT_OK && data != null)
    {
        String place=data.getStringExtra("place");
        search.setText(place);

    }

    if (requestCode==2 && resultCode==RESULT_OK && data!=null)
    {
        String dest=data.getStringExtra("my1dest");
        desti.setText(dest);
    }
}

and this is my Adpter class这是我的适配器 class

 @Override
public void onBindViewHolder(@NonNull SearchedAdapter.Myholder holder, int position) {
    Searchlist searchlist = list.get(position);

    holder.textView.setText(searchlist.getPlaces());

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(context, MainActivity.class);
            intent.putExtra("place", searchlist.getPlaces());
            intent.putExtra("my1dest", searchlist.getPlaces());
            intent.putExtra("lat", searchlist.getLat());
            intent.putExtra("lang", searchlist.getLang());
            ((Activity) context).setResult(Activity.RESULT_OK, intent);
            ((Activity) context).finish();
        }
    });

}

That set (:该集 (:

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

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