1

I have a listview inside a fragment class. When a listview item is clicked another activity is opened. The xml file of that activity has 2 imageviews inside a scroll view. The images change with respect to every listview item clicked. There are 5 listviews in my app. Each listview has 400+ items. What approach to use so that it runs smoothly. Is switch statement a good idea? Currently i am using a switch statement and there are only 4 items so far. Should i keep this approach?

My code

public class urdufrag1 extends Fragment {

public urdufrag1() {
    // Required empty public constructor
}

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

}

private static final String TAG = "urdufrag1";

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
main_content, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.geetfrag_urdu, main_content, false);



     final int[] menuImage = {R.drawable.tgeet94, 
     R.drawable.tgeet95,R.drawable.tgeet96,R.drawable.tgeet97};
    final ListView listView = (ListView) view.findViewById(R.id.GeetListU);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
          Intent intent = new Intent(getActivity(), PackageG.class);
            intent.putExtra("position", position);
            startActivity(intent);

        }
    });
    AdapterGeetUrdu adapter = new AdapterGeetUrdu(getContext(), menuImage);
    listView.setAdapter(adapter);

    // Inflate the layout for this fragment



    return view;
}

}

Second Activity

public class PackageG extends AppCompatActivity {

ImageView img1, img2;
PhotoViewAttacher mAttacher;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_package_g);

    img1 = (ImageView) findViewById(R.id.imageView8);
    img2 = (ImageView) findViewById(R.id.imageView9);
    mAttacher = new PhotoViewAttacher(img1);
    mAttacher = new PhotoViewAttacher(img2);

    Intent intent = this.getIntent();
    if(intent != null){
        Integer position = intent.getExtras().getInt("position");

        switch (position){

            case 0: img1.setImageResource(R.drawable.geet94);
                    img2.setImageResource(R.drawable.geet94_1);
                break;
            case 1: img1.setImageResource(R.drawable.geet95);
                    img2.setImageResource(R.drawable.geet95_1);
                break;
            case 2: img1.setImageResource(R.drawable.geet96);
                    img2.setImageResource(R.drawable.geet96_1);
                break;

            default:
                img1.setImageResource(R.drawable.carol);
                img2.setImageResource(R.drawable.carol);
                break;

        }

    }

    mAttacher.update();

}

}

My list view

Second Activity

Fidan Gill
  • 63
  • 7
  • Nopes. But i am asking this because i am not sure that will 400+ switch statements work fine or i should use some different approach? – Fidan Gill Aug 15 '17 at 21:18
  • [Resources#getIdentifier(String name, String defType, String defPackage)](https://stackoverflow.com/questions/15503639/how-i-can-optimize-this-code-that-contain-some-repetitive-line#answer-15503893) Return a resource identifier for the given resource name. – matoni Aug 15 '17 at 21:34

1 Answers1

0

This is the perfect job for a TypedArray. Create a file res/values/arrays.xml with something like

<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="firstImages">
    <item>@drawable/R.drawable.geet94</item>
    <item>@drawable/R.drawable.geet95</item>
    <item>@drawable/R.drawable.geet96</item>
    ...
</array>
<array name="secondImages">
    <item>@drawable/R.drawable.geet94_1</item>
    <item>@drawable/R.drawable.geet95_1</item>
    <item>@drawable/R.drawable.geet96_1</item>
    ...
</array>
</resources>

and then in your second Activity:

Intent intent = this.getIntent();
if(intent != null){
    Integer position = intent.getExtras().getInt("position");
    Resources res = getResources();

    TypedArray firstImages = res.obtainTypedArray(R.array.firstImages);
    TypedArray secondImages = res.obtainTypedArray(R.array.secondImages);

    Drawable firstDrawable = firstImages.getDrawable(position);
    Drawable secondDrawable = secondImages.getDrawable(position);

    img1.setImageDrawable(firstDrawable);
    img2.setImageDrawable(secondDrawable);
}
kenny_k
  • 3,831
  • 5
  • 30
  • 41
  • Is this approach better than switch statement? – Fidan Gill Aug 16 '17 at 06:35
  • Imagine you have 400 elements, and you wanted to insert something at position 100. You'd have to go and increase the value of every of the following 300 `case` statements by 1! With this, you just insert into the array at the right position, and you're done. Also, in general, almost any time you can replace an imperative style with something declarative it's a win. It's much easier and safer to edit an array in XML than to edit a flow control structure in Java. – kenny_k Aug 16 '17 at 08:01
  • Did you find this helpful? Are there additional questions? – kenny_k Aug 17 '17 at 10:01
  • what if e.g in the xml file in "first images" there are files @drawable/R.drawable.geet94, @drawable/R.drawable.geet95, @drawable/R.drawable.geet96. But in "Second images file there is only @drawable/R.drawable.geet96_1. How will it relate these 2 files? I mean how will it assess the correct position of second images with respect to first images? – Fidan Gill Aug 17 '17 at 15:55
  • The way to do that in XML is to make an array composed of 2-member arrays, where the 2-memeber arrays will be the pairs of drawables. See [here](https://stackoverflow.com/a/5931094/356629) for an example. – kenny_k Aug 17 '17 at 16:07