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

时间:2015-05-30 00:29:34

标签: android

您好我有两个EditText值,并希望将其传递给另一个活动中的自定义ListView。请帮我怎么做

2 个答案:

答案 0 :(得分:0)

要将值从一个Activity传递到另一个Activity,请将Intent与Bundle一起使用。 Bundle是包含要在键/值存储中设置的一组对象的对象(我假设由HashMap支持)。在您的情况下,您希望传递两个EditText值(字符串)。因此,您将使用Bundle通过Intent传递给其他活动:

    //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");

答案 1 :(得分:0)

头等舱

Square1

然后在第二课

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);
            }
        });

@覆盖     protected void onCreate(Bundle savedInstanceState)
    {         super.onCreate(savedInstanceState);         的setContentView(activity_second);          Intent intent = getIntent();          String text1 = intent.getStringExtra(“text1”);          String text2 = intent.getStringExtra(“text2”);

ListView List;

现在创建自己的listadapter

    List=(ListView) findViewById(R.id.ListView);//your listview id
    ListAdapter ListAdapter=new ListAdapter(this,text1,text2);
    List.setAdapter(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;
        }

    }

我希望它有所帮助