在ListView中更改TextView的文本

时间:2015-12-07 00:49:15

标签: java android alertdialog

我正在制作一个简单的待办事项清单,我希望能够点击该项目并输入一个新文本,然后替换该单元格中TextView的文本。我已经让dialogAlert工作了,我只是不知道如何抓住单元格的TextView并进行更改

这就是活动的样子,

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();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

ds.remove(position);
ds.add(position,edittext.getText().toString().trim())
ListDataSourceAdapter adapter = new ListDataSourceAdapter(this, ds)
listViewToDo.setAdapter(adapter );
adapter.notifDataSetChanged();