Edit Text inside LIstview in Android - android

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

Related

ListView EditText values changing on Scrolling also Taking Duplicate Entry

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.

Focus on wrong edit text inside listview

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();
}
}

Android listView with editText : how to edit text?

I created a listview with a textview and an edittext inside of it.
I’ve also done an adapter with an arraylist of string as parameter.
I want to edit the elements of my arraylist with the edittext but it works only with numbers whereas I want to edit text.
When the inputtype is “text”, the “onFocusChange” doesn’t work and I cannot enter text but when the inputtype is number, it works.
Here is an extract of my code :
Adapter :
public View getView(int position, View view, ViewGroup parent) {
ViewHolder viewHolder;
if (view == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.lv_editetext_reponse, null, true);
viewHolder.textView1 = (TextView) view.findViewById(R.id.Item_name);
viewHolder.editText1 = (EditText) view.findViewById(R.id.Item_price);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.textView1.setText(reponses.get(position));
viewHolder.editText1.setId(position);
viewHolder.ref = position;
// Add listener for edit text
viewHolder.editText1
.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
int itemIndex = v.getId();
String enteredPrice = ((EditText) v).getText()
.toString();
reponses.set(itemIndex, enteredPrice);
}
}
});
return view;
}
private class ViewHolder {
TextView textView1;
EditText editText1;
int ref;
}
XML :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/Item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingTop="5dp"/>
<EditText
android:id="#+id/Item_price"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignParentRight = "true"
android:layout_gravity="right"
android:hint="Price"
android:focusable="true"
android:focusableInTouchMode="true"
android:editable="true"
android:lines="1" />
</RelativeLayout>

remove items from listview by pressing delete button android

I want to delete my listview items when I press delete button but the problem is I should first press listview row then press delete button to remove it. I want to delete items immediately after pressing delete button not first press on listview then press delete button
Can you help me in this situation?
and this is my code
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wholeshop);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// getting values from selected ListItem
final View view = v;
final int pos =position;
deleteone= (Button)v.findViewById(R.id.bdeleteone);
deleteone.getTag(position);
deleteone.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
deleteone.setTag(view);
productsList.remove(pos);
ListAdapter adapter = new SimpleAdapter(
wholeShop.this, productsList,
R.layout.listshop, new String[] { "name",
"spinn","price"},
new int[] { R.id.wholename, R.id.wholespinn,R.id.wholeprice });
// updating listview
setListAdapter(adapter);
}
});
}
});
}
and this is my xmlfile for listview:
<?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:background="#ffffff">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="110dp"
android:layout_marginTop="50dp"
android:descendantFocusability="blocksDescendants"
>
</ListView>
and:
<?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:background="#ffffff"
android:weightSum="100">
<TextView
android:id="#+id/wholename"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="40"
android:paddingLeft="5dip"
android:text="Flower Name"
android:textColor="#000000" />
<TextView
android:id="#+id/wholespinn"
android:layout_width="66dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="2dp"
android:paddingRight="10dip"
android:text="Flower spinn"
android:textColor="#000000"
android:layout_weight="30"/>
<TextView
android:id="#+id/wholeprice"
android:text="Flower price"
android:layout_gravity="right"
android:paddingRight="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_weight="30"/>
<Button
android:id="#+id/bdeleteone"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Del"
android:layout_marginRight="10dp"
android:layout_weight="30"
android:focusable="false"
/>
</LinearLayout>
You are going with wrong way.
You may have to look this Example.
You have create Your Own Adapter like:
public class ListAdapter extends ArrayAdapter<String> {
customButtonListener customListner;
public interface customButtonListener {
public void onButtonClickListner(int position,String value);
}
public void setCustomButtonListner(customButtonListener listener) {
this.customListner = listener;
}
private Context context;
private ArrayList<String> data = new ArrayList<String>();
public ListAdapter(Context context, ArrayList<String> dataItem) {
super(context, R.layout.child_listview, dataItem);
this.data = dataItem;
this.context = context;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.child_listview, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView
.findViewById(R.id.childTextView);
viewHolder.button = (Button) convertView
.findViewById(R.id.childButton);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final String temp = getItem(position);
viewHolder.text.setText(temp);
viewHolder.button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (customListner != null) {
customListner.onButtonClickListner(position,temp);
}
}
});
return convertView;
}
public class ViewHolder {
TextView text;
Button button;
}
}

Listview with edit text onitemclick not working in android

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!

Categories

Resources