0

I want to open a list view of objects and clicking on a list row, it will open up another activity displaying the news detail of that rows. So far i am able to open a new activity that displays name of row when row in list is clicked. But i dont know how can i make activity for every row to display some different information such as Address, contact information opening hours for each row.

this is my first activity that show listview

 // storing string resources into Array
 String []Buildings_halls = getResources().getStringArray(R.array.halls);

    // Binding resources Array to ListAdapter
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.hallstudent, R.id.listhall, Buildings_halls));

 }

 protected void onListItemClick(ListView lv, View v, int position, long id){
     super.onListItemClick(lv, v, position, id);     

Intent intent = new Intent();
intent.setClass(StudentHall.this, StudentHallSelect.class);

intent.putExtra("position", position);

// Or / And
intent.putExtra("id", String.valueOf(id));
intent.putExtra("description", getResources().getStringArray(R.array.hallsdescription));
startActivity(intent);

        }               

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}
}
this is my second code package 





 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selecthall);
        // TODO Auto-generated method stub


        Intent intent = getIntent(); 
        int position = intent.getIntExtra("position", 0);
        String halldetails=intent.getStringExtra("description");

        TextView TextView1 = (TextView) findViewById(R.id.select_details);
        TextView1.setText(String.valueOf(halldetails));
        // Here we turn your string.xml in an array
        String[] myKeys = getResources().getStringArray(R.array.halls);

        TextView myTextView = (TextView) findViewById(R.id.selecthalllist);
        myTextView.setText(myKeys[position]);


    }
}



 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/select_details"
        android:padding="10dp"
        android:text="@string/news_description"
        android:textAppearance="?android:attr/textAppearanceMedium" />



</LinearLayout>

i am getting the name of list and Null in my new activity but not the details that i stored in my array.xml file. thanks

Mesh
  • 19
  • 1
  • 8
  • Are you trying to Ask what will go to the next screen `position` or `id`. Or something else. Everything you pass in `intent.putExtra` can be used in the next screen – MDMalik Feb 04 '15 at 16:26
  • You have the position of the item clicked in the next activity. Fetch the information using that position from where it is stored and display it. – Rohit5k2 Feb 04 '15 at 16:27
  • yeah i am having problem doing that.. what i tried was get another string of arrays same as String [ ] Buildings from arrays.xml which contains details of each buildings. But i couldn't put it on adapter.. i tried creating two seperate adapter for each array but didnt work.. – Mesh Feb 05 '15 at 23:34
  • and regarding using intent i dont know how a parameter value in put.extra (value, value)is suppose to hold the details and pass it to another activity as i keep getting error like cannot be resolved to variable.. does the value should be referenced for it to be used?, how does the value hold these details that i want to pass to another activity without? Sorry for amateur questions!! – Mesh Feb 05 '15 at 23:39

2 Answers2

0

For the case of passing the selected object from the list to the detail screen, see here: How to view detailed data of a specific list view item

Alternatively, if your data is in an SQLite DB, you would:

  1. Use an CursorAdapter
  2. Pass the id parameter of the selected item
  3. In your detail screen use the ID parameter to query for the selected object
Community
  • 1
  • 1
dominicoder
  • 9,338
  • 1
  • 26
  • 32
0

It depends on where and how is this extra information stored. I prefer to retrive this information from the second activity using the position, but there's other ways if this one doesn't suit you.

You could pass all the information needed using the same method that you're using for position. You can add diferent data types in the same intent, like int, String, float, long, byte, boolean, Arrays and also Parcelable and Serializable elements.

Simply use intent.putExtra("key", value); in the first activity and getIntExtra("key", defaultValue), getLongExtra("key", defaultValue), getFloatExtra("key", defaultValue)... in the second activity, depending on the type of each extra.

Jofre Mateu
  • 2,390
  • 15
  • 26
  • hello thanks for the answers but i have some questions regarding this intent.putExtra("key", value);..how/where do i assign/store my information so that i can send it using value. i have stored this information in array.xml file like this Address:, Telephone No:,Opening Times: – Mesh Feb 05 '15 at 23:49
  • If you want to use an string array you should declare it like this: ` Address Telephone Opening Times ` Then, in the first activity, `intent.putExtra("key", getResources().getStringArray(R.array.hallsdescription));` – Jofre Mateu Feb 06 '15 at 09:59
  • thanks for the reply..i updated my code above and when i execute them i only see null written on the new activity. – Mesh Feb 06 '15 at 16:43
  • It shoud be `String[] Descriptions = intent.getStringArrayExtra("description");` – Jofre Mateu Feb 06 '15 at 16:47