2011-10-08 70 views
1
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button button1 = (Button) findViewById(R.id.button1); 
    button1.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      EditText nameText = (EditText) findViewById(R.id.editText1); 
      name = nameText.getText().toString(); 
      nameText.setText(""); 
      EditText numberText = (EditText) findViewById(R.id.editText2); 
      number = numberText.getText().toString(); 
      numberText.setText(""); 
      person = new Person(name, number); 
      order.add(person); 

      PersonAdapter pa = new PersonAdapter(con, R.layout.row, order); 
      setListAdapter(pa); 
     } 
    }); 
} 

@Override 
protected void onListItemClick(ListView l, View v, final int position, long id) { 
    // TODO Auto-generated method stub 
    final int index = position; 
    AlertDialog.Builder aDialog = new AlertDialog.Builder(this); 
    aDialog.setTitle("What do you want??"); 
    aDialog.setPositiveButton("Delete", 
      new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        order.remove(index); 
        PersonAdapter pa = new PersonAdapter(con, R.layout.row, 
          order); 
        setListAdapter(pa); 
       } 
      }); 

    aDialog.setNeutralButton("Edit", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 

      LayoutInflater inflater = (LayoutInflater) con.getSystemService(LAYOUT_INFLATER_SERVICE); 
      View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.root_layout)); 
      builder = new AlertDialog.Builder(con); 
      builder.setView(layout); 
      builder.setTitle("Fill EditText"); 
      builder.setIcon(R.drawable.icon); 
      editNum = (EditText)findViewById(R.id.editNum); 
      editName = (EditText)findViewById(R.id.editName); 
      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 

        name = editName.getText().toString(); 
        number = editNum.getText().toString(); 

        order.set(position, new Person(name,number)); 

        PersonAdapter pa = new PersonAdapter(con, R.layout.row, order); 
        setListAdapter(pa); 
       } 
      }); 

      builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
       } 
      }); 

      alertDialog = builder.create(); 
      alertDialog.show(); 

     } 
    }); 

    aDialog.setNegativeButton("Cancel", 
      new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 

       } 
      }); 
    AlertDialog ad = aDialog.create(); 
    ad.show(); 
} 


public PersonAdapter(Context context, int textViewResourceId, 
     ArrayList<Person> items) { 
    super(context, textViewResourceId, items); 
    this.items = items; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if (v == null) { 
     LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
       Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.row, null); 
    } 

    Person p = items.get(position); 
    if (p != null) { 
     TextView tt = (TextView) v.findViewById(R.id.android_toptext); 
     TextView bt = (TextView) v.findViewById(R.id.android_bottomtext); 
     if (tt != null) { 
      tt.setText(p.getName()); 
     } 
     if (bt != null) { 
      bt.setText("TEL : " + p.getNumber()); 
     } 
    } 
    return v; 
} 

private String name; 
private String number; 

public Person(String name, String number) { 
    this.name = name; 
    this.number = number; 
} 

public String getName() { 
    return name; 
} 

public String getNumber() { 
    return number; 
} 

}當我使用「.getText()。toString();」

我練的Widget(恰好我正在定製對話框)和監聽器的Android。但我現在停止,因爲「.getText()。toString();」使錯誤...我不明白.. T^T請幫助我...爲什麼它會出錯?我認爲它沒有問題

+0

你做了什麼錯誤...? –

+0

應用程序XXXXX意外停止。請再試一次.... – Kong

回答

1

你應該聲明你EditText全球

EditText editName; 
EditText editNum; 

@Override 
    public void onCreate(Bundle savedInstanceState) { 
... 
} 

.....

@Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 

      LayoutInflater inflater = (LayoutInflater) con.getSystemService(LAYOUT_INFLATER_SERVICE); 
      View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.root_layout)); 
      builder = new AlertDialog.Builder(con); 
      builder.setView(layout); 
      builder.setTitle("Fill EditText modificated"); 
      builder.setIcon(R.drawable.icon); 
      editName = (EditText)findViewById(R.id.editName); 
      editNum = (EditText)findViewById(R.id.editNum); 
+0

它也犯了錯誤...雖然我修改了它... T^T我編輯源它是完整的源... – Kong

+0

你在Logcat中得到了什麼錯誤..? –

+0

它運行良好..但「應用程序XXXXX意外停止,請重試。」出現在AVD ... – Kong