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);
}
Related
Based on my previous question , I have a problem with onClick on CardView in my RecyclerView. I need to change the card layout when the CardView is clicked with another card layout xml. I follow this and this this but its not work. I dont too understand how to declare one CardView layout again in my code. Here my Code :
Adapter.Java
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int view1 = 0;
public static final int view2 = 1;
private static OnRecyclerViewItemClickedListener recyclerViewItemClickedListener;
public void setOnRecyclerViewClickedListener (OnRecyclerViewItemClickedListener l) {
recyclerViewItemClickedListener = l;
}
public interface OnRecyclerViewItemClickedListener {
void OnRecyclerViewItemClicked(int position);
void OnRecyclerViewItemBind(ViewHolder holder, int position);
int OnRecyclerViewItemCount();
}
public Adapter() {
super();
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
if (viewType == view1) {
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_item_empty, parent, false);
return new ViewHolder(itemView);
}
else if (viewType == view2) {
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_item_occupied, parent, false);
return new ViewHolder2(itemView);
}
return null;
}
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
recyclerViewItemClickedListener.OnRecyclerViewItemBind((ViewHolder) holder, position);
}
#Override
public int getItemCount() {
return recyclerViewItemClickedListener.OnRecyclerViewItemCount();
}
public int getItemViewType() { return recyclerViewItemClickedListener.getItemViewType(); }
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txt_no_table;
public TextView txt_no_table_occ;
public TextView txt_pax_occ;
public TextView txt_guest_name_occ;
public TextView txt_bill_occ;
public Chronometer txt_time_occ;
public CardView cardview_table;
public LinearLayout title_layout;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txt_no_table = (TextView) itemView.findViewById(R.id.txt_no_table_empty);
txt_no_table_occ = (TextView) itemView.findViewById(R.id.txt_no_table);
txt_pax_occ = (TextView) itemView.findViewById(R.id.txt_pax);
txt_guest_name_occ = (TextView)itemView.findViewById(R.id.txt_guestname);
txt_bill_occ = (TextView) itemView.findViewById(R.id.txt_bill);
txt_time_occ = (Chronometer) itemView.findViewById(R.id.txt_time);
cardview_table = (CardView) itemView.findViewById(R.id.table_card_empty);
title_layout = (LinearLayout) itemView.findViewById(R.id.table_ll1);
}
#Override
public void onClick(View itemView) {
recyclerViewItemClickedListener.OnRecyclerViewItemClicked(getAdapterPosition());
}
}
public class ViewHolder2 extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txt_no_table2;
public TextView txt_pax2;
public TextView txt_guest_name2;
public TextView txt_bill2;
public TextView txt_time2;
public ViewHolder2 (View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txt_no_table2 = (TextView) itemView.findViewById(R.id.txt_no_table_);
txt_pax2 = (TextView) itemView.findViewById(R.id.txt_pax_);
txt_guest_name2 = (TextView)itemView.findViewById(R.id.txt_guestname_);
txt_bill2 = (TextView) itemView.findViewById(R.id.txt_bill_);
txt_time2 = (TextView) itemView.findViewById(R.id.txt_time_);
}
#Override
public void onClick(View itemView) {
recyclerViewItemClickedListener.OnRecyclerViewItemClicked(getAdapterPosition());
}
}
}
Here My Activity :
adapter.setOnRecyclerViewClickedListener(new Adapter.OnRecyclerViewItemClickedListener() {
#Override
public void OnRecyclerViewItemClicked(int position) {
try {
JSONObject currTable = filteredTableList.getJSONObject(position);
if (currTable.has("selected")) {
currTable.put("selected", !currTable.getBoolean("selected"));
} else {
currTable.put("selected",true);
}
adapter.notifyItemChanged(position);
} catch (JSONException e) {
e.printStackTrace();
}
try
{
Toast.makeText(TableActivity.this, filteredTableList.getJSONObject(position).getString("tischnr"), Toast.LENGTH_SHORT).show();
}
catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void OnRecyclerViewItemBind(Adapter.ViewHolder holder, int position) {
try {
JSONObject currTable = filteredTableList.getJSONObject(position);
holder.txt_no_table.setText(currTable.getString("tischnr"));
holder.txt_guest_name_occ.setText("");
holder.txt_pax_occ.setText("");
holder.txt_bill_occ.setText("");
int queasy33Index = ProgramMethod.getJSONArrayIndex(queasy33List,"number2", currTable.getInt("tischnr"));
if (queasy33Index >= 0) {
holder.txt_guest_name_occ.setText(queasy33List.getJSONObject(queasy33Index).getString("char2"));
holder.txt_pax_occ.setText(queasy33List.getJSONObject(queasy33Index).getString("number3"));
}
if (currTable.has("selected") && currTable.getBoolean("selected")) {
holder.itemView.setBackgroundResource(R.color.colorRedTableOcc);
holder.txt_no_table.setVisibility(View.INVISIBLE);
holder.txt_no_table_occ.setVisibility(View.VISIBLE);
holder.txt_bill_occ.setVisibility(View.VISIBLE);
holder.txt_pax_occ.setVisibility(View.VISIBLE);
holder.txt_time_occ.setVisibility(View.VISIBLE);
holder.txt_guest_name_occ.setVisibility(View.VISIBLE);
} else {
holder.itemView.setBackgroundResource(R.color.colorTableGreen);
holder.txt_no_table.setVisibility(View.VISIBLE);
holder.txt_no_table_occ.setVisibility(View.INVISIBLE);
holder.txt_bill_occ.setVisibility(View.INVISIBLE);
holder.txt_pax_occ.setVisibility(View.INVISIBLE);
holder.txt_time_occ.setVisibility(View.INVISIBLE);
holder.txt_guest_name_occ.setVisibility(View.INVISIBLE);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public int OnRecyclerViewItemCount() {
return filteredTableList.length();
}
});
My table_item_empty.xml (first layout) :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/table_card_empty"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/colorTableGreen"
app:cardCornerRadius="15dp"
app:cardElevation="5dp"
android:layout_margin="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
android:gravity="center">
<RelativeLayout
android:id="#+id/table_rl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/table_ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/txt_no_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="15dp"
android:gravity="center"
android:textColor="#color/colorWhite"
android:visibility="invisible"/>
</LinearLayout>
<TextView
android:id="#+id/txt_pax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:text="5"
android:textSize="10dp"
android:textColor="#color/colorWhite"
android:drawableLeft="#drawable/ic_airline_seat_recline_normal_white_18dp"
android:visibility="invisible"/>
<TextView
android:id="#+id/txt_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10:00"
android:textSize="10dp"
android:textColor="#color/colorWhite"
android:drawableLeft="#drawable/ic_access_time_white_18dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/txt_pax"
android:layout_marginStart="2dp"
android:visibility="invisible"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/table_rl2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/table_rl1">
<TextView
android:id="#+id/txt_guestname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:text="Budi"
android:textSize="10dp"
android:textColor="#color/colorWhite"
android:drawableLeft="#drawable/ic_supervisor_account_white_18dp"
android:visibility="invisible"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/table_rl2">
<TextView
android:id="#+id/txt_bill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10000"
android:drawableLeft="#drawable/ic_attach_money_white_18dp"
android:textSize="10dp"
android:textColor="#color/colorWhite"
android:layout_marginStart="1dp"
android:visibility="invisible"/>
</RelativeLayout>
<TextView
android:id="#+id/txt_no_table_empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12"
android:textSize="25dp"
android:gravity="center_vertical|center_horizontal"
android:textColor="#color/colorWhite"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
App looks like :
My code make a first layout gone when i clicked but i want to show/change another card layout.
This is CardView1 Looks like :
Another CardView xml (CardView2) :
EDITED :
I trick it. Now when one or more item clicked, it will change the view into like this :
But i still want to try with two different layout.xml view when item clicked. Its work because i only 'play' with 'INVISIBLE' and 'VISIBLE' in element on one layout, not the xml.
[EDITED] :
I'm still try to make my RecyclerView with two Layout view. I improve my Adapter like above. I follow this but i get a litle problem. In my Acivity, i call my itemView from ViewHolder1 with holder (example: holder.txt_time....) like my code above. I want to know how i can call my second holder (ViewHolder2) in my Activity? I try to make hard code with add some listener but its not work for me :(
Maybe somebody can help and guide me to fix my code. Every answer will helpful for me. Thanks before.
I've read some SO questions like this and this but still couldn't figure out whats wrong with my code.
I have a ListFragment, and each row has a TextView and a CheckBox.
Clicking the CheckBox is working, but clicking on the TextView does nothing, and OnListItemClick doesn't get called. I've also tried to dynamically add an OnClickListener, which make it work, but it's not the correct way to do it, and it also lacks the GUI feedback of the click (item being "highlighted" for a sec).
This is my textview_with_checkbox.XML I'm using for each item:
<?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="wrap_content"
android:clickable="true"
android:focusable="true"
android:gravity="left"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal" >
<TextView
android:id="#+id/textview_event_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="3dp"
android:paddingTop="5dp"
android:focusable="true"
android:singleLine="false" />
<CheckBox
android:id="#+id/checkbox_for_event_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:focusable="false"
android:clickable="false" />
</LinearLayout>
now the code:
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
displayEventsLogFiles();
}
#Override
public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String chosenItem = (String) getListAdapter().getItem(position);
mCallBack.onEventItemSelected(chosenItem);
}
static class myCustomAdapterViewHolder
{
public TextView eventName;
public CheckBox eventCheckBox;
}
private class myCustomAdapter extends ArrayAdapter<String>
{
private ArrayList<String> sortedList;
private Context m_oContext;
List<String> m_oValues = null;
public myCustomAdapter(Context cont, int viewResId, List<String> objects)
{
super(cont, viewResId, objects);
m_oContext = cont;
m_oValues = objects;
sortedList = new ArrayList<String>();
for (String str : objects)
sortedList.add(str);
java.util.Collections.sort(sortedList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View l_oRowView = convertView;
myCustomAdapterViewHolder l_oInitializedViewHolder = null;
final int l_nPosition = position;
// Use convertView if possible, otherwise inflate a view:
if (l_oRowView == null)
{
LayoutInflater inflater = (LayoutInflater) m_oContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
l_oRowView = inflater.inflate(R.layout.textview_with_checkbox, parent, false);
// PlaceHolder pattern:
final myCustomAdapterViewHolder l_oViewHolder = new myCustomAdapterViewHolder();
l_oViewHolder.eventName = (TextView) l_oRowView.findViewById(R.id.textview_event_name);
l_oViewHolder.eventCheckBox = (CheckBox) l_oRowView.findViewById(R.id.checkbox_for_event_name);
l_oViewHolder.eventCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = (CheckBox)v;
//cb.setChecked(!cb.isChecked());
String l_sFilename = l_oViewHolder.eventName.getText().toString();
if (cb.isChecked())
{
if (!m_lstSelectedFilenames.contains(l_sFilename))
m_lstSelectedFilenames.add(l_sFilename);
}
else
{
if (m_lstSelectedFilenames.contains(l_sFilename))
m_lstSelectedFilenames.remove(l_sFilename);
}
}
});
l_oViewHolder.eventCheckBox.setFocusable(false);
//l_oViewHolder.eventCheckBox.setClickable(false);
// "Add" the viewHolder as a tag:
l_oRowView.setTag(l_oViewHolder);
l_oInitializedViewHolder = l_oViewHolder;
}
else
l_oInitializedViewHolder = (myCustomAdapterViewHolder) l_oRowView.getTag();
// By now, the rowView is initialized, just get the viewHolder and then get the views from it, to update:
//myCustomAdapterViewHolder l_oViewHolder = (myCustomAdapterViewHolder) l_oRowView.getTag();
/*l_oInitializedViewHolder.eventName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCallBack.onEventItemSelected((String)getListAdapter().getItem(l_nPosition));
}
});*/
l_oInitializedViewHolder.eventName.setText(m_oValues.get(position));
return l_oRowView;
//return super.getView();
}
public myCustomAdapter(Context cont, int viewResId, String[] strings)
{
this(cont, viewResId, Arrays.asList(strings));
}
#Override
public String getItem(int position)
{
return sortedList.get(position);
}
#Override
public int getPosition(String item)
{
return sortedList.indexOf(item);
}
}
What am I doing wrong here?
All I want is to be able to select "files" for deletion, using the CheckBoxes
Try only using only onclick for textview and checkbox, not onListItemClick -which you can remove- as well, so you should change some properties for the linear layout as well.
<?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="wrap_content"
android:clickable="false"
android:focusable="false"
android:gravity="left"
android:orientation="horizontal" >
<TextView
android:id="#+id/textview_event_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="3dp"
android:paddingTop="5dp"
android:focusable="true"
android:singleLine="false" />
<CheckBox
android:id="#+id/checkbox_for_event_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:focusable="false"
android:clickable="false" />
</LinearLayout>
Implement in the adapter
// PlaceHolder pattern:
final myCustomAdapterViewHolder l_oViewHolder = new myCustomAdapterViewHolder();
l_oViewHolder.eventName = (TextView) l_oRowView.findViewById(R.id.textview_event_name);
l_oViewHolder.eventCheckBox = (CheckBox) l_oRowView.findViewById(R.id.checkbox_for_event_name);
l_oViewHolder.eventName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// your code for textview click
}
}
l_oViewHolder.eventCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = (CheckBox)v;
//cb.setChecked(!cb.isChecked());
String l_sFilename = l_oViewHolder.eventName.getText().toString();
if (cb.isChecked())
{
if (!m_lstSelectedFilenames.contains(l_sFilename))
m_lstSelectedFilenames.add(l_sFilename);
}
else
{
if (m_lstSelectedFilenames.contains(l_sFilename))
m_lstSelectedFilenames.remove(l_sFilename);
}
}
});
l_oViewHolder.eventCheckBox.setFocusable(false);
add following attribute:
android:focusable="false"
android:clickable="false"
for example in my xml file:
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:button="?android:attr/listChoiceIndicatorSingle"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:buttonTint="#color/gray"
android:focusable="false"
android:clickable="false"
/>
and in my code:
public class MyTestFragment extends ListFragment {
.....
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
}
}
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...
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);
}
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);