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();
}
There are several ways to do this, you will have to give more information in particular on Adapter.
– ramaral
my Adapter receives data from the database (a list of names). I updated my question with the code snippet.
– Android_man
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.
– ramaral