Android listView with editText : how to edit text? - android

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>

Related

Unable to create sign up page by extending ArrayAdapter with ListView

Here is the code for extended ArrayAdapter class:
public class MultiAdapter extends ArrayAdapter<String> {
private final Activity a1;
private final String[] s1;
public MultiAdapter(Activity a, String[] s){
super(a,R.layout.loginlist,s);
s1=s;
a1=a;
}
#Override
public View getView(int i,View v,ViewGroup vg) {
LayoutInflater li = a1.getLayoutInflater();
View v1 = li.inflate(R.layout.loginlist, null, true);
TextView tv;
tv = (TextView) v1.findViewById(R.id.textView1);
tv.setText(s1[i]);
EditText et;
et = (EditText) v1.findViewById(R.id.editText1);
if (tv.getText().equals("Name")||tv.getText().equals("User Name")||tv.getText().equals("Company")) {
et.setInputType(InputType.TYPE_CLASS_TEXT);
}
if (tv.getText().equals("Phone")) {
et.setInputType(InputType.TYPE_CLASS_PHONE);
}
if (tv.getText().equals("E-mail")||tv.getText().equals("Company E-mail")) {
et.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS);
}
if (tv.getText().equals("Password")||tv.getText().equals("Re-type Password")) {
et.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
return v1;
}
}
loginlist.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
</EditText>
</LinearLayout>
What's happening is that whenever I click on the "Name", "Company" or "User Name" edittext's the cursor automatically jumps to the password and then re-type password edittext field. This does not happen on any other fields. What's causing this?
#Override public View getView(int position, View convertView,
ViewGroup parent) { View row; if (null == convertView) {
row = mInflater.inflate(R.layout.listitem, null); } else
{ row = convertView; } TextView tv =
(TextView)row.findViewById(android.R.id.text1);
tv.setText(items[position]); tv.setTypeface(font);
return row; }
This how you have to implement your getview. Check on null check on convert view. But I would strongly recommend you use linear layout.

setOnFocusChangeListener in ListView item

What I want to do is display actions in ActionBar, if item of ListView is selected.
So I create :
answer_item.xml:
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:weightSum="1">
<EditText android:id="#+id/simple_answer_item"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:singleLine="true"
android:layout_weight="1.0"
android:focusableInTouchMode="true"
android:focusable="true"
/>
<ImageView
android:id="#+id/file_loaded_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
/>
</LinearLayout>
And AnswerAdapter class :
public class AnswerAdapter extends ArrayAdapter<Answer>{
private final LayoutInflater mInflater;
private final Fragment parentFragment;
public AnswerAdapter(Context context, int textViewResourceId,Fragment parentFragment) {
super(context, textViewResourceId);
mInflater = LayoutInflater.from(getContext());
this.parentFragment = parentFragment;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.answer_item, null);
holder = new ViewHolder();
holder.answer = (EditText) convertView.findViewById(R.id.simple_answer_item);
holder.loadObjectImageView = (ImageView) convertView.findViewById(R.id.file_loaded_view);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
final Answer answer = getItem(position);
holder.answer.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
Toast.makeText(getContext(), "click " + position, Toast.LENGTH_SHORT).show();
if (parentFragment != null && parentFragment instanceof onAnswerListItemFocusChange)
((onAnswerListItemFocusChange) parentFragment).onListItemFocusChange(true, answer);
}
else {
Toast.makeText(getContext(), "unclick "+ position, Toast.LENGTH_SHORT).show();
final int position = view.getId();
final EditText answerBody = (EditText) view;
getItem(position).setBody(answerBody.getText().toString());
if (parentFragment != null && parentFragment instanceof onAnswerListItemFocusChange)
((onAnswerListItemFocusChange) parentFragment).onListItemFocusChange(false,answer);
view.clearFocus();
}
}
});
holder.answer.setText(answer.getBody());
holder.answer.setId(position);
return convertView;
}
private static class ViewHolder{
public EditText answer;
public ImageView loadObjectImageView;
}
}
Moreover I have android:descendantFocusability="afterDescendants" in ListView:
<ListView
android:id="#+id/bad_answer_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:descendantFocusability="afterDescendants">
</ListView>
So when I run app and focused EditText at position for exmaple 2 in ListView , I expected to see only "click 2" but in result I get "click 2" "unclick 2" "click 0" "unclick0" "click2"
And becouse of that I think in my ActionBar I see only one action
But normally in EditText not in ListView works fine and I see 3.
So please tell me what I'm doing wrong ?

Show/hide Edittext inside listview becomes invisible when keyboard pops up

