Custom list view with StringArray doesnt work notifyDataSetChanged() - android

I have a list activity with string array, And when I click the list item (convertview) I want to edit my string.
Once I edit one string then I pressed Guncelle Button I see new text , everythink is oke.
But when I refresh the list I dont see new text. It doesnt change.
I use notifyDataSetChanged but I think its not working. How can I solve this problem.
public class huawei_list extends ListActivity {
private static int selected_item = -1;
private String[] phones = { "(0554) 741 xx xx", "(0554) 582 xx xx",
"(0543) 613 xx xx", "(0543) 613 xx xx", "(0507) 183 xx xx",
"(0216) 444 xx xx" };
private String[] birimler = { "HUAWEI NOBET", "NAZMİ İŞLER", "DENİZ NAMAL",
"OKTAY TATAR", "ALP SİVRİ", "HUAWEI ACİL DESTEK" };
private telefonlarAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// birimler = getResources().getStringArray(R.array.birimler_huawei);
mAdapter = new telefonlarAdapter();
setListAdapter(mAdapter);
}
private static class AccessoriesViewHolder {
public ListView list;
public ImageView list_image;
public TextView textview_list;
public TextView textview_list2;
}
private class telefonlarAdapter extends BaseAdapter {
#Override
public int getCount() {
// TODO Auto-generated method stub
return birimler.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return birimler[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
AccessoriesViewHolder holder = null;
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.single_row,
parent, false);
holder = new AccessoriesViewHolder();
holder.list_image = (ImageView) convertView.findViewById(R.id.list_image);
holder.textview_list = (TextView) convertView.findViewById(R.id.textview_list);
holder.textview_list2 = (TextView) convertView.findViewById(R.id.textview_list2);
holder.list = (ListView) convertView.findViewById(R.layout.single_row);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Toast.makeText(getApplicationContext(),
// "Bu Tıkladığınız "+position+". satırdır" ,
// 1000).show();
selected_item = position;
final Dialog dialog_guncelle = new Dialog(
huawei_list.this);
dialog_guncelle.setContentView(R.layout.dialog);
dialog_guncelle.setCancelable(true);
dialog_guncelle.setTitle(birimler[selected_item]);
dialog_guncelle.show();
ImageView dialogresim = (ImageView) dialog_guncelle.findViewById(R.id.imageView1);
TextView guncelleText = (TextView) dialog_guncelle.findViewById(R.id.guncelle_text);
final EditText editIsim = (EditText) dialog_guncelle.findViewById(R.id.editText_isim);
final EditText editTelefone = (EditText) dialog_guncelle.findViewById(R.id.editText_telefone);
Button guncelle = (Button) dialog_guncelle.findViewById(R.id.guncelle);
Button iptalButon = (Button) dialog_guncelle.findViewById(R.id.iptal);
guncelleText.setText(birimler[selected_item]);
editIsim.setText(birimler[selected_item]);
editTelefone.setText(phones[selected_item]);
iptalButon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog_guncelle.cancel();
}
});
guncelle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
birimler[selected_item] = editIsim.getText().toString();
phones[selected_item] = editTelefone.getText().toString();
notifyDataSetChanged();
dialog_guncelle.dismiss();
}
});
}
});
((Button) convertView.findViewById(R.id.call_button))
.setOnClickListener(call_buttonClisckListener);
convertView.setTag(holder);
} else {
holder = (AccessoriesViewHolder) convertView.getTag();
}
String s = phones[position];
holder.textview_list.setText(phones[position]);
holder.textview_list2.setText(birimler[position]);
if (s.contains("(0216)")) {
holder.list_image.setImageResource(R.drawable.masaustuphone);
} else {
holder.list_image.setImageResource(R.drawable.gsm);
}
return convertView;
}
}
private OnClickListener call_buttonClisckListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Cyril: Not implemented yet!
final int position = getListView().getPositionForView(v);
if (position != ListView.INVALID_POSITION) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + phones[position]));
startActivity(intent);
// Toast.makeText(getApplicationContext(),
// ""+getResources().getStringArray(R.array.huawei_rsmc)[position],
// Toast.LENGTH_SHORT).show();
}
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if (item.getItemId() == R.id.aboutmenu)
;
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.popup);
dialog.setTitle("İLETİŞİM");
dialog.setCancelable(true);
dialog.show();
TextView hakkindamail = (TextView) dialog
.findViewById(R.id.hakkindamail);
TextView hakkindamobile = (TextView) dialog
.findViewById(R.id.hakkindamobile);
hakkindamobile.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0507771xxxx"));
startActivity(intent);
}
});
hakkindamail.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String[] TO = { "mustafa.kayahan#xxxxx.com" };
Intent intentmail = new Intent(Intent.ACTION_SEND);
intentmail.setData(Uri.parse("mailto:"));
intentmail.putExtra(Intent.EXTRA_EMAIL, TO);
intentmail.putExtra(intentmail.EXTRA_SUBJECT,
"SMC REHBER HAKKINDA");
intentmail.setType("text/plain");
try {
startActivity(Intent.createChooser(intentmail,
"Send mail..."));
finish();
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(huawei_list.this,
"There is no email client installed.",
Toast.LENGTH_SHORT).show();
}
}
});
return super.onOptionsItemSelected(item);
}
}
This is my custom layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/list_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/call_button"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_alignBottom="#+id/list_image"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/selector"
android:focusable="false"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="0"
android:text="Ara"
android:textColor="#ffffff"
android:textSize="30sp" />
<TextView
android:id="#+id/textview_list2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/list_image"
android:text=""
android:textStyle="bold"
android:textColor="#color/birimler"/>
<TextView
android:id="#+id/textview_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/list_image"
android:layout_alignLeft="#+id/textview_list2"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textview_list3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textview_list2"
android:layout_below="#+id/textview_list2"
android:text=""
android:textSize="8sp"/>
</RelativeLayout>
Dialog Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/userinfoicon" />
<TextView
android:id="#+id/guncelle_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:text="Large Text"
android:textSize="25dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView3"
android:layout_width="56dp"
android:layout_height="wrap_content"
android:text="İsim"
android:textSize="20sp" />
<EditText
android:id="#+id/editText_isim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView4"
android:layout_width="56dp"
android:layout_height="wrap_content"
android:text="Tel"
android:textAlignment="center"
android:textSize="20sp" />
<EditText
android:id="#+id/editText_telefone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.86"
android:inputType="textNoSuggestions" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/guncelle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#drawable/selector"
android:text="Güncelle"
android:textColor="#FFFF" />
<Button
android:id="#+id/iptal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#drawable/selector"
android:text="İptal"
android:textColor="#FFFF" />
</LinearLayout>
</LinearLayout>

