0

只要有ListViewEditText是很难搞清楚,我试过另一种解决方案:一个ListViewTextViews,当你点击一个,我赶上聚焦TextView向监听器(父ListView一个OnItemClickListener)和我开一个AlertDialog如何从AlertDialog中更改值?

ISSUE:当我按下了AlertDialog OK按钮,我想聚焦TextView得到的警告对话框中的EditText的文本值,但它不工作,他保持相同的文本值。 On activity:

 listView.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> a, View v, int pos, long id){ 

      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); 

      id ++; 
      t = new TextView(activity); 
      t = (TextView) v; 

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

      alert.setTitle("Title"); 
      alert.setMessage("Message"); 

      // Set an EditText view to get user input 
      final EditText input = new EditText(activity); 
      input.setText(t.getText()); 
      alert.setView(input); 

      alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 

        //Changer la valeur dans la base et dans la liste 


        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS,0); 

        t.setText(input.getText()); 

        System.out.println("input avant : " + t.getText()); 
       } 
      }); 
      alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        // Canceled. 
       } 
      }); 


      alert.show(); 
     } 
    }); 

我认为这段代码足以让你知道我的代码中有什么问题。只是为了让你知道:

  • ListViewTextView)被声明为在活动类私有变量
  • 有一个Adapter类,如果我改变自动构建TextView列表
  • ListView侦听器上的EditText的值,它可以工作,但在对话框警报侦听器中,它不会。

询问您是否需要更多信息。

回答

2

我不知道你是如何构建适配器的,所以我会猜测答案。您必须将文本放入适配器数据中(例如,在提供给适配器的ArrayList中),并在适配器上调用notifyDataSetChanged()

//ArrayList field on which your adapter is based ?!?(if you used this) 
private ArrayList<String> items; 

listView.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> a, final View v, int pos, long id){ 

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

      alert.setTitle("Title"); 
      alert.setMessage("Message"); 
      final EditText input = new EditText(activity); 
      input.setText(t.getText()); 
      alert.setView(input); 

      alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        items.set(pos, input.getText().toString()); 
        adapterObject.notifyDataSetChanged(); // the adapter you set in the listView.setAdapter(); 
       } 
      }); 
      alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        // Canceled. 
       } 
      }); 


      alert.show(); 
     } 
    }); 
+0

哦,我的天啊你的权利。这就像我做了一个自定义适配器,但不知道如何使用它。无论如何,我开始在Android上。谢谢 ! – 2012-03-08 06:41:58

+0

@SebastienFERRAND如果你想要的是一个'TextView'一个排了,你会用一个简单的'ArrayAdapter '更好。自定义适配器适用于您想要自定义行布局或具有更复杂的数据结构。 – Luksprog 2012-03-08 06:47:37

+0

是啊,你是对的,谢谢你的建议,但我需要它在未来更加复杂 – 2012-03-08 06:57:38