2012-07-30 2 views
0

내 코드를 살펴 본다면. ListActivity에서 버튼이 눌려 졌을 때 (이 경우 위치 0) 표시 할 경고가 필요합니다. 눌렀을 때 사용자가 새로운 카테고리를 만들 수 있도록 경고를 표시합니다. 사용자가 원하는 문자열을 범주로 가져와 arraylist에 추가해야합니다.경고에서 arraylist에 값을 추가해야합니다.

public class Data extends ListActivity { 
ArrayList<String> items = new ArrayList<String>(); 
private Context show; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    items.add("+ Create New"); 
    setListAdapter(new ArrayAdapter<String>(Data.this, 
      android.R.layout.simple_list_item_1, items)); 

} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    super.onListItemClick(l, v, position, id); 
    if (position == 0) { 
     items.add(getText()); 

    } 

protected void getText() { 

    AlertDialog.Builder alert = new AlertDialog.Builder(this); 

    alert.setTitle("Title"); 
    // Set an EditText view to get user input 
    final EditText input = new EditText(this); 
    alert.setView(input); 

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      Editable value = input.getText(); 

      // Need to add value to arraylist! 
     } 
    }); 
    alert.setNegativeButton("Cancel", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        // Canceled. 
       } 
      }); 

    alert.show(); 
} 

가}

......

내가 그것을 알아 냈 T_T 내가 시간 동안 노력 해왔다 도와주세요.

protected void setCategory() { 
    AlertDialog.Builder alert = new AlertDialog.Builder(this); 
    alert.setTitle("New Category"); 
    // Set an EditText view to get user input 
    final EditText input = new EditText(this); 
    alert.setView(input); 

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     //create a button that says ok, and can be pressed 
     public void onClick(DialogInterface dialog, int whichButton) { 
      Editable value = input.getText(); 
      getValue(value); 
      //getValue() allows the string to be taken out of this method 
      items.add(output);//put the string into the global variable 
    /* 
    * I dont understand why this way works over making "value" a string and    then    adding it 
    * as the global variable. 
    */ 
     } 
    }); 
    alert.setNegativeButton("Cancel", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        //do nothing 
       } 
      }); 
    alert.show(); 
} 
protected void getValue(Editable theInput) { 
    String input = theInput.toString(); 
    output = input; 
} 

} 이제 어댑터 목록에서

답변

0
private ArrayAdapter<String> adapter; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    items.add("+ Create New"); 
    adapter = new ArrayAdapter<String>(Data.this, 
      android.R.layout.simple_list_item_1, items) 
    setListAdapter(adapter); 

} 

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
     String inputText = input.getText().toString() 
     adapter.add(inputText); 
     /* if need add to items 
     items.add(inputText); 
     */ 
     } 
}); 
+0

흠 .... 나는 아직도 arraylist 항목을 유지합니까? 또는 아니오? –

+0

예, 이것이 작동하지 않습니다 .... 내 목록이 더 이상 나타나지 않습니다. –

+0

당신은 더 많은 세부 정보를 사용할 수 있습니까? – silentnuke

관련 문제