just Recall your adapter and Bind Data further after adding new data to StringArray.
Replace
notifyDataSetChanged();
with
mAdapter = new telefonlarAdapter();
setListAdapter(mAdapter);
and if setListAdapter(mAdapter); method not working in your Adapter class just set id of listview and get id using viewbyid then set adapter to it.
Thats it...

Related

RecyclerView Issue with Child Items Visibilities

Hey guys I am creating chatting app something like whatsapp but facing issue with Childs Visibilties in RecyclerView here it contains my message and message from server I've defined the two items for my message and server message in single layout and I'm Setting Visibility Based on the Boolean ; but the items are not rendering properly
Adapter Class :
public class MessageDetailsListAdapter extends RecyclerView.Adapter<MessageDetailsListAdapter.MessageViewHolder> {
private List<MessageModel> messagesList;
private Activity activityContext;
public MessageDetailsListAdapter(List<MessageModel> messagesList,Activity activityContext) {
this.messagesList = messagesList;
this.activityContext = activityContext;
}
#Override
public int getItemCount() {
return messagesList.size();
}
#Override
public void onBindViewHolder(MessageViewHolder messageViewHolder, final int i) {
final MessageModel msg = messagesList.get(i);
if(msg.getIsMessageSentbyMe())
{
messageViewHolder.card_linear.setVisibility(View.GONE);
messageViewHolder.messageTextMe.setText(msg.getMsgText());
messageViewHolder.userNameMe.setText(msg.getSenderName());
messageViewHolder.timeStampMe.setText(msg.getSentTime());
messageViewHolder.card_linearMe.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(activityContext.getString(R.string.brMessageDetailsDeleteItem));
intent.putExtra("deletePosition", i);
activityContext.sendBroadcast(intent);
return false;
}
});
}
else
{
messageViewHolder.card_linearMe.setVisibility(View.GONE);
messageViewHolder.messageText.setText(msg.getMsgText());
messageViewHolder.userName.setText(msg.getSenderName());
messageViewHolder.timeStamp.setText(msg.getSentTime());
messageViewHolder.card_linear.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(activityContext.getString(R.string.brMessageDetailsDeleteItem));
intent.putExtra("deletePosition", i);
activityContext.sendBroadcast(intent);
return false;
}
});
}
}
#Override
public MessageViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.messagedetail_listitem, viewGroup,false);
return new MessageDetailsListAdapter.MessageViewHolder(itemView);
}
public static class MessageViewHolder extends RecyclerView.ViewHolder {
public LinearLayout card_linear,card_linearMe;
public TextView userName,messageText,timeStamp,userNameMe,messageTextMe,timeStampMe;
public MessageViewHolder(View v) {
super(v);
card_linear = (LinearLayout) v.findViewById(R.id.card_linear);
card_linearMe = (LinearLayout) v.findViewById(R.id.card_linearMe);
userName = (TextView) v.findViewById(R.id.userName);
messageText = (TextView) v.findViewById(R.id.messageText);
timeStamp = (TextView) v.findViewById(R.id.timeStamp);
userNameMe = (TextView) v.findViewById(R.id.userNameMe);
messageTextMe = (TextView) v.findViewById(R.id.messageTextMe);
timeStampMe = (TextView) v.findViewById(R.id.timeStampMe);
}
}
public void add(MessageModel messageModel) {
// TODO Auto-generated method stub
messagesList.add(messageModel);
notifyItemInserted(messagesList.size());
}
public void addAll(Collection<MessageModel> messages)
{
messagesList.clear();
messagesList.addAll(messagesList);
notifyDataSetChanged();
}
public void remove(MessageModel mDelete) {
// TODO Auto-generated method stub
messagesList.remove(mDelete);
notifyDataSetChanged();
}
}
messagedetail_listitem.xml :
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="#dimen/message_detail_padding_top" >
<LinearLayout
android:id="#+id/card_linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/card_liner_margin_left"
android:layout_marginRight="#dimen/card_liner_margin_right"
android:background="#drawable/message_holder_white"
android:orientation="vertical"
android:padding="#dimen/card_linear_padding" >
<TextView
android:id="#+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Me"
android:paddingLeft="#dimen/card_linear_padding"
style="#style/MessageUserNameFont"
/>
<TextView
android:id="#+id/messageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/message_text_margin_top"
android:paddingBottom="#dimen/message_text_padding"
android:paddingLeft="#dimen/message_text_padding"
android:paddingTop="#dimen/message_text_padding"
style="#style/MessageTextFont"
android:text="hii fdsfdsfsdf sdf dsf sdfs"
/>
<TextView
android:id="#+id/timeStamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="#dimen/time_stamp_margin_right"
android:gravity="right"
android:text="22:20"
style="#style/MessageTextFont"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/card_linearMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/card_linearme_margin_left"
android:layout_marginRight="#dimen/card_linearme_margin_right"
android:background="#drawable/message_holder_blue"
android:orientation="vertical"
android:padding="#dimen/card_linearme_padding" >
<TextView
android:id="#+id/userNameMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Me"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/messageTextMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/message_text_margin_top"
android:paddingBottom="#dimen/message_text_padding"
android:paddingRight="#dimen/message_text_padding"
android:paddingTop="#dimen/message_text_padding"
android:text="hii"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/timeStampMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="#dimen/time_stamp_margin_right"
android:gravity="right"
android:text="22:20"
android:textColor="#android:color/white" />
</LinearLayout>
Important thing to understand here is that view holders are reused.
First, you have this condition:
if(msg.getIsMessageSentbyMe())
{
messageViewHolder.card_linear.setVisibility(View.GONE);
...
}
and later you have:
else
{
messageViewHolder.card_linearMe.setVisibility(View.GONE);
}
Now imagine the first time you're using your VH and the message is (for example) sent by you. You have effectively hidden card_linear.
Say the view holder now needs to be reused for a message that is NOT sent by you. You will also make the visibility of card_linearMe to gone.
What has happened to your VH is that both layouts are invisible after a scenario like this. To fix it, add these lines:
if(msg.getIsMessageSentbyMe())
{
messageViewHolder.card_linear.setVisibility(View.GONE);
messageViewHolder.card_linearMe.setVisibility(View.VISIBLE);
...
}
else
{
messageViewHolder.card_linear.setVisibility(View.VISIBLE);
messageViewHolder.card_linearMe.setVisibility(View.GONE);
}

