How to change color a Listview item based on the "onActivityResult" response?

Asked

Viewed 466 times

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 "Pedro" opens an Activity with a form to register it, after finishing the registration I give a Finish() in Activity and go back to the list of names, but I needed Pedro’s item to be for example, green, to know that it has already been registered, I’m returning Pedro’s name in the method "onActivityResult", but I’m not able to do it. Can anyone help? I thank you in advance!

  • There are several ways to do this, you will have to give more information in particular on Adapter.

  • my Adapter receives data from the database (a list of names). I updated my question with the code snippet.

  • You’ll have to create your own Adapter, preferably inherited from Cursoradapter. Having in the bank the information whether a name is registered or not, it is easy to implement what you want.

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" />

within the getView method I have an if that checks if the current position is equal to my selected item (mSelectedItem) if yes I modify the color of that line. Note that I created a method to pass the position that was clicked in your case will be the position of the name you will make the registration.

What you need to do in onActivityResult is to check if the registration was really done and pass 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();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.