0

I have a ListView populated with some text ( phone models in my case, ex: Samsung A, B, C...) and an image ( loaded from URL using android-query ). When a row is selected, a ViewPager will be created along with 3 Tabs ( Specs, Info, Img ), both fragments Specs and Info are displaying a ListView and the last fragment, Img is displaying a GridView. I'm having no issues with the last fragment, but only with the first 2, as I don't know how to populate the ListView so it will display the content for the selected row.

Is it possible to specify for each row a different string array?

If not, how can I achieve this:

enter image description here

As you can notice in this example, Samsung D, has more informations than the previous one.

enter image description here

This is the main ListView's ClickListener:

listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putInt("id", position);
                TabStripAdapter newFragment = new TabStripAdapter();
                newFragment.setArguments(bundle);
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.frame_container, newFragment);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });

Sorry for my english.

Alec
  • 347
  • 2
  • 9
  • 29
  • Are you using main ListView's onClickListener? – dhun Apr 20 '15 at 17:51
  • **Yes** you can specify for each row a different data to display in your ViewPager. – Rami Apr 20 '15 at 18:09
  • Yes it is possible. Use an Custom ArrayAdapter.You can find nice tutorials regarding that [here](http://www.vogella.com/tutorials/AndroidListView/article.html) – dora Apr 20 '15 at 18:09
  • @dhun Yes I am using it to start the ViewPager and to pass data – Alec Apr 20 '15 at 18:16
  • okay, each time you call onClickListener, based on the item id, pass custom array adapter. Listview is totally dynamic, it can add as many rows as you want. You have the control. – dhun Apr 20 '15 at 18:34
  • This is harder than I first thought, I'll see if I can get it done tomorrow. In the mean time maybe someone can provide some code so I can understand better what I have to do. Thanks! – Alec Apr 20 '15 at 19:10
  • @Rami can you show me an example? – Alec Apr 21 '15 at 12:23
  • You just need to pass the data with bundle (like you did in your `onItemClick`), then in `onCreate()` method of the fragment you get this data ( e.g `Bundle bundle=getArguments();` and `int id = bundle.getInt("id", 0);` ). Finally you load the details of your object (front camera, back camera, storage...) depending on the `id` and build the view. – Rami Apr 21 '15 at 12:40

1 Answers1

0

You should use the on OnItemClickListener for your main ListView and you will have 3 options:

  1. Having an array of objects in the main listview, but only display the model name or the info you want, so when the user select the item just get the object from the position and pass the values (object atributes) to the other activity.

  2. Having an array of objects in the main listview, but only display the model name or the info you want, so when the user select the item just get the object from the position and pass the object to the other activity.

  3. Having an two atributes object array (itemId, itemName) at the first activity and when the user select the item, pass the itemId to the next Activity, and from the 3-tabed activity ask (to the web service or database) for the info from the item you have the Id.

It should look something like this:

    MainListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

    @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {  
 //       String selectedProductId = MainListArray.get(position).getId(); //Option 1 & 3
 //       String selectedProductInfo = MainListArray.get(position).getInfo ();
// ...more attributes  //Option 1
 //       CustomObject selectedProduct = MainListArray.get(position); //Option 2.
         Intent i = new Intent(_myContext, newActivity.class);
//         i.putString("productId", selectedProductId); //Option 1 & 3.
//         i.putString("productInfo", selectedProductInfo); //Option 1.
// ...more attributes //Option 2.
 //       i.putParceleable("product", selectedProduct); //Option 2.

         startActivity(i);
        }

});

For passing a complete object look at this thread, it will show you how to do it.

Community
  • 1
  • 1
Alejandro Cumpa
  • 2,118
  • 1
  • 24
  • 45