How to change listview's button background color when button clicked in android

In my application, I keep yes and no button in listview as an item. setOnClickListener is defined in Adapter class. I have a problem with this. After I scroll the listview, I see the changed button in another row. How can I resolve my issue ?
public class QuizAdapter extends BaseAdapter {
private static final String FONTH_PATH_1 = "fonts/Brandon_reg.otf";
private static final String FONTH_PATH_2 = "fonts/Brandon_bld.otf";
private ArrayList<Question> questionList = new ArrayList<Question>();
private LayoutInflater inflater;
private Context context;
public QuizAdapter(Context context) {
super();
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
}
public void addListItem(final Question item) {
questionList.add(item);
notifyDataSetChanged();
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return questionList.size();
}
#Override
public Question getItem(int position) {
// TODO Auto-generated method stub
return questionList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
Question currentQuestion = getItem(position);
Typeface font1 = Typeface.createFromAsset(context.getAssets(),
FONTH_PATH_1);
Typeface font2 = Typeface.createFromAsset(context.getAssets(),
FONTH_PATH_2);
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.quiz_list, null);
holder.questionCounterTextView = (TextView) convertView
.findViewById(R.id.questionCounterTextView);
holder.questionCounterTextView.setTypeface(font2);
holder.questionTextView = (TextView) convertView
.findViewById(R.id.questionTextView);
holder.questionTextView.setTypeface(font1);
holder.yesButton = (Button) convertView
.findViewById(R.id.yesButton);
holder.yesButton.setTypeface(font2);
holder.noButton = (Button) convertView.findViewById(R.id.noButton);
holder.noButton.setTypeface(font2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.yesButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
holder.yesButton.setBackgroundColor(R.color.Gray);
}
});
holder.questionCounterTextView.setText(Integer.toString(position + 1));
holder.questionTextView.setText(currentQuestion.getQuestionMessage());
return convertView;
}
public static class ViewHolder {
public TextView questionCounterTextView, questionTextView;
public Button yesButton, noButton;
}
}
xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/White"
android:orientation="vertical"
tools:context="...." >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
<ImageView
android:id="#+id/flagImageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:src="#drawable/flag" />
<TextView
android:id="#+id/questionCounterTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/flagImageView"
android:layout_alignLeft="#+id/flagImageView"
android:layout_alignRight="#+id/flagImageView"
android:layout_alignTop="#+id/flagImageView"
android:layout_marginLeft="10dp"
android:text="1"
android:gravity="center|left"
android:textColor="#color/White"
android:textSize="#dimen/question_counter_text_size"/>
<TextView
android:id="#+id/questionTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/flagImageView"
android:text="#string/loremIpsum2"
android:textColor="#color/Blue"
android:textSize="#dimen/text_size" />
</RelativeLayout>
<LinearLayout
android:id="#+id/quizLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:orientation="horizontal"
android:weightSum="640" >
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30" />
<Button
android:id="#+id/yesButton"
style="#style/button_type"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="275"
android:background="#color/QuizGreen"
android:text="#string/yes"
android:textColor="#color/White"
android:textSize="#dimen/question_button_text_size" />
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30" />
<Button
android:id="#+id/noButton"
style="#style/button_type"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="275"
android:background="#color/QuizOrange"
android:text="#string/no"
android:textColor="#color/White"
android:textSize="#dimen/question_button_text_size" />
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30" />
</LinearLayout>
</LinearLayout>
I solved my problem with defining a new String in Question class which is kept user answer. Controlling yes or no than change the background.
holder.yesButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (currentQuestion.getUserAnswer() == null) {
questionList.get(position).setUserAnswer("TRUE");
notifyDataSetChanged();
} else if (currentQuestion.getUserAnswer().equals("FALSE")) {
questionList.get(position).setUserAnswer("TRUE");
notifyDataSetChanged();
}
}
});
holder.noButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (currentQuestion.getUserAnswer() == null) {
questionList.get(position).setUserAnswer("FALSE");
notifyDataSetChanged();
} else if (currentQuestion.getUserAnswer().equals("TRUE")) {
questionList.get(position).setUserAnswer("FALSE");
notifyDataSetChanged();
}
}
});
if (currentQuestion.getUserAnswer() == null) {
holder.yesButton.setBackgroundColor(context.getResources()
.getColor(R.color.QuizGreen));
holder.noButton.setBackgroundColor(context.getResources().getColor(
R.color.QuizOrange));
} else if (currentQuestion.getUserAnswer().equals("TRUE")) {
holder.yesButton.setBackgroundColor(context.getResources()
.getColor(R.color.NewsFeedDividerGray));
holder.noButton.setBackgroundColor(context.getResources().getColor(
R.color.QuizOrange));
} else {
holder.yesButton.setBackgroundColor(context.getResources()
.getColor(R.color.QuizGreen));
holder.noButton.setBackgroundColor(context.getResources().getColor(
R.color.NewsFeedDividerGray));
}
Have you tried not using the holder, and doing something like this?
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
RelativeLayout rl = (RelativeLayout)v.getParent();
Button bttn = (Button)rl.findViewById(R.id.yesButton);
bttn.setBackgroundColor(R.color.Gray);
}

