1

I have a ListView, that when an item is clicked starts a new Activity. Once this Activity is done, depending on the result, I want to change the background color of that ListView item. How can I do this from onActivityResult? I know the position of the item being clicked, but I can't figure out the steps to do it. If I go the custom adapter route, how do I tell the Adapter which item was changed? I'm using SimpleAdapter at the moment.

relevant code of what I have so far

// Get listview
        lv = getListView();     

        // on selecting single product
        // launching Edit Product Screen
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {                

                // getting values from selected ListItem
                String assemblyName = ((TextView) view.findViewById(R.id.assemblyName)).getText()
                        .toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        ViewOrderlineAssemblyActivity.class);
                // sending pid to next activity
                in.putExtra("assemblyName", assemblyName);

                // starting new activity and expecting some response back
                startActivityForResult(in, 100);
            }
        });

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // if result code 100
        if (resultCode == 100) {
            // if result code 100 is received 
            // means user has successfully completed assembly. 
            //add assembly to our list of assemblies            

            Assembly assembly = (Assembly) data.getSerializableExtra("assembly");

            //if the assembly is found in our list, replace it, if we get to the end of the list and it is not found, add it to list.
            for(int i = 0;i<assemblies.size();i++){
                if(assemblies.get(i).getName().equalsIgnoreCase(assembly.getName())){
                    assemblies.remove(i);
                    assemblies.add(assembly);
                }
                else if(i == assemblies.size()-1){
                    assemblies.add(assembly);
                }
            }


            //change background color of listview item so we know it has been completed. 

        }

    }
john
  • 775
  • 3
  • 15
  • 31

1 Answers1

1

found the answer in this question: How can I update a single row in a ListView?

and the solution is this code

            //change background color of listview item so we know it has been completed. 
            View v = lv.getChildAt(position - lv.getFirstVisiblePosition());
            TextView assemblyText = (TextView) v.findViewById(R.id.assemblyName);
            assemblyText.setBackgroundColor(Color.BLUE);
Community
  • 1
  • 1
john
  • 775
  • 3
  • 15
  • 31