Android中实现长按修改ListView对象的内容

所属分类: 软件编程 / Android 阅读数: 39
收藏 0 赞 0 分享

实现的效果如下:

我在ListView的Item长按事件内打开一个弹出窗口,窗口内有一个EditText对象,在这个编辑框内输入文本点确定后,直接修改掉ListView对象内某个TextView对象的内容。

示例代码如下:

import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.graphics.Color; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemLongClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TextView; 
 
public class MainActivity extends Activity { 
private ListView lvShow; 
private AlertDialog dialog; 
 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
lvShow = (ListView) findViewById(R.id.lvShow); 
String[] arr = { "李四", "小猪", "店小二" }; 
ArrayAdapter<String> Adap1 = new ArrayAdapter<String>(this, 
R.layout.test_list, arr); 
lvShow.setAdapter(Adap1);// 设置ListView的显示 
lvShow.setOnItemLongClickListener(new OnItemLongClickListener() { 
 
@Override 
public boolean onItemLongClick(AdapterView<?> parent, View view, 
int position, long id) { 
setAlertDialog(view); 
dialog.show(); 
return false; 
} 
}); 
} 
 
private void setAlertDialog(final View view) { 
LayoutInflater factory = LayoutInflater.from(getApplicationContext()); 
// 引入一个外部布局 
View contview = factory.inflate(R.layout.test_dialog, null); 
contview.setBackgroundColor(Color.BLACK);// 设置该外部布局的背景 
final EditText edit = (EditText) contview 
.findViewById(R.id.edit_dialog);// 找到该外部布局对应的EditText控件 
Button btOK = (Button) contview.findViewById(R.id.btOK_dialog); 
btOK.setOnClickListener(new OnClickListener() {// 设置按钮的点击事件 
 
@Override 
public void onClick(View v) { 
((TextView) view).setText(edit.getText().toString()); 
dialog.dismiss(); 
} 
}); 
dialog = new AlertDialog.Builder(MainActivity.this).setView(contview) 
.create(); 
} 
} 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/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:id="@+id/lvShow" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" /> 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:gravity="center_horizontal" 
 android:orientation="vertical" > 
 
 <EditText 
  android:id="@+id/edit_dialog" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:textSize="28sp" /> 
 
 <Button 
  android:id="@+id/btOK_dialog" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="确定" /> 
 
</LinearLayout> 

总结

以上就是这篇文章的全部内容了,希望本文的内容对各位Android开发者们能带来一定的帮助,如果有疑问大家可以留言交流。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多