Hi I have two EditText
values and want to pass them to customized ListView
in another activity. Please help me how to do that
To pass values from one Activity to another, use Intent with a 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). In your case, you want to pass two EditText values (Strings). So you are going to use a Bundle passed via Intent to another activity:
//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)
{ super.onCreate(savedInstanceState); setContentView(activity_second); Intent intent = getIntent(); String text1 = intent.getStringExtra("text1"); String 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.