2015-12-07 144 views
2

我正在做一個簡單的待辦事項列表,我希望能夠點擊該項目並輸入一個新文本,然後將替換該單元格中TextView的文本。我已經得到了dialogAlert的工作,我只是不知道如何抓住細胞的TextView的,並改變它更改ListView中的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(); 
      } 
     }); 
    } 
} 
+0

嘗試使用,而不是RecyclerView列表顯示。 –

回答

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