2016-01-30 56 views
2

我是Android新手,並構建了一個簡單的待辦事項列表。 在Alert Builder View的自定義佈局中,我想從此自定義佈局中檢索兩個EditText字段中的用戶輸入。在自定義Alert Builder視圖佈局中未從EditText獲取文本

這是自定義視圖:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="118dp" 
     android:layout_height="60dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:text="@string/describe" 
     android:textColor="#A4C639" /> 

    <EditText 
     android:id="@+id/task_description" 
     android:layout_width="155dp" 
     android:layout_height="wrap_content" 
     android:gravity="top|left" 
     android:inputType="textMultiLine|textAutoComplete|textAutoCorrect|textCapSentences" 
     android:lines="4" 
     android:minLines="2" 
     android:hint="@string/text_hint" 
     android:imeOptions="actionNext|actionDone" 
     android:scrollbars="horizontal" /> 
    <requestFocus /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="118dp" 
      android:layout_height="92dp" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:text="@string/when" 
      android:textColor="#A4C639" /> 

     <EditText 
      android:id="@+id/task_date" 
      android:layout_width="155dp" 
      android:layout_height="wrap_content" 
      android:gravity="top|left" 
      android:inputType="date" 
      android:imeOptions="actionNext|actionDone" 
      android:scrollbars="horizontal" /> 
     <requestFocus /> 

    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="1"> 

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:text="@string/task_relevance" 
      android:textColor="#A4C639" /> 

     <CheckBox 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/task_favourite" 
      /> 
     <!-- 
     <Button 
      android:layout_width="64dp" 
      android:layout_height="wrap_content" 
      android:text="@string/save" 
      android:id="@+id/describetaskButton" 
      android:onClick="saveEntryClickFunction" 
      android:layout_weight="0.14" />--> 
    </LinearLayout> 


</LinearLayout> 

這是主要活動:

package com.example.TodoList; 

import android.app.AlertDialog; 
import android.app.ListActivity; 
import android.content.ContentValues; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.EditText; 
import android.widget.ListAdapter; 
import android.widget.SimpleCursorAdapter; 
import android.widget.TextView; 
import com.example.TodoList.db.TaskContract; 
import com.example.TodoList.db.TaskDBHelper; 
import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.api.GoogleApiClient; 
import static com.example.TodoList.R.id.task_description; 
import static com.example.TodoList.db.TaskContract; 

public class MainActivity extends ListActivity { 
    private ListAdapter listAdapter; 
    private TaskDBHelper helper; 
    private Toolbar toolbar; 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     final EditText inputField = new EditText(this); 
     builder.setNegativeButton("Abbrechen", null); 

