简体   繁体   English

如何在Android中的另一个活动中将两个edittext值传递给listview?

[英]How to pass two edittext values to listview in another activity in android?

Hi I have two EditText values and want to pass them to customized ListView in another activity. 嗨,我有两个EditText值,并希望在另一个活动中将它们传递给自定义的ListView Please help me how to do that 请帮我怎么做

To pass values from one Activity to another, use Intent with a Bundle. 要将值从一个Activity传递到另一个Activity,请使用Intent with Bundle。 A Bundle is object containing a set of objects your are to set in a key/value store (I assume is backed by a HashMap). Bundle是一个对象,其中包含要在键/值存储中设置的一组对象(我假设它由HashMap支持)。 In your case, you want to pass two EditText values (Strings). 您要传递两个EditText值(字符串)。 So you are going to use a Bundle passed via Intent to another activity: 因此,您将使用通过Intent传递给另一个活动的Bundle

    //In first Activity that isn't the ListView Activity
    Intent intent = new Intent(this, YourListActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("stringOne", editTextOne.getText().toString());
    bundle.putString("stringTwo", editTextTwo.getText().toString());
    intent.putExtras(bundle);
    startActivity(intent);

    //Now in your List Activity's onCreate() method you can do the following
    Bundle extras;
    if(getIntent().getExtras() != null){
        extras = getIntent().getExtras();
    }

    //Now, anywhere after in your List Activity you can access
    //the EditText strings from first Activity

    // (Somewhere later in your code, or just in your onCreate method)
    String first = extras.getString("stringOne");
    String second = extras.getString("stringTwo");

at first class 头等舱

Button button = (Button) findViewById(R.id.button);
    EditText editT1 = (EditText) findViewById(R.id.editT1);
    EditText editT2 = (EditText) findViewById(R.id.editT2);
button.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                Intent intent = new Intent(First_activity.this,Second_activity.class);
                intent.putExtra("text1", et1.getText().toString());
                intent.putExtra("text2", et2.getText().toString());
                intent.putExtra("Image_URL", "your_image_url");
                startActivity(intent);
            }
        });

then in second class 然后在二等舱

ListView List;

@Override protected void onCreate(Bundle savedInstanceState) @Override受保护的void onCreate(捆绑的saveInstanceState)
{ super.onCreate(savedInstanceState); {super.onCreate(savedInstanceState); setContentView(activity_second); setContentView(activity_second); Intent intent = getIntent(); Intent intent = getIntent(); String text1 = intent.getStringExtra("text1"); 字符串text1 = intent.getStringExtra(“ text1”); String text2 = intent.getStringExtra("text2"); 字符串text2 = intent.getStringExtra(“ text2”);

    List=(ListView) findViewById(R.id.ListView);//your listview id
    ListAdapter ListAdapter=new ListAdapter(this,text1,text2);
    List.setAdapter(ListAdapter);

}

Now create your own listadapter 现在创建自己的列表适配器

  import android.app.Activity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;

    public class ListAdapter extends BaseAdapter
    {
        Activity context;
        String title[];
        String description[];

        public ListAdapter (Activity context, String[] title, String[] description) {
            super();
            this.context = context;
            this.title = title;
            this.description = description;
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return title.length;
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        private class ViewHolder {
            TextView txtViewTitle;
            TextView txtViewDescription;
        }

        public View getView(int position, View convertView, ViewGroup parent)
        {
            // TODO Auto-generated method stub
            ViewHolder holder;
            LayoutInflater inflater =  context.getLayoutInflater();

            if (convertView == null)
            {
                convertView = inflater.inflate(R.layout.dital_list_view, null);//your listview layout
                holder = new ViewHolder();
                holder.txtViewTitle = (TextView) convertView.findViewById(R.id.titleText);//forst textview id
                holder.txtViewDescription = (TextView) convertView.findViewById(R.id.descriptionText);
                convertView.setTag(holder);//second text view id
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txtViewTitle.setText(title[position]);
            holder.txtViewDescription.setText(description[position]);

            return convertView;
        }

    }

now create listview layout 现在创建列表视图布局

 <?xml version="1.0" encoding="UTF-8"?>

    -<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
 android:layout_height="wrap_content" 
android:layout_width="match_parent" 
android:id="@+id/titleText"/>

    <TextView 
android:layout_height="wrap_content"
 android:layout_width="match_parent" 
android:id="@+id/descriptionText" />

    </LinearLayout>

i hope it be helpful 我希望这会有所帮助

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

相关问题 将 ListView 值传递给另一个 Activity 中的多个 EditText - Pass ListView values to multiple EditText in another Activity 将ListView中的EditText值传递给另一个Activity - Pass EditText value from ListView to another Activity Android SQlite,如何将两个值传递给另一个活动? - Android SQlite, How do you pass two values to another activity? 如何将列表视图中的单击项转移到Android中另一个活动中的editText - How to transfer the clicked item in listview to editText in another activity in android 如何将 EditText 传递给另一个活动? - How to pass EditText to another activity? 如何将多个edittext值传递到Arraylist中,然后将其发送到另一个活动? - How to pass multiple edittext values into a Arraylist and then send it to another activity? 如何将EditText值从RecyclerView适配器传递到Android中的活动 - How to pass edittext values from recyclerview adapter to activity in android android ListView将隐藏的值从一个活动传递给另一个 - android ListView pass hidden values from one Activity to another 如何将多个数据从 EditText(Main Activity) 传递到 ListView(Another Activity) - How to pass multiple data from EditText(Main Activity) to ListView(Another Activity ) android在另一个活动的edittext中传递用户名和密码 - android pass the username and password in edittext of another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM