Replacing ListView string with another onClick layout

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"); /* * MenuInflater inflater = getMenuInflater(); * inflater.inflate(R.menu.context_menu, menu); */ } 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; } } 
+4
source share
2 answers

You can achieve this in two ways: -

  • First, in the listview layout, add an EditText and a button and hide them. Now set the onItemClickListener of the list in which the text view is hidden and the editext and replace button are displayed.

  • Secondly, create a new layout with the edit text and button and set onItemClickListener to view the list, and when you click on a line, you can easily inflate this layout into your list item layout.

Second solution code:

inflate.xml

 <EditText android:id="@+id/enter_txt" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_replace" android:layout_width="fill_parent" android:layout_height="wrap_content" /> 

* list_view_item.xml *

 <TextView android:id="@+id/my_txt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="ABCD" /> <RelativeLayout android:id="@+id/rl_inflate" android:layout_width="fill_parent" android:layout_height="wrap_content" /> 

listview onItemClickListener

  listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { TextView txt_view = (TextView)view.findViewById(R.id.my_txt); txt_view.setVisibility(View.GONE); RelativeLayout rl_inflate = (RelativeLayout)view.findViewById(R.id.rl_inflate); View child = getLayoutInflater().inflate(R.layout.inflate); rl_inflate.addView(child); Button my_btn = (Button)child.findViewById(R.id.btn_replace); EditText enter_txt = (EditText)child.findViewById(R.id.enter_txt); my_btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { txt_view.setText(enter_txt.getText().toString()); txt_view.setVisibility(View.VISIBLE); } }); } }); 
+9
source

You can go with either of the two solutions below:

  • Turn on the edittext and replace button along with the textview layout, which should be in a hidden state. When you click on a list item, change the visibility of the edit text and the button to a note and change the value of textview to Gone. When you click on the replace button, change the visibility.

  • When you click on a list item, display a warning window with the edittext and replace buttons. After changing the contents and clicking the Replace button, change the contents of the text field to the changed value.

0
source

Source: https://habr.com/ru/post/1502539/


All Articles