I am using edit text in a listview, but listview onItemclick is not working, When i click on list item then its not clickable
when i am using android:focusable="false"
android:focusableInTouchMode="false" on edit text then edit text is not enable
Please Help me How a can fix this problem
My Listview code
<LinearLayout
android:id="#+id/listlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="#+id/createlist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:clickable="true"
android:descendantFocusability="beforeDescendants"
android:divider="#eeeeee"
android:dividerHeight="1dip"
android:scrollingCache="true"
android:smoothScrollbar="true"
android:transcriptMode="normal" >
</ListView>
</LinearLayout>
List Item Xml file
<?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="horizontal"
android:padding="18dip"
>
<TextView
android:id="#+id/txtItemcode"
android:layout_width="110dip"
android:layout_height="fill_parent"
android:layout_marginLeft="10dip"
android:layout_marginTop="9dip"
android:focusable="false"
android:focusableInTouchMode="false"
android:singleLine="true"
android:text="323232"
android:textColor="#0C090A"
android:textSize="20sp" />
<TextView
android:id="#+id/txtItem"
android:layout_width="300dip"
android:layout_height="fill_parent"
android:layout_marginLeft="25dp"
android:layout_marginTop="9dip"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="5456455565456"
android:textColor="#0C090A"
android:textSize="20sp" />
<EditText
android:layout_marginTop="4.5dip"
android:id="#+id/editcreateQuantity"
android:layout_width="100dp"
android:layout_height="50dip"
android:background="#android:drawable/editbox_background"
android:ems="10"
android:inputType="number"
android:singleLine="true"
/>
</LinearLayout>
My Getview Java code
// Create List Adapter
class CreateAdapter extends ArrayAdapter<String> {
String[] strItecode=null;
String[] strItem;
String[] strQuantity;
Context context;
int temp;
CreateAdapter(Context context, String[] strItemcode, String[] strItem,
String[] strQauntity) {
super(context, R.layout.create_list_item, R.id.txtItemcode, strItemcode);
this.context = context;
this.strItecode = strItemcode;
this.strItem = strItem;
this.strQuantity = strQauntity;
// text= new String[strItem.length];
}
private int editingPosition = 0;
private TextWatcher watcher = new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
text[editingPosition] = s.toString();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void afterTextChanged(Editable s) { }
};
public View getView( final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
temp=position;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater
.inflate(R.layout.create_list_item, parent, false);
holder.txtItecode = (TextView) convertView
.findViewById(R.id.txtItemcode);
holder.txtItem = (TextView) convertView
.findViewById(R.id.txtItem);
holder.editQuantity = (EditText) convertView
.findViewById(R.id.editcreateQuantity);
holder.editQuantity.setTag(position);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.editQuantity.removeTextChangedListener(watcher);
if(text[temp].contentEquals("0"))
holder.editQuantity.setText("");
else
holder.editQuantity.setText(text[temp]);
holder.editQuantity.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
editingPosition = position;
}
}
});
holder.editQuantity.addTextChangedListener(watcher);
// The rest is good, just make sure to remove the call to setting the EditText's text at the end
holder.txtItecode.setText(strItecode[position]);
holder.txtItem.setText(strItem[position]);
// holder.editQuantity.setText(text[temp]);
return convertView;
}
class ViewHolder {
TextView txtItecode;
TextView txtItem;
EditText editQuantity;
}
}
Thanks IN Advance
Try to change value
android:descendantFocusability="beforeDescendants"
to
android:descendantFocusability="blocksDescendants"
Hope it help for you!
Related
Please Help to Solve My Issue !!
In the Activity there is a listview and A Button
ListView Row contains a TextView which displays the Serial No and 3 Edittexts (in which values will be entered)
The button Adds another row to the List.
I used my own class MemberEntry.
Problem is that In listview, data is entered smoothly but on scrolling
the textview value (Serial No.) remains fine but
the value of edittext (All 3) changes(The value of some other Edittext from the list takes place of another one).
I used this and found a little bit good results. But Not Accurate.
Code
Activity
OnCreate
MemberEntry m = new MemberEntry();
memberEntries = new ArrayList<>();
memberEntries.add(m);
adapter = new CustomListViewAdapter3(this, R.layout.row_entry_member, memberEntries);
LVMembers.setAdapter(adapter);
OnClick of a button
public void fab(View view) {
memberEntries.add(new MemberEntry());//memberEntries and adapter are global
adapter.notifyDataSetChanged();
}
Adapter
public class CustomListViewAdapter3 extends ArrayAdapter<MemberEntry> {
Context context;
int layoutResourceId;
public String[] Current;
ArrayList<MemberEntry> data = new ArrayList<>();
public static HashMap<Integer,String> myListOfNames=new HashMap<Integer,String>();
public CustomListViewAdapter3(Context context, int layoutResourceId,
ArrayList<MemberEntry> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
Current=new String[50];
this.data = data;
Log.d("CustomListViewAdapter", "Data = " + data + "\n Data Length = " + data.size() + "\n getCount=" + getCount());
for(int i=0;i<50;i++)
{
myListOfNames.put(i,"");
}
}
#Override
public int getCount() {
if(data != null && data.size() != 0){
return data.size();
}
return 0;
}
#Override
public MemberEntry getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public int getViewTypeCount() {
return 1;
}
#Override
public int getItemViewType(int position) {
return position;
}
#NonNull
#Override
public View getView(final int position, View convertView, #NonNull ViewGroup parent) {
View row = convertView;
final RecordHolder holder;
//d("CustomListViewAdapter","GetView() called");
MemberEntry m = data.get(position);
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.TVMemNo = (TextView) row.findViewById(R.id.TVMemNo);
holder.ETName = (EditText) row.findViewById(R.id.ETName);
holder.ETMobileNo = (EditText) row.findViewById(R.id.ETMobile);
holder.ETDefaultShare = (EditText) row.findViewById(R.id.ETDefaultShare);
row.setTag(holder);
m.setBinded(true);
Log.w("Yo","M is now Binded");
//e("Row is NOT Null", "NOT NULL NOT NULL NOT NULL");
} else {
if(m.isBinded()) {
Log.w("Yo","M found Binded");
holder = (RecordHolder) row.getTag();
}else{
Log.w("Yo","M found unBinded");
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.TVMemNo = (TextView) row.findViewById(R.id.TVMemNo);
holder.ETName = (EditText) row.findViewById(R.id.ETName);
holder.ETMobileNo = (EditText) row.findViewById(R.id.ETMobile);
holder.ETDefaultShare = (EditText) row.findViewById(R.id.ETDefaultShare);
row.setTag(holder);
Log.w("Yo","M is now Binded");
m.setBinded(true);
}
//e("Row is Null","NULL NULL NULL");
}
holder.ref = position;
holder.TVMemNo.setText("+ Add Member " + (position + 2));//don't think much about +2, answer well and i will give a +2 (from my friend's account)
holder.ETName.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
Current[holder.ref] = s.toString();
myListOfNames.put(position,s.toString().trim());
}
});
holder.ETName.setText(myListOfNames.get(position));
m.setRow(row);
data.set(position, m);
Log.d("Hi", "Added A Row");
return row;
}
//Log.e("Logger", "Position=" + position);
public ArrayList<MemberEntry> getEntries() {
return data;
}
static class RecordHolder {
//TextView TVRequestSent;
TextView TVMemNo;
EditText ETName, ETMobileNo, ETDefaultShare;
int ref;
}
}
row_entry_member.xml
<?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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginBottom="15dp"
android:layout_marginTop="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_weight=".3"
android:orientation="horizontal">
<TextView
android:id="#+id/TVMemNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:layout_weight="1.6"
android:gravity="left"
android:text="+ Add Member 2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#4285F4"
android:textStyle="bold" />
<TextView
android:id="#+id/textView96"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="right"
android:text="(if Require) Default Share %"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#4285F4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:layout_weight="1.8"
android:gravity="center_horizontal"
android:orientation="horizontal">
<android.support.design.widget.TextInputLayout
android:id="#+id/textInpuitLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/textInputLayout"
android:layout_weight="1">
<EditText
android:id="#+id/ETName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="left"
android:hint="Name"
android:inputType="textPersonName"
android:maxLength="15"
android:textColor="#33691e" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/texjtInpuitLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/textInputLayout"
android:layout_weight="1.2">
<EditText
android:id="#+id/ETMobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="left"
android:hint="Mobile No."
android:inputType="number"
android:maxLength="10"
android:textColor="#33691e" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/textInpjuitLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/textInputLayout"
android:layout_weight="1.6">
<EditText
android:id="#+id/ETDefaultShare"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:layout_weight="1.4"
android:gravity="center"
android:hint=" %"
android:inputType="number"
android:maxLength="2"
android:textColor="#33691e" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I have already searched on google but not getting proper accurate Solution.
Please Check My Code and Suggest best for me.
When I try to click on EditText inside ListView item. It gains focus and loses focus, while debugging I found that if I clicked on 21st item, the position value changed like 21, 19, 1, 2 etc. Not sure if the list is re-rendering itself or something else is happening. I have already searched a lot and already tried configuring ListView (beforeDescendants and afterDescendants) and Activity (adjustPan).
I have used my custom adaptor. Sharing the XML file of ListView and list item along with java code of adapter.
XML list :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<!-- submit progress -->
<ProgressBar
android:id="#+id/list_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone"/>
<LinearLayout
android:id="#+id/list_form"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp">
<TextView
android:id="#+id/nodata_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No data Found"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:visibility="gone"/>
</LinearLayout>
<LinearLayout
android:id="#+id/info_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<Button
android:id="#+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Order"
android:textSize="14sp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_weight="1"
android:textColor="#android:color/white"
android:background="#drawable/bg_button"/>
</LinearLayout>
<LinearLayout
android:id="#+id/ly_item_list_form"
android:layout_below="#+id/info_ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#+id/item_list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="afterDescendants"/>
</LinearLayout>
</RelativeLayout>
XML of list_item_selector
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:id="#+id/card_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="2dp"
card_view:cardPreventCornerOverlap="true"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical">
<TextView
android:id="#+id/item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="ItemName"
android:textColor="#color/colorPrimaryDark"
android:textSize="15sp"
android:textStyle="bold"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:background="#FF0B38CC"></RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/uom_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="10dp"
android:text="uom"
android:textColor="#color/colorPrimary"
android:textSize="13sp"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<ImageButton
android:id="#+id/button_minus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_remove_black_24dp"/>
<EditText
android:id="#+id/number_of_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:gravity="center"
android:text="0"
android:digits="0123456789"
android:maxLength="4"
android:inputType="number"
android:textColor="#color/colorPrimary"/>
<ImageButton
android:id="#+id/button_plus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_add_black_24dp"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
My custom Adaptor class
public class OrderViewListAdapter extends ArrayAdapter<OrderItemList> {
public OrderViewListAdapter(Context context, int resource) {
super(context, resource);
}
private class ViewHolder {
TextView sku_desc;
TextView uom_desc;
EditText order_qty;
ImageButton plus;
ImageButton minus;
AutoCompleteTextView uom;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Get the data item for this position
OrderItemList vehicleTransit = mDataSet.get(position);
//System.out.println("kamal123" +mDataSet.get(position).getOrderID());
System.out.println("kamal123" +position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_item_selector, parent, false);
viewHolder.sku_desc = (TextView) convertView.findViewById(R.id
.item_name);
viewHolder.uom_desc = (TextView) convertView.findViewById(R.id
.uom_desc);
viewHolder.order_qty = (EditText) convertView.findViewById(R.id.number_of_item);
viewHolder.plus= (ImageButton)convertView.findViewById(R.id.button_plus) ;
viewHolder.minus= (ImageButton)convertView.findViewById(R.id.button_minus) ;
convertView.setTag(viewHolder);
viewHolder.plus.setTag(viewHolder);
viewHolder.minus.setTag(viewHolder);
viewHolder.order_qty.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if(vehicleTransit!= null) {
System.out.println("kamal123" + vehicleTransit.getExpectedDeliveryDate()
.toString());
if (vehicleTransit.getExpectedDeliveryDate() != null) {
viewHolder.uom_desc.setText(vehicleTransit.getUOMDesc()
.toString());
System.out.println("kamal123" +vehicleTransit.getExpectedDeliveryDate()
.toString());
}if (vehicleTransit.getSkuDesc() != null) {
viewHolder.sku_desc.setText(vehicleTransit.getSkuDesc().toString());
}
viewHolder.order_qty.setText(vehicleTransit.getOrderQty()+"");
viewHolder.order_qty.setId(position);
}
viewHolder.plus.setOnClickListener(new View.OnClickListener() {
int count=0;
#Override
public void onClick(View v) {
View p = (View) v.getParent();
ViewHolder holder1 = (ViewHolder) v.getTag();
count = Integer.valueOf(holder1.order_qty.getText().toString());
count++;
holder1.order_qty.setText(String.valueOf(count));
mDataSet.get(position).setOrderQty(count);
//mAdapter.notifyDataSetChanged();
}
});
viewHolder.minus.setOnClickListener(new View.OnClickListener() {
int count=0;
#Override
public void onClick(View v) {
View p = (View) v.getParent();
ViewHolder holder1 = (ViewHolder) v.getTag();
count = Integer.valueOf(holder1.order_qty.getText().toString());
count--;
if(count>=0)
{holder1.order_qty.setText(String.valueOf(count));
mDataSet.get(position).setOrderQty(count);
//mAdapter.notifyDataSetChanged();
}
}
});
//we need to update adapter once we finish with editing
viewHolder.order_qty.setOnFocusChangeListener(new View.OnFocusChangeListener() {
int count=0;
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
View p = (View) v.getParent();
ViewHolder holder1 = (ViewHolder) v.getTag();
final int position = holder1.order_qty.getId();
count = Integer.valueOf(holder1.order_qty.getText().toString());
if(count>0)
{holder1.order_qty.setText(String.valueOf(count));
mDataSet.get(position).setOrderQty(count);
//mAdapter.notifyDataSetChanged();
}
}
}
});
/*viewHolder.order_qty.addTextChangedListener(new TextWatcher() {
int count=0;
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
count = Integer.valueOf(s.toString());
mDataSet.get(position).setOrderQty(count);
}
});*/
return convertView;
}
#Override
public int getCount() {
return mDataSet.size();
}
}
Thanks in advance.
Insert this Line in onCreate before setContentView()
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST);
It will definitely help you
Finally, I figured it out. the problem was trying to solve get tex changed value inside getview. This was re-rendering the list items and I was getting wrong focus and different weird behavious. I have created a textwatcher class and added it while initializing the object, so that it doesn't affect everytime for each item in the list. Below is the code for MyCustom Adapter
public class OrderViewListAdapter extends ArrayAdapter<OrderItemList> {
public OrderViewListAdapter(Context context, int resource) {
super(context, resource);
}
private class ViewHolder {
TextView sku_desc;
TextView uom_desc;
EditText order_qty;
ImageButton plus;
ImageButton minus;
AutoCompleteTextView uom;
MyCustomEditTextListener myCustomEditTextListener;
public ViewHolder(View convertView, MyCustomEditTextListener myCustomEditTextListener) {
this.myCustomEditTextListener = myCustomEditTextListener;
this.order_qty = (EditText) convertView.findViewById(R.id.number_of_item);
this.order_qty.addTextChangedListener(myCustomEditTextListener);
}
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Get the data item for this position
OrderItemList vehicleTransit = mDataSet.get(position);
//System.out.println("kamal123" +mDataSet.get(position).getOrderID());
System.out.println("kamal123" +position);
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_item_selector, parent, false);
viewHolder = new ViewHolder(convertView, new MyCustomEditTextListener());
viewHolder.sku_desc = (TextView) convertView.findViewById(R.id
.item_name);
viewHolder.uom_desc = (TextView) convertView.findViewById(R.id
.uom_desc);
viewHolder.order_qty = (EditText) convertView.findViewById(R.id.number_of_item);
viewHolder.plus= (ImageButton)convertView.findViewById(R.id.button_plus) ;
viewHolder.minus= (ImageButton)convertView.findViewById(R.id.button_minus) ;
convertView.setTag(viewHolder);
viewHolder.plus.setTag(viewHolder);
viewHolder.minus.setTag(viewHolder);
viewHolder.order_qty.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if(vehicleTransit!= null) {
System.out.println("kamal123" + vehicleTransit.getExpectedDeliveryDate()
.toString());
if (vehicleTransit.getExpectedDeliveryDate() != null) {
viewHolder.uom_desc.setText(vehicleTransit.getUOMDesc()
.toString());
System.out.println("kamal123" +vehicleTransit.getExpectedDeliveryDate()
.toString());
}if (vehicleTransit.getSkuDesc() != null) {
viewHolder.sku_desc.setText(vehicleTransit.getSkuDesc().toString());
}
viewHolder.myCustomEditTextListener.updatePosition(position);
viewHolder.order_qty.setText(vehicleTransit.getOrderQty()+"");
viewHolder.order_qty.setId(position);
}
viewHolder.plus.setOnClickListener(new View.OnClickListener() {
int count=0;
#Override
public void onClick(View v) {
View p = (View) v.getParent();
ViewHolder holder1 = (ViewHolder) v.getTag();
count = Integer.valueOf(holder1.order_qty.getText().toString());
count++;
holder1.order_qty.setText(String.valueOf(count));
mDataSet.get(position).setOrderQty(count);
//mAdapter.notifyDataSetChanged();
}
});
viewHolder.minus.setOnClickListener(new View.OnClickListener() {
int count=0;
#Override
public void onClick(View v) {
View p = (View) v.getParent();
ViewHolder holder1 = (ViewHolder) v.getTag();
count = Integer.valueOf(holder1.order_qty.getText().toString());
count--;
if(count>=0)
{holder1.order_qty.setText(String.valueOf(count));
mDataSet.get(position).setOrderQty(count);
//mAdapter.notifyDataSetChanged();
}
}
});
return convertView;
}
private class MyCustomEditTextListener implements TextWatcher {
private int position;
public void updatePosition(int position) {
this.position = position;
}
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// no op
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if (!charSequence.toString().equals("")) {
mDataSet.get(position).setOrderQty(Integer.valueOf(charSequence.toString()));
} else {
mDataSet.get(position).setOrderQty(0);
}
}
#Override
public void afterTextChanged(Editable editable) {
// no op
}
}
#Override
public int getCount() {
return mDataSet.size();
}
}
This question already has answers here:
ListView and Buttons inside ListView
(5 answers)
Closed 8 years ago.
Currently, my listView is sitting inside a fragment. However, the buttons inside my view are not able to receive the on click event whenever I tap on them.
I am not sure what to do.
Here is my code:
public class ViewInvitationsCursorAdapter extends CursorAdapter {
private Activity activity;
private int layout;
private Cursor cr;
private final LayoutInflater inflater;
private Fragment f;
private boolean displayAcceptDeny;
public ViewInvitationsCursorAdapter(Activity activity, Fragment f,
int layout, Cursor c, boolean displayAcceptDeny) {
super(activity, c);
this.layout = layout;
this.activity = activity;
this.inflater = LayoutInflater.from(activity);
this.cr = c;
this.f = f;
this.displayAcceptDeny = displayAcceptDeny;
}
public static class ViewHolder {
public TextView sender;
public TextView sender_email;
public TextView author_role;
public Button deny;
public Button accept;
public Button cancel;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
View view = inflater.inflate(layout, parent, false);
holder.sender = (TextView) view.findViewById(R.id.connection_sender);
holder.sender_email = (TextView) view
.findViewById(R.id.connection_sender_email);
holder.author_role = (TextView) view.findViewById(R.id.connection_role);
holder.accept = (Button) view.findViewById(R.id.connection_allow);
holder.deny = (Button) view.findViewById(R.id.connection_deny);
holder.cancel = (Button) view.findViewById(R.id.connection_cancel);
view.setTag(holder);
return view;
}
#Override
public void bindView(View view, Context context, final Cursor cursor) {
// .bindView(view, context, cursor);
final ViewHolder holder = (ViewHolder) view.getTag();
int sender_index = cursor
.getColumnIndexOrThrow(FamilyDBHelper.COLUMN_SENDER_BY_NAME);
int sender_email_index = cursor
.getColumnIndexOrThrow(FamilyDBHelper.COLUMN_SENDER_BY_EMAIL);
String senderString = cursor.getString(sender_email_index);
int author_role_name = cursor
.getColumnIndexOrThrow(FamilyDBHelper.COLUMN_RELATIONTYPE_NAME);
int message_index = cursor
.getColumnIndexOrThrow(FamilyDBHelper.COLUMN_MESSAGE);
holder.sender.setText(cursor.getString(sender_index));
holder.author_role.setText(cursor.getString(author_role_name));
holder.sender_email.setText(cursor.getString(sender_email_index));
holder.cancel.setVisibility(View.VISIBLE);
// setUpCancel(view, cursor);
holder.cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i("cancel", "cancel clicked!");
}
});
}
protected void setUpAcceptDeny(View view, final Cursor cursor) {
final ViewHolder holder = (ViewHolder) view.getTag();
holder.accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i("accept", "accept clicked!");
}
});
holder.deny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i("deny", "deny clicked!");
}
});
}
}
Here is the xml for each of the items:
<?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:id="#+id/connection_item_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp" >
<LinearLayout
android:id="#+id/connection_item_info_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/connection_sender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"
tools:text="ANDROID: PREFERENCEFRAGMENTCOMPAT" >
</TextView>
<TextView
android:id="#+id/connection_sender_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/connection_sender"
android:textColor="#000000"
android:textSize="14sp"
tools:text="ANDROID: PREFERENCEFRAGMENTCOMPAT" >
</TextView>
<TextView
android:id="#+id/connection_role"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/connection_sender_email"
android:textColor="#000000"
android:textSize="14sp"
>
</TextView>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/connection_item_info_layout"
android:orientation="horizontal"
android:paddingBottom="1.0dip"
android:paddingLeft="4.0dip"
android:paddingRight="4.0dip"
android:paddingTop="5.0dip" >
<Button
android:id="#+id/connection_allow"
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="Allow"
android:background="#color/green"
android:textColor="#color/white"
android:visibility="gone" />
<Button
android:id="#+id/connection_deny"
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:background="#color/red"
android:textColor="#color/white"
android:text="Deny"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/connection_item_info_layout_request"
android:orientation="horizontal"
android:paddingBottom="1.0dip"
android:paddingLeft="4.0dip"
android:paddingRight="4.0dip"
android:paddingTop="5.0dip" >
<Button
android:id="#+id/connection_cancel"
android:layout_width="0.0dip"
android:background="#color/red"
android:textColor="#color/white"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="Cancel Request"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
Add this line to your list item xml file.
android:descendantFocusability="blocksDescendants"
if you set clicklistener inside newView method like this it should work. I generally user getView() for this method and there has never been a problem..
#Override
public View getView(Context context, View View, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View = inflater.inflate(R.layout.your_layout_file_id, parent,
false);
holder.sender = (TextView) view.findViewById(R.id.connection_sender);
holder.sender_email = (TextView) view
.findViewById(R.id.connection_sender_email);
holder.author_role = (TextView) view.findViewById(R.id.connection_role);
holder.accept = (Button) view.findViewById(R.id.connection_allow);
holder.deny = (Button) view.findViewById(R.id.connection_deny);
holder.cancel = (Button) view.findViewById(R.id.connection_cancel);
view.setTag(holder);
holder.cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i("cancel", "cancel clicked!");
}
});
return view;
}
In my project need to edited edit text when tuch listview item and edit text
i am using two textview and one edit text in listview when i use android:focusable="false" and android:focusableInTouchMode="false" in all listview item then; showed listview selection but edit text is not edited.
when i remove android:focusable="false" and android:focusableInTouchMode="false" in edit text then edit text is edited but not selected listview 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="fill_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="horizontal"
android:padding="18dip" >
<TextView
android:id="#+id/txtItemcode"
android:layout_width="110dip"
android:layout_height="fill_parent"
android:layout_marginLeft="10dip"
android:layout_marginTop="9dip"
android:focusable="false"
android:focusableInTouchMode="false"
android:singleLine="true"
android:text="323232"
android:textColor="#0C090A"
android:textSize="20sp" />
<TextView
android:id="#+id/txtItem"
android:layout_width="300dip"
android:layout_height="fill_parent"
android:layout_marginLeft="25dp"
android:layout_marginTop="9dip"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="5456455565456"
android:textColor="#0C090A"
android:textSize="20sp" />
<EditText
android:layout_marginTop="4.5dip"
android:id="#+id/editcreateQuantity"
android:layout_width="100dp"
android:layout_height="50dip"
android:background="#android:drawable/editbox_background"
android:ems="10"
android:inputType="number"
android:singleLine="true"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
my list view
<ListView
android:id="#+id/createlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:clickable="true"
android:divider="#eeeeee"
android:dividerHeight="1dip"
android:scrollingCache="true"
android:smoothScrollbar="true"
android:descendantFocusability="beforeDescendants" >
</ListView>
Here getView Adapter java code
// Create List Adapter
class CreateAdapter extends ArrayAdapter<String> {
String[] strItecode=null;
String[] strItem;
String[] strQuantity;
Context context;
int temp;
CreateAdapter(Context context, String[] strItemcode, String[] strItem,
String[] strQauntity) {
super(context, R.layout.create_list_item, R.id.txtItemcode, strItemcode);
this.context = context;
this.strItecode = strItemcode;
this.strItem = strItem;
this.strQuantity = strQauntity;
// text= new String[strItem.length];
}
private int editingPosition = 0;
private TextWatcher watcher = new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
text[editingPosition] = s.toString();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void afterTextChanged(Editable s) { }
};
public View getView( final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
temp=position;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater
.inflate(R.layout.create_list_item, parent, false);
holder.txtItecode = (TextView) convertView
.findViewById(R.id.txtItemcode);
holder.txtItem = (TextView) convertView
.findViewById(R.id.txtItem);
holder.editQuantity = (EditText) convertView
.findViewById(R.id.editcreateQuantity);
holder.editQuantity.setTag(position);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.editQuantity.removeTextChangedListener(watcher);
if(text[temp].contentEquals("0"))
holder.editQuantity.setText("");
else
holder.editQuantity.setText(text[temp]);
holder.editQuantity.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) editingPosition = position;
}
});
holder.editQuantity.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.requestFocus();
v.requestFocusFromTouch();
}
});
// The rest is good, just make sure to remove the call to setting the EditText's text at the end
holder.txtItecode.setText(strItecode[position]);
holder.txtItem.setText(strItem[position]);
// holder.editQuantity.setText(text[temp]);
return convertView;
}
class ViewHolder {
TextView txtItecode;
TextView txtItem;
EditText editQuantity;
}
}
Please Help me how can edited edit text when i click on list view And click on edit text
Thanks In Advance
I couldn't select an item on ListView. I use Custom Adapter to set content of ListView. I have set OnItemClickListener of ListView. However, it didn't respond. I appriciate your help. Here is my code:
List Item (connections.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="wrap_content"
android:orientation="horizontal"
android:padding="6dip" >
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.90"
android:orientation="vertical" >
<TextView
android:id="#+id/connname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/acc1" />
<TextView
android:id="#+id/conntype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="9sp"
android:text="#string/acctype1" />
</LinearLayout>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
The screen which containt ListView (groupconnections.xml):
<?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"
android:orientation="vertical">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:orientation="vertical"
android:id="#+id/textOperLayout">
<TextView
android:id="#+id/connectionsLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/connections"
android:textColor="#color/conn_text"
android:textSize="25sp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="15dp"
android:background="#color/conn_back"/>
<EditText
android:id="#+id/searchEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/searchhint"
android:layout_marginTop="10dp"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dip"
android:layout_marginBottom="50dip"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_above="#+id/textOperLayout"
android:id="#+id/listviewlayout">
<ListView
android:id="#+id/connectionlist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice" />
</LinearLayout>
<Button
android:id="#+id/addConnCommitButton"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="15dp"
android:text="#string/commitToAdd" />
</RelativeLayout>
The related activity (AddMoreConnections.xml):
public class AddMoreConnections extends Activity implements OnItemClickListener{
private ListView mainListView ;
private ArrayAdapter<AbstractHolder> listAdapter ;
private TextView searchConnTextView;
private Button commitButton;
private ArrayList<AbstractHolder> connlist;
private ArrayList<AbstractHolder> listNotOnView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Generate list View from ArrayLis
setContentView(R.layout.groupconnections);
addListenerOnSearchConnTextView();
//Initialize properties
mainListView = (ListView) findViewById( R.id.connectionlist );
mainListView.setOnItemClickListener(this);
// Create and populate a List of planet names.
listNotOnView = new ArrayList<AbstractHolder>();
connlist = new ArrayList<AbstractHolder>(5);
Iterator<AbstractHolder> iter = SocialRssModel.holders.values().iterator();
while(iter.hasNext())
connlist.add(iter.next());
// Create ArrayAdapter using the planet list.
listAdapter = new CustomAdapter(this,
R.layout.connlist, connlist);
mainListView.setAdapter( listAdapter );
}
public void addListenerToFinishButton(){
commitButton = (Button) findViewById(R.id.addConnCommitButton);
commitButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
public void addListenerOnSearchConnTextView(){
searchConnTextView = (TextView) findViewById(R.id.searchEditText);
searchConnTextView.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int listNotOnViewsize = listNotOnView.size();
int connlistsize = connlist.size();
for(int i= 0; i < connlistsize; i++){
if(!connlist.get(i).connNameContains(s.toString())){
listNotOnView.add(connlist.remove(i));
i--;
connlistsize--;
}
}
for(int i=0; i < listNotOnViewsize; i++){
if(listNotOnView.get(i).connNameContains(s.toString())){
connlist.add(listNotOnView.remove(i));
i--;
listNotOnViewsize--;
}
}
((CustomAdapter) listAdapter).updateList(connlist);
listAdapter.notifyDataSetChanged();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
AbstractHolder temp = (AbstractHolder)mainListView.getItemAtPosition(arg2);
Intent i = new Intent(this, CategoryContentViewerController.class);
Bundle b = new Bundle();
b.putInt("AbstractHolderKey", temp.getId());
i.putExtras(b);
startActivity(i);
finish();
}
private class CustomAdapter extends ArrayAdapter<AbstractHolder> {
private ArrayList<AbstractHolder> connectionList;
public CustomAdapter(Context context, int textViewResourceId, ArrayList<AbstractHolder> connList) {
super(context, textViewResourceId, connList);
this.connectionList = new ArrayList<AbstractHolder>();
this.connectionList.addAll(connList);
}
public void updateList(ArrayList<AbstractHolder> connList){
connectionList.clear();
connectionList.addAll(connList);
}
private class ViewHolder {
TextView name;
TextView acctype;
CheckBox sel;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)(((Activity)this.getContext()).getSystemService(Context.LAYOUT_INFLATER_SERVICE));
convertView = vi.inflate(R.layout.connlist, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.connname);
holder.acctype = (TextView) convertView.findViewById(R.id.conntype);
holder.sel = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
AbstractHolder conn = connectionList.get(position);
holder.name.setText(conn.getName());
holder.acctype.setText(conn.getConntype());
holder.sel.setChecked(conn.isSelected());
holder.sel.setTag(conn);
return convertView;
}
}
}
It is related to checkbox item in connections.xml which is the list row item. If I remove checkbox, related listeners responses. I think, it may be thought there is no need a setItemOnClickListener(..) method since each row has a checkbox. Or it is a bug.