Get listitem from listview(Customized using Radio Button) when clicked on the Particular Item

I have prepared custom Listview in that i want to select one item from view so for that i have used RadioButton view for single choice item,but i customized it.Following is my total code ,because there may be error in the layout so please help me in solving this.
Activity:-
public class MainActivity extends Activity {
TextView tvEmpty;
ListView listView;
Spinner spnStage;
Button btnGet;
String sfrom;
ArrayList<String> arrayList;
StartFromAdapter arrayAdapter;
String[] stages = { "School", "Collage", "Office" };
String resultdata = "{\"Age\":{\"School\":[{\"Stage\":\"class1\",\"Name\":\"Classname1\"},{\"Stage\":\"class2\",\"Name\":\"ClassName2\"}],\"Collage\":[],\"Office\":[{\"Stage\":\"Office1\",\"Name\":\"Officename1\"},{\"Stage\":\"Office2\",\"Name\":\"Officename2\"}]}}";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvEmpty = (TextView) findViewById(R.id.emptytv);
listView = (ListView) findViewById(R.id.lstDemo);
spnStage = (Spinner) findViewById(R.id.spinner1);
btnGet = (Button) findViewById(R.id.button1);
ArrayAdapter<String> stageAdapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_spinner_item, stages);
stageAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnStage.setAdapter(stageAdapter);
arrayList = new ArrayList<String>();
spnStage.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int pos,
long id) {
if (arrayList.size() > 0 && arrayList != null) {
arrayList.clear();
}
loadListView(parent.getSelectedItem().toString());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
btnGet.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int selectedPosition = (Integer) v.getTag();
Log.i("SELECTED IN GET", "" + selectedPosition);
}
});
}
public void loadListView(String selectedItem) {
try {
JSONObject jObject = new JSONObject(resultdata);
JSONObject jAge = jObject.getJSONObject("Age");
JSONArray jSelectedItem = jAge.getJSONArray(selectedItem);
if (jSelectedItem.length() != 0) {
for (int i = 0; i < jSelectedItem.length(); i++) {
JSONObject jObj = jSelectedItem.getJSONObject(i);
arrayList.add(jObj.getString("Name"));
}
}
arrayAdapter = new StartFromAdapter(this, arrayList);
arrayAdapter.notifyDataSetChanged();
listView.setAdapter(arrayAdapter);
listView.setEmptyView(tvEmpty);
Log.i("BEFORE LISTVIEW ONCLICK", "THIS IS ADDRESSLIST");
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int pos,
long id) {
sfrom = ((RadioButton) v.findViewById(R.id.startfromrb))
.getText().toString();
Log.i("STARTFROM", sfrom);
}
});
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Adapter:-
public class StartFromAdapter extends BaseAdapter {
ArrayList<String> detailsArrayList;
Context context;
int selectedPosition = 0;
public StartFromAdapter(Context context, ArrayList<String> detailsArrayList) {
this.detailsArrayList = detailsArrayList;
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return detailsArrayList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return detailsArrayList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout rowLayout = null;
if (convertView == null) {
rowLayout = (LinearLayout) LayoutInflater.from(context).inflate(
R.layout.listitem_view, parent, false);
} else {
rowLayout = (LinearLayout) convertView;
}
RadioButton rbStartFrom = (RadioButton) rowLayout
.findViewById(R.id.startfromrb);
rbStartFrom.setChecked(position == selectedPosition);
rbStartFrom.setTag(position);
rbStartFrom.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
selectedPosition = (Integer) v.getTag();
notifyDataSetInvalidated();
Log.i("IN ADAPTER", "" + selectedPosition);
}
});
rbStartFrom.setText(detailsArrayList.get(position));
return rowLayout;
}
}
MAINLAYOUT:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/lstDemo"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#+id/spinner1"
android:layout_marginBottom="60dp" >
</ListView>
<TextView
android:id="#+id/emptytv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/spinner1"
android:layout_marginBottom="60dp"
android:gravity="center_vertical|center_horizontal"
android:text="No Records Found."
android:textSize="25sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="31dp"
android:text="Button" />
</RelativeLayout>
listitem_view:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/startfromrb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="#null"
android:drawableRight="#android:drawable/btn_radio"
android:focusable="false"
android:focusableInTouchMode="false"
android:lines="3"
android:paddingLeft="20dp"
android:text="RadioButton" />
</LinearLayout>
Update use code as per below
// On item click listener
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long itemId) {
RadioButton btn=(RadioButton) view.findViewById(R.id.startfromrb);
String sfrom = btn.getText().toString();
arrayAdapter.setSelected((int)itemId);
Log.i("STARTFROM", sfrom);
}
});
//Add below function in adapter class
public void setSelected(int selectedPosition){
this.selectedPosition=selectedPosition;
notifyDataSetChanged();
}
Also remove from adapter
rbStartFrom.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
selectedPosition = (Integer) v.getTag();
notifyDataSetInvalidated();
Log.i("IN ADAPTER", "" + selectedPosition);
}
});
And add android:clickable="false" and set selectedPosition to -1 by default
The first I see is, that You have to change the reference to the Views where You want to set the other views below :
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/lstDemo"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#+id/spinner1" <!-- correct: #id/spinner1 -->
android:layout_marginBottom="60dp" >
</ListView>
<TextView
android:id="#+id/emptytv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/spinner1" <!-- correct: #id/spinner1 -->
android:layout_marginBottom="60dp"
android:gravity="center_vertical|center_horizontal"
android:text="No Records Found."
android:textSize="25sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="31dp"
android:text="Button" />
</RelativeLayout>
But anyway, Your TextView is sitten directly above the listView, is this Your intention? If not, You have to set
android:layout_below="#+id/lstDemo"
Also, if You get any errors, please post the logcat output. If You just have problems with Your layout, please explain clear enough what exactly You want.
EDIT
try to get the selected item String like this:
final String item = (String) parent.getItemAtPosition(position);
loadListView(item);

