1.MainActivity.java
public class MainActivity extends AppCompatActivity{
ListView l1;
String[] t1={"TITLE 1","TITLE 2"};
String[] d1={"Detail 1","Detail 2"};
String[] s1={"Content 1", "Content 2"};
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.title);
l1=(ListView)findViewById(R.id.list);
l1.setAdapter(new dataListAdapter(t1, d1, s1));
}
class dataListAdapter extends BaseAdapter {
String[] Title, Detail, Content;
dataListAdapter() {
Title = null;
Detail = null;
Content=null;
}
public dataListAdapter(String[] text, String[] text1, String[] text3) {
Title = text;
Detail = text1;
Content = text3;
}
public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.custom, parent, false);
final TextView title, detail, detail2;
title = (TextView) row.findViewById(R.id.title);
detail = (TextView) row.findViewById(R.id.detail);
detail2 = (TextView) row.findViewById(R.id.detail2);
title.setText(Title[position]);
detail.setText(Detail[position]);
detail2.setText(Content[position]);
l1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selected = title.getText().toString();
Intent i = new Intent(getApplicationContext(), Main2Activity.class);
i.putExtra("name", selected);
startActivity(i);
}
});
return (row);
}
}
}
2.Main2Activity.java
公共类Main2Activity扩展了AppCompatActivity {
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
editText = (EditText)findViewById(R.id.et);
Intent in = getIntent();
String name = in.getStringExtra(("name"));
editText.setText(name);
}
}
答案 0 :(得分:1)
您应该在行
之后的MainActivity
中声明此方法
l1=(ListView)findViewById(R.id.list);
l1.setAdapter(new dataListAdapter(t1, d1, s1));
l1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selected = t1[position];
Intent i = new Intent(getApplicationContext(), Main2Activity.class);
i.putExtra("name", selected);
startActivity(i);
}
});
并从dataListAdapter
类
在另一项活动中,使用以下方法获取标题
Bundle extras = getIntent().getExtras();
String name = extras.getString("name");
editText.setText(name);
希望它能运作。
答案 1 :(得分:0)
在第一个活动中,您应该为这样的意图添加额外的参数。存储&#34;论证&#34;也很好。 MainActivity中的字符串为常量,并始终在其他位置引用它。
public static final String ARG_FROM_MAIN = "argument";
//我假设B.class是你的第二个活动
Intent intent = new Intent(MainActivity.this, B.class);
intent.putExtra("argument", "Title1");
startActivity(intent);
然后在 B 活动中,您可以检索这样的参数:
String passedArg = getIntent().getExtras().getString("argument");
enteredValue.setText(passedArg);