I have a custom Listview whose adapter extends BaseAdapter class. Inside every row in listview there is an invisible EditText. it get visible to only particular row which has been click and on rest of the row it remains invisible.Problem comes when I touch edittext to get keyboard, as soon as keyboard appears, listview gets recycle and edittext again become invisible.
I am looking for a solution in which either keyboard comes with focus on edittext as soon as that row is selected or edittext does not disapear when keyboard is poped-up.Following is the adapter I am using:
this is my adapter xml file
saved_option_adapter_content.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/lineItem"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
>
<TextView
android:id="#+id/textLine"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="15sp"
android:layout_marginBottom="5sp"
android:textSize="14sp"
android:paddingTop="-5dp"
android:layout_width="wrap_content"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentRight="true">
<EditText
android:id="#+id/edittext_qty"
android:visibility="invisible"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:singleLine="true"
android:maxLength="4"
android:textSize="14sp"
android:inputType="numberPassword"
android:focusable="true"
android:textColor="#android:color/black"
android:hint="cvv"/>
</LinearLayout>
</RelativeLayout>
and this is my getView Method:
public View getView(final int position, View convertView, final ViewGroup parent)
{
final ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.saved_options_adapter_content, null);
holder = new ViewHolder();
holder.textLine = (TextView) convertView.findViewById(R.id.textLine);
holder.Edittext = (EditText) convertView.findViewById(R.id.edittext_qty);
editTextList.add(holder.Edittext);
convertView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(final View v)
{
selected_Txt="";
if(view==null || view!=v){
v.post(new Runnable() {
#Override
public void run() {
view=v;
ViewHolder holder = ((ViewHolder)v.getTag());
holder.Edittext.setVisibility(View.VISIBLE);
holder.Edittext.requestFocus();
holder.Edittext.setCursorVisible(true);
holder.Edittext.setFocusable(true);
if(selectedHolder != null ){
selectedHolder.Edittext.setVisibility(View.INVISIBLE);
}
selectedHolder = holder;
}
});
}
}
});
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
((ViewHolder)convertView.getTag()).Edittext.setTag(title.get(position));
}
convertView.setTag(holder);
return convertView;
}
I don't see listview in your code. Maybe ArrayAdapter can help you: http://developer.android.com/reference/android/widget/ArrayAdapter.html

Edit Text inside LIstview in 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

Animate background on listview

I have a ListView that have in each row 2 TextView and a CheckBox.
I have a drawable (a line), that I want once the user check the CheckBox that the drawable will be scale as an animation at the background of the checked CheckBox row, I was able in my getView() method on my Custom Adapter, to animate the drawable as a background but it also scale the 2 TextView and the CheckBox, and I want that only the drawable background will be scale as an animation, how do i do that?
here is my getView() method on the custom adapter:
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.listview_item_row, parent, false);
// cache view fields into the holder
holder = new ViewHolder();
holder.itemTitle = (TextView) convertView.findViewById(R.id.itemTitle);
holder.itemQuantity = (TextView) convertView.findViewById(R.id.itemQuantity);
holder.itemCheck = (CheckBox) convertView.findViewById(R.id.itemCheck);
convertView.setTag(holder);
final Cursor cursor = getCursor();
final View rowView = convertView;
cursor.moveToPosition(position);
holder.itemTitle.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_NAME))));
holder.itemQuantity.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_QUANTITY))));
holder.itemCheck.setChecked(cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.KEY_CHECKED)) == 1);
holder.itemCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
cursor.moveToPosition(position);
itemsDataSource.updateItemCheckBox(1, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), true);
Animation drawLine = AnimationUtils.loadAnimation(context, R.anim.item_done);
rowView.setBackgroundResource(R.drawable.line);
rowView.startAnimation(drawLine);
ShowListActivity.PRIORITY++;
}
else {
cursor.moveToPosition(position);
itemsDataSource.updateItemCheckBox(0, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), false);
rowView.setBackgroundResource(0);
ShowListActivity.PRIORITY--;
}
}
});
if (holder.itemCheck.isChecked()) rowView.setBackgroundResource(R.drawable.line);
else rowView.setBackgroundResource(0);
return rowView;
}
private static class ViewHolder {
TextView itemTitle;
TextView itemQuantity;
CheckBox itemCheck;
}
update
I managed to solve the problem:
first I changed the custom row layout file like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frameRowLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="10">
<LinearLayout android:id="#+id/rowLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<CheckBox android:id="#+id/itemCheck"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="2" android:gravity="center"
android:focusable="false"
android:focusableInTouchMode="false"
android:singleLine="false" />
<TextView android:id="#+id/itemTitle"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="6"
android:gravity="center"
android:text="some text1"
android:textColor="#2B89A8"
android:textSize="22dp"
android:textStyle="bold" />
<TextView android:id="#+id/itemQuantity"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="2"
android:gravity="center"
android:text="some text2"
android:textColor="#2B89A8"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
<View
android:id="#+id/backgroundView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
the whole idea of Framelayout is that views can overlap each other, there for the View after the LinearLayout is overlapping the LinearLayout.
then I changed my getView() method in my custom adapter as Following:
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.listview_item_row, parent, false);
// cache view fields into the holder
holder = new ViewHolder();
holder.itemTitle = (TextView) convertView.findViewById(R.id.itemTitle);
holder.itemQuantity = (TextView) convertView.findViewById(R.id.itemQuantity);
holder.itemCheck = (CheckBox) convertView.findViewById(R.id.itemCheck);
holder.rowView = (View) convertView.findViewById(R.id.backgroundView);
convertView.setTag(holder);
final Cursor cursor = getCursor();
cursor.moveToPosition(position);
holder.itemTitle.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_NAME))));
holder.itemQuantity.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_QUANTITY))));
holder.itemCheck.setChecked(cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.KEY_CHECKED)) == 1);
holder.itemCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
cursor.moveToPosition(position);
itemsDataSource.updateItemCheckBox(1, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), true);
Animation drawLine = AnimationUtils.loadAnimation(context, R.anim.item_done);
holder.rowView.setBackgroundResource(R.drawable.line);
holder.rowView.startAnimation(drawLine);
ShowListActivity.PRIORITY++;
}
else {
cursor.moveToPosition(position);
itemsDataSource.updateItemCheckBox(0, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), false);
holder.rowView.setBackgroundResource(0);
ShowListActivity.PRIORITY--;
}
}
});
if (holder.itemCheck.isChecked()) holder.rowView.setBackgroundResource(R.drawable.line);
else holder.rowView.setBackgroundResource(0);
return convertView;
}
private static class ViewHolder {
TextView itemTitle;
TextView itemQuantity;
CheckBox itemCheck;
View rowView;
}
hope you can understand what I did.

Categories

Resources