android edittext dynamic list

problem with settingText for each and individual edittext in listview.Edittext focus missing..Check my image above which is haveing custom keypad and i need to settext when keypad buttons is pressed.Below is my code. i tried but the focus is going for some other edittext.Please help me out.
private LayoutInflater mInflater;
public ArrayList myItems = new ArrayList();
List < HashMap < String, String >> painItems;
public MyAdapet()
{
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
painItems = new ArrayList < HashMap < String, String >> ();
HashMap < String, String > map = new HashMap < String, String > ();
for(int i=1;i<10;i++)
{
map.put("row_2", "Location Content");
map.put("row_3","Content");
map.put("row_4", "50");
map.put("row_5", "");
painItems.add(map);
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return painItems.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_xml, null);
} else {
}
returnText = (EditText) convertView
.findViewById(R.id.returnedit);
returnText.setInputType(InputType.TYPE_NULL);
//Fill EditText with the value you have in <span id="r774jm31_1" class="r774jm31">data source</span>
//we need to update adapter once we finish with editing
returnText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
listview.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
returnText.requestFocus();
}
});
return convertView;
}
My Listxml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:descendantFocusability="afterDescendants"
android:focusable="false"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
My custom List:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#cfcfcf"
android:gravity="center">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/seq_bg"
android:layout_centerInParent="true"
/>
<TextView
android:id="#+id/sqlno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="left"
android:text="1"
android:textColor="#000000"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:textStyle="bold" />
</RelativeLayout>
<TextView
android:id="#+id/lcontent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="Location"
android:textColor="#ffffff"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:textStyle="bold" />
<TextView
android:id="#+id/addcontent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="Add Content"
android:textColor="#ffffff"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:textStyle="bold" />
<TextView
android:id="#+id/drop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="50"
android:textColor="#ffffff"
android:gravity="center"
android:textStyle="bold"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:layout_weight="1"
/>
<EditText
android:id="#+id/returnedit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="67"
android:textColor="#000000"
android:textStyle="bold"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:layout_weight="1"
/>
<Button
android:id="#+id/viewnotesButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="view Notes"
android:textColor="#000000"
android:textStyle="bold"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
My clicklisteners for custom kepad is like this:
button0.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
returnText.setText(returnText.getText().toString()+"0");
MyAdapet.notifyDataSetChanged();
}
});
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
returnText.setText(returnText.getText().toString()+"1");
MyAdapet.notifyDataSetChanged();
}
});
try this code -
returnText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
listview.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
returnText.requestFocus();
notifyDataSetChanged(); // for update adapter
}
});
Hope this code helps you !!!
if its not working please let me know i will try to help more.

