0

我是一个初学者程序员,所以请多多包涵。我正在尝试创建一个应用程序,其中列表视图中的项目会影响将在下一个活动中显示的内容。到目前为止,我有列表活动:

public class Primary extends ListActivity{
private static final String[] items = {"Item1", "Item2", "Item3", "item4", "Item5"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));

    TextView heading =(TextView)findViewById(R.id.listViewHeading);
    heading.setText("Primary");
}

public void onListItemClick(ListView parent, View v, int position, long id){

}

对于第二个活动,我有这个:

public class ImageActivity extends Activity{
TextView heading;
ImageView image;
TextView text;
    public static final String[] headings={"heading 1", "heading 2", "heading 3", "heading 4", "heading 5",};


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_activity);

    heading = (TextView)findViewById(R.id.adHeading);
    image = (ImageView)findViewById(R.id.adImage);
    text =(TextView)findViewById(R.id.adText);

    addInfo();
}

private void addInfo() {
    heading.setText(headings[x]);
    image.setImageResource(images[x]);
    text.setText(text[x]);

}

我怎样才能使标题、图像和文本根据列表视图中的选择项而改变?

4

4 回答 4

2

在列表视图活动中。

Intent i = new Intent(this, ImageActivity.class);
    i.putExtra("data", data);
    startActivity(i);

下一个 Acitivty onCreate() 方法。

final String data = getIntent().getStringExtra("data");
于 2012-05-12T01:34:56.253 回答
0

使用作为 Intent 一部分的“附加”功能。

当您从 Primary 调用 start ImageActivity 时,您可以使用“附加”在两者之间传递信息。

有关详细信息,请参阅链接。

I'll give you a basic example here. When the list item is clicked, put the data that you want ImageActivity to have into the intent using "putExtra".

Intent intent = new Intent(getBaseContext(), ImageActivity.class);
String data = "somedata";
intent.putExtra("DATA", data);
startActivity(intent)

Then, in ImageActivity onCreate, retrieve the data like this:

Bundle extras = getIntent().getExtras();
if(extras !=null) {
String data= extras.getString("DATA"); // matches the tag used in putExtra
}

Once you have retrieved the data, set the necessary views.

于 2012-05-12T01:38:28.773 回答
0

I think u want to set the heading, image and text in second activity, related to first activity's selected index in list.

just do 1 thing, put following code in 1st activity

    public void onListItemClick(ListView parent, View v, int position, long id)
    {
        Intent intent = new Intent(this.getApplicationContext(), ImageActivity.class);
        intent.putExtra("pos", position);
        startActivity(intent);
    }

so, now u r passing the position of item selected in list.

now, put following code in next activity

    private void addInfo()
    {
        Bundle ext = getIntent().getExtras();
        if(ext != null)
        {
            int pos= ext.getInteger("pos");
                       //  ext.getInt("pos");

            heading.setText(headings[pos]);

            //  hey, frend, you don't have any array for selecting image-name and text
            //  image.setImageResource(images[x]);
            //  text.setText(text[x]);
        }
    }
于 2012-05-12T02:19:02.070 回答
0

use below code

public void onListItemClick(ListView parent, View v, int position, long id)
        {
            Intent intent = new Intent(Primary.this, ImageActivity.class);
            intent.putExtra("selected value", item[position]);
            startActivity(intent);
        }

in ImageActivity class:in oncreate (or you can put item variable as global)

String item = getIntent().getStringExtra("Selected value");
于 2012-05-12T03:31:36.130 回答