dimanche 4 décembre 2016

Setting ImageView at certain positions in a ListView based on CheckBox

So I have a List View that when you click on a row it opens up a new activity. In the new activity there's a checkbox. If you check the check box and then go back to the listview activity it should set a checkmark next to the list view item that was initially clicked.

Whats happening right now is when I check the checkbox and return to the listview every row has a checkmark next to it regardless of which row the checkbox was checked from.

heres my mainactivity with the listview and on click listener that starts the second checkbox activity

        //fill list view with xml array of routes
    final CharSequence[] routeListViewItems =    getResources().getTextArray(R.array.routeList);


    //custom adapter for list view
    ListAdapter routeAdapter = new CustomAdapter(this, routeListViewItems);
    final ListView routeListView = (ListView) findViewById(R.id.routeListView);
    routeListView.setAdapter(routeAdapter);

    routeListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    int listViewItemPosition = position;
                    CharSequence route = routeListViewItems[position];
                    int imageId = (int) image.getResourceId(position, -1);
                    if (route.equals(routeListViewItems[position]))
                    {
                        Intent intent = new Intent(view.getContext(), RouteDetails.class);
                        intent.putExtra("route", routeDetail[position]);
                        intent.putExtra("imageResourceId", imageId);
                        intent.putExtra("routeName", routeListViewItems[position]);
                        intent.putExtra("listViewItemPosition", listViewItemPosition);
                        startActivity(intent);
                    }
                }
            }
    );
}

then heres what im passing from the second activity back to the listview activity

 @Override ///////for back button///////
public void onBackPressed() {
    super.onBackPressed();
            ////////sets checkmark next to listview item//////
            if (routeCheckBox.isChecked())
            {
                Intent check = new Intent(RouteDetails.this,MainActivity.class);
                check.putExtra("checkImageResource", R.drawable.checkmark);
                check.putExtra("listViewItemPosition", listViewItemPosition);
                startActivity(check);
            }
}

and heres back in my listview activity where I recieve the info from my checkbox activity and set the checkmark

        /////////////set check mark/////////////
        ImageView checkImageView = (ImageView) customView.findViewById(R.id.checkImageView);

        int checkImageResourceId = ((Activity) getContext()).getIntent().getIntExtra("checkImageResource",0);

        int listViewItemPosition = ((Activity) getContext()).getIntent().getIntExtra("listViewItemPosition",0);

        /////I want to set this at only listViewItemPosition
        checkImageView.setImageResource(checkImageResourceId);
        //////////////////////////////////////////////////

thanks for any help!




Aucun commentaire:

Enregistrer un commentaire