How do I add an OnClickListener to a button inside a ListView adapter?

I have a listview that contains the list of all my users. Every item from the list is a layout that has a button to show an AlertDialog to change the value of the label of the button. How can I dynamically add an on click event to that button that is generated by the listview Adapter?
This is my adapter:
public class PerfilAdapter extends BaseAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
List<PerfilBean> listaPerfiles = new ArrayList<PerfilBean>();
public Settings01 set=new Settings01();
public PerfilAdapter(Context context,List<PerfilBean> lista) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listaPerfiles=lista;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return listaPerfiles.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listaPerfiles.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout itemView;
if (convertView == null) {
itemView = (RelativeLayout) mLayoutInflater.inflate(
R.layout.item_perfil, parent, false);
} else {
itemView = (RelativeLayout) convertView;
}
// obtengo los valores de la vista
Button moneda = (Button) itemView.findViewById(R.id.Moneda);
TextView titulo = (TextView) itemView.findViewById(R.id.Titulo);
TextView nombredesc = (TextView) itemView.findViewById(R.id.txtNombre);
TextView descripcion = (TextView) itemView.findViewById(R.id.txtDescripcion);
String nombreM = Metodos.monedas[listaPerfiles.get(position).getPerfil_tipoMoneda()];
moneda.setText(nombreM);
titulo.setText(listaPerfiles.get(position).getPerfil_nombre());
nombredesc.setText(listaPerfiles.get(position).getPerfil_nombreSec());
descripcion.setText(listaPerfiles.get(position).getPerfil_texto());
return itemView;
}
// metodo parahacer la vista de la celda
public void actualizaDatosLista(List<PerfilBean> listaPerfilesM) {
for(int i=0;i<listaPerfilesM.size();i++){
Log.d("ITEM "+i,listaPerfilesM.get(i).getPerfil_nombreSec());
}
listaPerfiles = listaPerfilesM;
notifyDataSetChanged();
}}
and this is my Activity:
public class Settings01 extends Activity implements OnClickListener {
private List<PerfilBean> lst;
private PerfilDAO perfildao;
private PerfilAdapter perfiladapter;
private ListView lstPerfiles;
public void changeMoneda(final int position) {
int x = 0;
AlertDialog.Builder builder = new AlertDialog.Builder(Settings01.this);
builder.setTitle("Seleccione Tipo de Distribuidor");
builder.setItems(R.array.moneda, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
lst.get(position).setPerfil_tipoMoneda(item);
perfiladapter = new PerfilAdapter(getApplicationContext(), lst);
lstPerfiles.setAdapter(perfiladapter);
dialog.dismiss();
}
});
builder.create();
builder.show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings01);
lstPerfiles = (ListView) findViewById(R.id.lstSettings);
perfildao = new PerfilDAOImplDB(Settings01.this);
lst = new ArrayList<PerfilBean>();
lst = perfildao.getAll();
perfiladapter = new PerfilAdapter(getApplicationContext(), lst);
Log.d("Info", "En Settings");
lstPerfiles.setAdapter(perfiladapter);
}
#Override
public void onClick(View v) {
Log.d("Info", "derp" + v.getId());
}}
This is the layout that my adapter is currently using:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/Titulo"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="150dp"
android:gravity="left|center_vertical"
android:textColor="#color/Negro"
android:text="derp" />
<TextView
android:id="#+id/lblTipoMoneda"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_toLeftOf="#+id/Moneda"
android:gravity="left|center_vertical"
android:text="Tipo de moneda: " />
<Button
android:id="#+id/Moneda"
android:layout_width="160dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginRight="150dp"
android:gravity="left|center_vertical"
android:background="#color/Blanco"
android:textColor="#color/Negro"
android:text="Peso argentino" />
<ImageView
android:id="#+id/Separador"
android:layout_width="match_parent"
android:layout_height="2.5dp"
android:layout_below="#+id/Moneda"
android:layout_marginLeft="150dp"
android:layout_marginRight="150dp"
android:background="#color/Negro" />
<TextView
android:id="#+id/Nombre"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/Separador"
android:layout_marginLeft="150dp"
android:layout_marginTop="10dp"
android:clickable="true"
android:gravity="left|center_vertical"
android:onClick="changeMoneda"
android:text="Nombre :" />
<EditText
android:id="#+id/txtNombre"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_below="#+id/Separador"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/Nombre"
android:background="#drawable/fondotxt"
android:textColor="#color/Negro"
android:inputType="text" />
<TextView
android:id="#+id/lblTitulo"
android:layout_width="360dp"
android:layout_height="24dp"
android:layout_below="#+id/txtNombre"
android:layout_marginTop="10dp" />
<EditText
android:id="#+id/txtDescripcion"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#+id/lblTitulo"
android:layout_marginLeft="150dp"
android:layout_marginRight="150dp"
android:textColor="#color/Negro"
android:gravity="left|center_vertical" />
<ImageView
android:id="#+id/imgPicturefantes"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_below="#+id/txtDescripcion"
android:layout_toLeftOf="#+id/lblFotoAntes"
android:src="#drawable/what" />
<ImageView
android:id="#+id/imgPicturefdespues"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_below="#+id/txtDescripcion"
android:layout_marginLeft="50dp"
android:layout_toRightOf="#+id/centerPoint"
android:src="#drawable/what" />
<TextView
android:id="#+id/lblFotoDespues"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_below="#+id/txtDescripcion"
android:layout_marginTop="50dp"
android:layout_toRightOf="#+id/imgPicturefdespues"
android:gravity="center"
android:text="Foto despues: "
android:textSize="18sp" />
<ImageButton
android:id="#+id/btnDespuesF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/btnAntesF"
android:layout_toRightOf="#+id/imgPicturefdespues"
android:background="#drawable/btnupload" />
<TextView
android:id="#+id/centerPoint"
android:layout_width="2dp"
android:layout_height="2dp"
android:layout_below="#+id/txtDescripcion"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/lblFotoAntes"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_below="#+id/txtDescripcion"
android:layout_marginRight="50dp"
android:layout_marginTop="50dp"
android:layout_toLeftOf="#+id/centerPoint"
android:gravity="center"
android:text="Foto antes: "
android:textSize="18sp" />
<ImageButton
android:id="#+id/btnAntesF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/lblFotoAntes"
android:layout_marginRight="75dp"
android:layout_toLeftOf="#+id/centerPoint"
android:background="#drawable/btnupload" />
You can do so in the getView() method of your adapter. For that you will need to use a custom adapter (if you are not doing that already). It will be better if you can show the relevant portions of your code.
EDIT:
The dialog will be shown by your activity. So you can create an interface for listening to this button click event.
public interface BtnClickListener {
public abstract void onBtnClick(int position);
}
Let your custom adapter receive it as input.
private BtnClickListener mClickListener = null;
public PerfilAdapter(Context context, List<PerfilBean> lista, BtnClickListener listener) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listaPerfiles=lista;
mClickListener = listener;
}
Now you can simply set the normal onClickListener in getView() as below
Button moneda = (Button) itemView.findViewById(R.id.Moneda);
moneda.setTag(position); //For passing the list item index
moneda.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mClickListener != null)
mClickListener.onBtnClick((Integer) v.getTag());
}
});
Let your activity pass the required BtnClickListener as part of adapter creation.
perfiladapter = new PerfilAdapter(getApplicationContext(), lst, new BtnClickListener() {
#Override
public void onBtnClick(int position) {
// TODO Auto-generated method stub
// Call your function which creates and shows the dialog here
changeMoneda(position);
}
});
Assuming that lst.get(position).setPerfil_tipoMoneda(item); changes the text which will be used as button text correctly, you should simply call perfiladapter.notifyDataSetChanged() in the onClick of your dialog (Currently you are creating the adapter again which is not required).
public void onClick(DialogInterface dialog, int item) {
lst.get(position).setPerfil_tipoMoneda(item);
perfiladapter.notifyDataSetChanged();
dialog.dismiss();
}
Hope it will work as you expect it to.
In my case i had to add this attribute in the listView :
<ListView
...
android:clickable="true"
...
</ListView>
And in the adapter just add on click listener in the button view.
wrapper.getButtonHi().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DebugUtils.logDebug("Clickeado :: "+ mContact.getUserId());
}
});
Its important to set final the variables:
public View getRowView(final int position, View convertView, ViewGroup parent) {
final BrowseContactItemWrapper wrapper;
final UserModel mContact = lstContact.get(position);
.....
}
Just a small tweak to refresh the renderer from outside.
final FinalMenuListAdapter adapter = this;
viewHolder.deleteItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mClickListener != null)
mClickListener.onBtnClick((MenuItemObject)v.getTag(),adapter);
}
});
final MenuItemObject menuItemObject = getItem(position);
viewHolder.deleteItem.setTag(menuItemObject);

Categories

Resources