SoFunction
Updated on 2025-03-11

Implement long press to modify the content of ListView object in Android

The effect achieved is as follows:

I opened a pop-up window in the Item event of ListView. There is an EditText object in the window. After entering the text point in this edit box to confirm, I directly modify the content of a TextView object in the ListView object.

The sample code is as follows:

import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
public class MainActivity extends Activity { 
private ListView lvShow; 
private AlertDialog dialog; 
 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
(savedInstanceState); 
setContentView(.activity_main); 
lvShow = (ListView) findViewById(); 
String[] arr = { "Li Si", "piggy", "Shop Waiter" }; 
ArrayAdapter<String> Adap1 = new ArrayAdapter<String>(this, 
.test_list, arr); 
(Adap1);// Set the display of ListView(new OnItemLongClickListener() { 
 
@Override 
public boolean onItemLongClick(AdapterView<?> parent, View view, 
int position, long id) { 
setAlertDialog(view); 
(); 
return false; 
} 
}); 
} 
 
private void setAlertDialog(final View view) { 
LayoutInflater factory = (getApplicationContext()); 
// Introduce an external layoutView contview = (.test_dialog, null); 
();// Set the background of the external layoutfinal EditText edit = (EditText) contview 
.findViewById(.edit_dialog);// Find the EditText control corresponding to the external layoutButton btOK = (Button) (.btOK_dialog); 
(new OnClickListener() {// Set button click event 
@Override 
public void onClick(View v) { 
((TextView) view).setText(().toString()); 
(); 
} 
}); 
dialog = new ().setView(contview) 
.create(); 
} 
} 
<RelativeLayout xmlns:andro 
 xmlns:tools="/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:paddingBottom="@dimen/activity_vertical_margin" 
 android:paddingLeft="@dimen/activity_horizontal_margin" 
 android:paddingRight="@dimen/activity_horizontal_margin" 
 android:paddingTop="@dimen/activity_vertical_margin" 
 tools:context=".MainActivity" > 
 
 <ListView 
  android: 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" /> 
&lt;?xml version="1.0" encoding="utf-8"?&gt; 
&lt;LinearLayout xmlns:andro 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:gravity="center_horizontal" 
 android:orientation="vertical" &gt; 
 
 &lt;EditText 
  android: 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:textSize="28sp" /&gt; 
 
 &lt;Button 
  android: 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="Sure" /&gt; 
 
&lt;/LinearLayout&gt; 

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to all Android developers. If you have any questions, you can leave a message to communicate.