I have a ListView with only TextView. I want an implementation in which, if I click on the ListView line, an edittext appears with a replace button, and all I type in this edittext and click "replace", listrow should update. My main problem I am facing is to inflate the layout on the list bar. Can someone tell me how to do this?
public class ContextMenuActivity extends Activity { private ListView list; TextView tv; ArrayList<String> alistItems; int loopCount; CustomAdapter adapter; LayoutInflater inflater; TextView textView; EditText edtTextToReplace; RelativeLayout rl_inflate; View child; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.contextmenulist); initComponents(); setActionListener(); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("Options"); menu.add(0, v.getId(), 0, "Edit"); menu.add(0, v.getId(), 0, "Delete"); } private void initComponents() { inflater = ContextMenuActivity.this.getLayoutInflater(); list = (ListView) findViewById(R.id.contextmenu_lst_list); tv = (TextView) findViewById(R.id.listitem_txt_item); alistItems = new ArrayList<String>(); for (loopCount = 1; loopCount < 30; loopCount++) { alistItems.add("Item " + loopCount); } prepareView(); } private void prepareView() { adapter = new CustomAdapter(getApplicationContext(), R.layout.listitem, alistItems); list.setAdapter(adapter); registerForContextMenu(list); } private void setActionListener() { list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, final View view, final int arg2, long arg3) { System.out.println("alist is " + alistItems); textView = (TextView) view.findViewById(R.id.listitem_txt_item); rl_inflate = (RelativeLayout) view .findViewById(R.id.rl_inflate); child = getLayoutInflater().inflate(R.layout.clicklistitem, null); rl_inflate.addView(child); textView.setVisibility(View.GONE); rl_inflate.setVisibility(View.VISIBLE); Button my_btn = (Button) child .findViewById(R.id.clicklistitem_btn_replace); edtTextToReplace = (EditText) child .findViewById(R.id.clicklistitem_edt); my_btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { textView.setText(edtTextToReplace.getText().toString()); alistItems.set(arg2, edtTextToReplace.getText() .toString()); rl_inflate.setVisibility(View.GONE); rl_inflate.removeViewInLayout(child); textView.setVisibility(View.VISIBLE); adapter.notifyDataSetChanged(); } }); } }); } public class CustomAdapter extends ArrayAdapter<String> { ArrayList<String> alistItems; int resource; public CustomAdapter(Context context, int resource, ArrayList<String> alistItems) { super(context, resource); this.alistItems = alistItems; this.resource = resource; } @Override public int getCount() { return alistItems.size(); } @Override public View getView(int position, View convertView, ViewGroup parent) { Holder holder = new Holder(); if (convertView == null) { convertView = inflater .inflate(R.layout.listitem, parent, false); } holder.tvRow = (TextView) convertView .findViewById(R.id.listitem_txt_item); convertView.setTag(holder); holder = (Holder) convertView.getTag(); holder.tvRow.setText(alistItems.get(position)); return convertView; } } class Holder { TextView tvRow; } }
source share