Showing posts with label Custom Listview. Show all posts
Showing posts with label Custom Listview. Show all posts

Thursday 6 February 2014

Android : Custom Listview with EditText in it and Managing data of Editext


In One of my application we required to have EditText in Listview and i need to manage hot to fetch data from particular Editext when user done Editing after Using TextWatcher interface i can able to do what i required .

So I am sharing my code with you people ,if any one have better approach to work with Edittext in Listview and manage it's data as well then you can share your idear and implementation as well.


As my screen contain only list so i prefer to use List Activity.





public class ListActivity extends android.app.ListActivity {
private ListView mListview;
private final int TOTAL_ITEMS=100; 
private BaseListAdapter mBaseListAdapter;
ArrayList<String> mArrayList=new ArrayList<String>(TOTAL_ITEMS);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_list);
init();
}
private void init(){
mListview=getListView();
for(int i=0;i<TOTAL_ITEMS;i++){
mArrayList.add("");
}
mBaseListAdapter=new BaseListAdapter(ListActivity.this, mArrayList);
mListview.setAdapter(mBaseListAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
return true;
}

}


Other class required is my Listview adapter class which is below .

public class BaseListAdapter extends BaseAdapter implements OnTouchListener,TextWatcher{
private Context mContext;
private ArrayList<String> mArrayEdittxt;
private LayoutInflater mLayoutInflater;
private int mPosition;
private boolean isTouch=false;
public BaseListAdapter(Context _Context,ArrayList<String> mArrayEditTxtData){
this.mContext=_Context;
mArrayEdittxt=mArrayEditTxtData;
mLayoutInflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return mArrayEdittxt.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder;
if(convertView==null){
mViewHolder=new ViewHolder();
convertView=mLayoutInflater.inflate(R.layout.list_row_items, parent, false);
mViewHolder.mEditxt=(EditText)convertView.findViewById(R.id.editxt);
mViewHolder.mImgView=(ImageView)convertView.findViewById(R.id.imgview);
convertView.setTag(mViewHolder);
}else{
mViewHolder=(ViewHolder) convertView.getTag();
}
isTouch=false;
mViewHolder.mEditxt.addTextChangedListener(BaseListAdapter.this);
mViewHolder.mEditxt.setOnTouchListener(BaseListAdapter.this);
mViewHolder.mEditxt.setTag(position);
mViewHolder.mEditxt.setText(mArrayEdittxt.get(position));
return convertView;
}
public class ViewHolder{
EditText mEditxt;
ImageView mImgView;
}


@Override
public boolean onTouch(View v, MotionEvent event) {
Log.v("LOG_TAG", "onTouch-->");
isTouch=true;
mPosition=(Integer) v.getTag();
return false;
}



@Override
public void afterTextChanged(Editable s) {
Log.v("LOG_TAG", "afterTextChanged -->"+ s.toString() );
if(isTouch){
mArrayEdittxt.set(mPosition, s.toString());
}
}



@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}



@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

}


Below is my listview row xml file .

<?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="wrap_content" >

    <EditText
        android:id="@+id/editxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_toLeftOf="@+id/imgview"
        android:ems="10" >

        <requestFocus />
        
    </EditText>

    <ImageView
        android:id="@+id/imgview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@android:drawable/ic_media_play" />

</RelativeLayout>
If any one want to check my source code they can directly download it from my Google Drive -->
https://drive.google.com/file/d/0B-8An4Rd1nmKNXNDTDRqM2FxWFU/edit?usp=sharing