Как организовать последовательную передачу данных между тремя активностями?
задал [пользователь удален] 1 месяц назад
Здравствуйте!
Столкнулся с проблемой.
Есть потребность в справочном приложении, которое будет выглядеть следующим образом:
Из первой активности (ListView) через intent происходит передача картинки во вторую активность в зависимости от выбранного элемента.
Во второй активности необходимо перейти к комментарию в виде текста в третьей активности. Вывод через нажатие иконки в Action Bar.
Подскажите, пожалуйста, как можно реализовать передачу текста в третью активность в строгой зависимости от первого выбора.
Первая активность.
``public class FirstActivity extends ListActivity {
/** Called when the activity is first created. */
public TextView Text;
public ArrayList<ListData> catalog;
String[] names;
String[] descriptionsAlg;
int img = R.drawable.ic_item_alg;
int[] png = { R.drawable.opisanie, R.drawable.predosterezenie };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
names = new String[2];
names[0] = getResources().getString(R.string.str_o);
names[1] = getResources().getString(R.string.str_p);
descriptionsAlg = new String[2];
descriptionsAlg[0] = getResources().getString(R.string.txt_o);
descriptionsAlg[1] = getResources().getString(R.string.txt_p);
// Создаем массив объектов ListData и заполняем их данными
catalog = new ArrayList<ListData>();
for (int i = 1; i <= 2; i++) {
catalog.add(new ListData(names[i - 1], png[i - 1], img,
// Создаем адаптер данных
CatalogAdapter catAdapter;
catAdapter = new CatalogAdapter(this, catalog);
setListAdapter(catAdapter);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Проверяем ориентацию экрана
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
}
}
public void onListItemClick(ListView parent, View v, int position, long id) {
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(SecondActivity.PICTURE, png[position]);
intent.putExtra(ThirdActivity.DESCRIPTIONSALG,
}
public TextView getText() {
return Text;
}
public void setText(TextView Text) {
this.Text = Text;
}
``
Вторая активность.
``public class SecondActivity extends Activity implements OnClickListener {
public static final String PICTURE = "picture";
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// Получаем данные от деятельности FirstActivity
Intent intent = getIntent();
int imageId = intent.getIntExtra(PICTURE, R.drawable.ic_launcher);
ImageView image = (ImageView) findViewById(R.id.aboutImage);
image.setImageDrawable(getResources().getDrawable(imageId));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_second, menu);
return true;
}
public void onClick(View v) {
finish();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_2: {
Intent intent = new Intent(this, ThirdActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
case R.id.item_1: {
Intent intent = new Intent(this, FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
``
Третья активность
``public class ThirdActivity extends Activity implements OnClickListener {
public static final String DESCRIPTIONSALG = "descriptionsAlg";
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
// Получаем данные от деятельности FirstActivity
Intent intent = getIntent();
int textId = intent.getIntExtra(DESCRIPTIONSALG, R.string.txt_o);
TextView text = (TextView) findViewById(R.id.TextViev);
text.setText(getResources().getString(textId));
}
public static String getDescriptionsalg() {
return DESCRIPTIONSALG;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_third, menu);
return true;
}
public void onClick(View v) {
finish();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Проверяем ориентацию экрана
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_1: {
Intent intent = new Intent(this, FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
``
Столкнулся с проблемой.
Есть потребность в справочном приложении, которое будет выглядеть следующим образом:
Из первой активности (ListView) через intent происходит передача картинки во вторую активность в зависимости от выбранного элемента.
Во второй активности необходимо перейти к комментарию в виде текста в третьей активности. Вывод через нажатие иконки в Action Bar.
Подскажите, пожалуйста, как можно реализовать передачу текста в третью активность в строгой зависимости от первого выбора.
Первая активность.
``public class FirstActivity extends ListActivity {
/** Called when the activity is first created. */
public TextView Text;
public ArrayList<ListData> catalog;
String[] names;
String[] descriptionsAlg;
int img = R.drawable.ic_item_alg;
int[] png = { R.drawable.opisanie, R.drawable.predosterezenie };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
names = new String[2];
names[0] = getResources().getString(R.string.str_o);
names[1] = getResources().getString(R.string.str_p);
descriptionsAlg = new String[2];
descriptionsAlg[0] = getResources().getString(R.string.txt_o);
descriptionsAlg[1] = getResources().getString(R.string.txt_p);
// Создаем массив объектов ListData и заполняем их данными
catalog = new ArrayList<ListData>();
for (int i = 1; i <= 2; i++) {
catalog.add(new ListData(names[i - 1], png[i - 1], img,
descriptionsAlg[i - 1]));
}
// Создаем адаптер данных
CatalogAdapter catAdapter;
catAdapter = new CatalogAdapter(this, catalog);
setListAdapter(catAdapter);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Проверяем ориентацию экрана
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
}
}
public void onListItemClick(ListView parent, View v, int position, long id) {
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(SecondActivity.PICTURE, png[position]);
intent.putExtra(ThirdActivity.DESCRIPTIONSALG,
descriptionsAlg[position]);
startActivity(intent);
}
public TextView getText() {
return Text;
}
public void setText(TextView Text) {
this.Text = Text;
}
``
Вторая активность.
``public class SecondActivity extends Activity implements OnClickListener {
public static final String PICTURE = "picture";
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// Получаем данные от деятельности FirstActivity
Intent intent = getIntent();
int imageId = intent.getIntExtra(PICTURE, R.drawable.ic_launcher);
ImageView image = (ImageView) findViewById(R.id.aboutImage);
image.setImageDrawable(getResources().getDrawable(imageId));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_second, menu);
return true;
}
public void onClick(View v) {
finish();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_2: {
Intent intent = new Intent(this, ThirdActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
case R.id.item_1: {
Intent intent = new Intent(this, FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
``
Третья активность
``public class ThirdActivity extends Activity implements OnClickListener {
public static final String DESCRIPTIONSALG = "descriptionsAlg";
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
// Получаем данные от деятельности FirstActivity
Intent intent = getIntent();
int textId = intent.getIntExtra(DESCRIPTIONSALG, R.string.txt_o);
TextView text = (TextView) findViewById(R.id.TextViev);
text.setText(getResources().getString(textId));
}
public static String getDescriptionsalg() {
return DESCRIPTIONSALG;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_third, menu);
return true;
}
public void onClick(View v) {
finish();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Проверяем ориентацию экрана
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_1: {
Intent intent = new Intent(this, FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
``
</ListData></ListData>