在我的代码中,当按下按钮时,我需要将两个编辑文本字段和一个图像值传递给另一个活动的列表视图。我尝试了很多教程,但我没有找到适合我要求的教程。谁能帮忙我?
问问题
1288 次
3 回答
0
在您的第一个活动中:
Button btn = (Button) findViewById(R.id.btn);
EditText et1 = (EditText) findViewById(R.id.et1);
EditText et2 = (EditText) findViewById(R.id.et2);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(First_activity.this, Second_activity.class);
intent.putExtra("ET1", et1.getText().toString());
intent.putExtra("ET2", et2.getText().toString());
intent.putExtra("Image_URL", "your_image_url");
startActivity(intent);
}
});
在您的第二个活动中:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(Your_Layout);
Intent intent = getIntent();
String first = intent.getStringExtra("ET1");
String second = intent.getStringExtra("ET2");
String url = intent.getStringExtra("Image_URL");
// Now you can set this values to your Listview.
}
于 2013-10-17T06:11:31.223 回答
0
试试这个,使用 puExtra 将值从一个活动发送到另一个活动
Intent i = new Intent(currentActivity.this, NextActivity.class);
i.putExtra("editvalue1", editbox1.getText().toString());
i.putExtra("editvalue2", editbox2.getText().toString());
i.putExtra("image_url", "your image url");
startActivity(i);
将此值获取到下一个活动
Intent i = getIntent();
String edit1 = i.getStringExtra("editvalue1");
String edit2 = i.getStringExtra("editvalue2");
String url = i.getStringExtra("image_url");
于 2013-10-17T05:52:42.160 回答
0
头等舱
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);
}
});
然后在二等
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);
}
现在创建您自己的列表适配器
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;
}
}
现在创建列表视图布局
<?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>
我希望它会有所帮助
于 2017-11-06T09:13:43.513 回答