本文介绍了改变一个TextView的文本一个ListView内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在做一个简单的待办事项列表,我希望能够点击的项目,并进入一个新的文本,然后将取代在该单元格中的TextView的文本。我已经得到了dialogAlert工作,我只是不知道如何抓住细胞的TextView的,并改变它
这是活动的样子,
公共类MainActivity扩展AppCompatActivity { 私人ListDataSource DS;
私人的ListView listViewToDo; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
最后上下文的背景下=这;
Log.d(MainActivity,试图创建数据源);
尝试{
DS =新ListDataSource();
}
赶上(例外五)
{
e.printStackTrace();
Log.d(MainActivity,无法创建数据源);
}
Log.d(主要活动,尝试空单视图查看屏幕上的链接);
listViewToDo =(ListView控件)findViewById(R.id.listOfLists);
Log.d(主要活动,查看联系在一起,试图设置适配器的ListView);
listViewToDo.setAdapter(新ListDataSourceAdapter(这一点,DS));
Log.d(主要活动,成功设置适配器); //添加按钮监听器
listViewToDo.setOnItemClickListener(新AdapterView.OnItemClickListener()
{
@覆盖
公共无效onItemClick(适配器视图<>母公司,观景,最终诠释的地位,长期ID)
{
AlertDialog.Builder editItem =新AlertDialog.Builder(背景);
最终的EditText的EditText =新的EditText(背景);
editItem.setTitle(更改项目);
editItem
.setMessage(设置新的待办事项)
.setView(EditText上)
.setCancelable(假)
.setPositiveButton(提交,新DialogInterface.OnClickListener()
{
公共无效的onClick(DialogInterface对话框,INT ID)
{
//我该怎么放在这里?
}
})
.setNegativeButton(取消,新DialogInterface.OnClickListener()
{
公共无效的onClick(DialogInterface对话框,INT ID)
{
dialog.cancel();
}
});
AlertDialog alertDialog = editItem.create();
alertDialog.show();
}
});
}
}
解决方案
ds.remove(位置);
ds.add(位置,edittext.getText()。的toString()。修剪())
ListDataSourceAdapter适配器=新ListDataSourceAdapter(这一点,DS)
listViewToDo.setAdapter(适配器);
adapter.notifDataSetChanged();
I'm making a simple to do list and I want to be able to click on the item and enter a new text which will then replace the text of the TextView in that cell. I've got the dialogAlert working, I just don't know how to grab the cell's TextView and change it
This is what the Activity looks like,
public class MainActivity extends AppCompatActivity {
private ListDataSource ds;
private ListView listViewToDo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Context context = this;
Log.d("MainActivity","Attempting to create data source");
try {
ds = new ListDataSource();
}
catch(Exception e)
{
e.printStackTrace();
Log.d("MainActivity","Failed to create data source");
}
Log.d("Main Activity","Attempting to link empty list view to on screen view");
listViewToDo = (ListView)findViewById(R.id.listOfLists);
Log.d("Main Activity","Views linked, Attempting to set adapter to listView");
listViewToDo.setAdapter(new ListDataSourceAdapter(this, ds));
Log.d("Main Activity", "Successfully set Adapter");
// add button listener
listViewToDo.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id)
{
AlertDialog.Builder editItem = new AlertDialog.Builder(context);
final EditText edittext = new EditText(context);
editItem.setTitle("Change Item");
editItem
.setMessage("Set new todo item")
.setView(edittext)
.setCancelable(false)
.setPositiveButton("Submit", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
//what do I put here?
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alertDialog = editItem.create();
alertDialog.show();
}
});
}
}
解决方案
ds.remove(position);
ds.add(position,edittext.getText().toString().trim())
ListDataSourceAdapter adapter = new ListDataSourceAdapter(this, ds)
listViewToDo.setAdapter(adapter );
adapter.notifDataSetChanged();
这篇关于改变一个TextView的文本一个ListView内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!