     switch (item.getItemId()) { 
      case R.id.action_detail_task: 
       Intent TaskActivityIntent = new Intent(getApplicationContext(), 
         TaskActivity.class); 
       startActivity(TaskActivityIntent); 
       return true; 

      case R.id.action_add_task: 
       LayoutInflater inflater = (this).getLayoutInflater(); 
       builder.setTitle("Eine Aufgabe hinzufügen"); 
       builder.setMessage("Was möchten Sie erledigen? Bitte benennen Sie die Aufgabe"); 
       builder.setView(R.layout.custom_view); 
       final View view=inflater.inflate(R.layout.custom_view, null); 

       builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) { 
         View v = (View) view.getParent(); 
         final EditText taskDescription; 
         taskDescription = (EditText)view.findViewById(R.id.task_description); 
         taskDescription.getText().toString().trim(); 
         Log.d("EditText TaskDate", taskDescription.getText().toString().trim()); 

         final EditText taskDate; 
         taskDate = (EditText) view.findViewById(R.id.task_date); 
         taskDate.getText().toString().trim(); 
         Log.d("EditText TaskDate", taskDate.getText().toString().trim()); 
         final CheckBox taskFavourite; 
         taskFavourite = (CheckBox) view.findViewById(R.id.task_favourite); 
         if (taskFavourite.isChecked()) { 
          helper = new TaskDBHelper(MainActivity.this); 
          SQLiteDatabase db = helper.getWritableDatabase(); 
          ContentValues values = new ContentValues(); 
          values.clear(); 
          values.put(Columns.FAVOURITE, String.valueOf(taskFavourite)); 
          db.insertWithOnConflict(TaskContract.TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE); 
         } else { 
          Log.d("Task Activity", "Checkbox is not checked"); 
         } 
         Log.d("Task Text", taskDescription.getText().toString().trim()); 
         Log.d("Task Date", taskDate.getText().toString().trim()); 
         helper = new TaskDBHelper(MainActivity.this); 
         SQLiteDatabase db = helper.getWritableDatabase(); 
         ContentValues values = new ContentValues(); 
         values.clear(); 
         values.put(TaskContract.Columns.TASK_DESCRIPTION, task_description); 
         values.put(Columns.DATE, String.valueOf(taskDate)); 
         //values.put(Columns.FAVOURITE, String.valueOf(taskFavourite)); 
         db.insertWithOnConflict(TaskContract.TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE); 
         updateUI(); 
         db.close(); 
        } 
       }); 
       builder.create().show(); 
       return true; 


      case R.id.action_remove_task: 
       builder.setTitle("Eine Aufgabe entfernen"); 
       builder.setMessage("Wurde die Aufgabe bereits erledigt?"); 
       View checkBoxView = View.inflate(this, R.layout.checkbox, null); 
       CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkbox); 
       builder.setView(checkBoxView); 
       builder.setNegativeButton("Entfernen", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) { 
         String task = inputField.getText().toString(); 
         String sql = String.format("DELETE FROM %s WHERE %s = '%s'", 
           TABLE, 
           Columns.TASK, 
           Columns.TASK_DESCRIPTION, 
           Columns.TASK_STATUS, 
           Columns.DATE, 
           Columns.FAVOURITE, 
           task); 
         helper = new TaskDBHelper(MainActivity.this); 
         SQLiteDatabase sqlDB = helper.getWritableDatabase(); 
         sqlDB.execSQL(sql); 
         sqlDB.close(); 
         updateUI(); 
        } 
       }); 
       builder.create().show(); 
       return true; 
      default: 
       return false; 
     } 
    } 

    public void onDoneCheckBoxClick(View view) { 
     Log.d("onDoneCheckBoxClick", "First Check Box Click Function"); 
     View v = (View) view.getParent(); 
     TextView taskTextView = (TextView) v.findViewById(R.id.taskTextView); 
     helper = new TaskDBHelper(MainActivity.this); 
     SQLiteDatabase sqlDB = helper.getWritableDatabase(); 
     String task = taskTextView.getText().toString(); 
     String sql = String.format("DELETE FROM %s WHERE %s = '%s'", 
       TABLE, 
       TaskContract.Columns.TASK, 
       task); 

     sqlDB.execSQL(sql); 
     sqlDB.close(); 
     updateUI(); 
    } 

    public void DetailClick(View view) { 
     Intent TaskActivityIntent = new Intent(getApplicationContext(), 
       TaskActivity.class); 
     startActivity(TaskActivityIntent); 
    } 

    private void updateUI() { 
     helper = new TaskDBHelper(MainActivity.this); 
     SQLiteDatabase sqlDB = helper.getReadableDatabase(); 
     Cursor cursor = sqlDB.query(TABLE, 
       new String[]{Columns._ID, Columns.TASK, Columns.DATE, 
         Columns.TASK_STATUS 
         }, 
       null, null, null, null, null 
     ); 


     listAdapter = new SimpleCursorAdapter(
       this, 
       R.layout.task_view, 
       cursor, 
       new String[]{Columns.TASK, Columns.DATE, Columns.TASK_STATUS}, 
       new int[]{R.id.taskTextView,R.id.taskDateView,R.id.task_favourite}, 
       0 
     ); 

     this.setListAdapter(listAdapter); 

    } 

    public void onDetailViewClick(View view) { 
     Intent TaskActivityIntent = new Intent(getApplicationContext(), 
       TaskActivity.class); 
     startActivity(TaskActivityIntent); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client.connect(); 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "Main Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app deep link URI is correct. 
       Uri.parse("android-app://com.example.TodoList/http/host/path") 
     ); 
     AppIndex.AppIndexApi.start(client, viewAction); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "Main Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app deep link URI is correct. 
       Uri.parse("android-app://com.example.TodoList/http/host/path") 
     ); 
     AppIndex.AppIndexApi.end(client, viewAction); 
     client.disconnect(); 
    } 
} 

我無法接收在任何logcat中記錄從輸入的EditText。 任何提示或幫助將不勝感激!

回答

1

您使用的視圖與對話框中的視圖不同。 嘗試將dialogInterface對話框對話框:

taskDescription = (EditText) ((Dialog) dialogInterface).findViewById(R.id.task_description); 
+0

謝謝!這非常有幫助。 –

相關問題