How to change a ListView item's color based on the "onActivityResult" response?

1
lv = new ListView(this);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
                                   android.R.id.text1, vetorNomes);
v.setAdapter(adapter); 

I have a list of names, when I click on "Peter" opens an Activity with a form to register it, after finishing the registration I give a finish () in Activity and I go back to the list of names, however I needed that Pedro's item was green, for example, to know that it was already registered, I am returning Pedro's name in the "onActivityResult" method, but I can not do that. Can someone help? Thank you in advance!

    
asked by anonymous 17.08.2015 / 17:52

1 answer

1

You will need to customize the adapter I have something that is roughly what you want to follow the code

public class AdapterList extends ArrayAdapter<String> {
private String[] values;
private int mSelectedItem;
private Context context;
public AdapterList(Context context, String[] values) {
    super(context, R.layout.layout_navigation_drawer);
    this.values = values;
    this.context = context;
}

public int getmSelectedItem() {
    return mSelectedItem;
}

public void setmSelectedItem(int mSelectedItem) {
    this.mSelectedItem = mSelectedItem;
}

@Override
public int getCount() {
    return this.values.length;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View rowView = inflater.inflate(R.layout.layout_navigation_drawer, parent, false);
    TextView  txt = (TextView) rowView.findViewById(R.id.txt_nav_drawer);
    txt.setText(values[position]);
    if(position == mSelectedItem){
        txt.setTextColor(getContext().getResources().getColor(android.R.color.white));
        txt.setBackgroundColor(getContext().getResources().getColor(R.color.red_color));
    }else {
        txt.setTextColor(getContext().getResources().getColor(R.color.green_color));
    }

   return rowView;
}

}

layout_navigation_drawer.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
    android:id="@+id/txt_nav_drawer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif-light"
    android:paddingBottom="10dp"
    android:paddingLeft="20dp"
    android:paddingTop="10dp"
    android:text="algo"
    android:textColor="@drawable/txt_selected"
    android:textSize="21dp" />

In the getView method I have an if that checks if the current position is the same as my selected item (mSelectedItem) if yes I change the color of that line. See that I created a method to pass the position that was clicked on your case will be the position of the name that you will make the registration.

What you need to do in the onActivityResult is to verify that the registration has actually been made and to move the position of the name that was clicked to register.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    lv = new ListView(this);
    AdapterList adapter = new AdapterList(this, vetornomes);
    lv.setAdapter(adapter);
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    //verificar se o cadastro foi feito caso sim o seguinte codigo deve ser adicionado
    adapter.setmSelectedItem(posicao);
    //para que seja atualizado a cor vc deve chamar o seguinte método
    adapter.notifyDataSetChanged();
}
    
20.08.2015 / 15:33