user1641906
user1641906

Reputation: 117

How to pass Listview selected item values to another activity

I have successfully added Images from a ListView to a detailed View, when an item in the list is clicked on.But how would I add longer descriptions (which would not be shown in the list, but only appear in a textView in the single_list_item xml?Here's my current code for sending images(string "web" is short text descriptions, string ImageId is the list of images) to my SingleListItem.java.

setContentView(R.layout.hotels);

CustomList adapter = new CustomList(Hotels.this, web, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
        ImageView imgview = (ImageView) view.findViewById(R.id.img);
        //int images = imgview.getId();
        int images = position;
        //int images = parent.getAdapter().getItem(position);
        //Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
        Intent i = new Intent(getApplicationContext(), SingleListItem.class);
        //i.putExtra("zurag", images);
        i.putExtra("zurag", imageId[position]);
        startActivity(i);
    }
});

And here's my current code in SingleListItem.java:

public class SingleListItem extends Activity{
    Button button;
    Button button2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.single_list_item_view);

        ImageView images = (ImageView) findViewById(R.id.imageView1);
        Intent i = getIntent();
        int pic =i.getIntExtra("zurag", 0);
        images.setImageResource(pic);
        TextView texts = (TextView) findViewById(R.id.textView1);
        Intent i2 = getIntent();
        int text1 =i2.getIntExtra("zurag", 0);
        texts.setText(text1);

        addListenerOnButton();
    }
}

Could anyone show how I send over the longer text from, say, a string called longdesc?

Upvotes: 2

Views: 10184

Answers (2)

Somasundaram NP
Somasundaram NP

Reputation: 1018

    Intent i = new Intent(getApplicationContext(), SingleListItem.class);
            i.putExtra("str", longdesc);
            i.putExtra("zurag", imageId[position]);
            startActivity(i);

in the singlelistitem class

 Intent i = getIntent();
        Intent i2 = getIntent();
        String text1 =i2.getStringExtra("str");
        texts.setText(text1);

Upvotes: 4

Matthias Miro
Matthias Miro

Reputation: 206

sending string:

i.putExtra("longdesc", longdesc);

reading string at destination:

String longdesc = i.getStringExtra("longdesc");

Upvotes: 1

Related Questions