2015-02-24 64 views
0

我有一个带有十个名字的ListView。我想在点击某个名称时使用自定义提醒,并且希望它可以进行编辑。在这段代码中,我只获取ListView,而不是自定义警报。列表视图使用自定义提醒并在android中编辑名称

public class MainActivity extends Activity { 

    public ListView mainListView; 
    public ArrayAdapter listAdapter; 
    final Context context = this; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Find the ListView resource. 
     mainListView = (ListView) findViewById(R.id.mainListView); 
     // Create and populate a List of planet names. 
     String[] planets = new String[] { "Allu", "Abin", "Bibin", "Aswathy", 
     "Jibin", "Saran", "Jobin", "Neethu","ammu","Ram"}; 
     ArrayList planetList = new ArrayList(); 
     planetList.addAll(Arrays.asList(planets)); 

     // Create ArrayAdapter using the planet list. 
     listAdapter = new ArrayAdapter(this, R.layout.simplerow, planetList); 

     /*// Add more planets. If you passed a String[] instead of a List 
     // into the ArrayAdapter constructor, you must not add more items. 
     // Otherwise an exception will occur. 
     listAdapter.add("Ceres"); 
     listAdapter.add("Pluto"); 
     listAdapter.add("Haumea"); 
     listAdapter.add("Makemake"); 
     listAdapter.add("Eris");*/ 

     // Set the ArrayAdapter as the ListView’s adapter. 
     mainListView.setAdapter(listAdapter); 
     mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       AlertDialog.Builder alert = new AlertDialog.Builder(context); 
       alert.setTitle("Alert Dialog With EditText"); //Set Alert dialog title here 
       alert.setMessage("Edit Your Name Here"); //Message here 

       // Set an EditText view to get user input 
       final EditText input = new EditText(context); 
       alert.setView(input); 

       alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         // You will get as string input data in this variable. 
         // here we convert the input to a string and show in a toast. 
         String srt = input.getEditableText().toString(); 
         Toast.makeText(context,srt,Toast.LENGTH_LONG).show(); 
        } 
       }); 
       //End of alert.setPositiveButton 

       alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         // Canceled. 
         dialog.cancel(); 
        } 
       }); //End of alert.setNegativeButton 
       AlertDialog alertDialog = alert.create(); 
       alertDialog.show(); 
      } 

      public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { 
       // TODO Auto-generated method stub 
      } 
     }); 
    } 
} 
+1

尝试打开然后用cliecked和update来更新列表项的所有值。 – 2015-02-24 07:51:26

+0

将“Allu”,“Abin”,“Bibin”,“Aswathy”, “Jibin”,“Saran”,“Jobin”,“Neethu”,“ammu”,“Ram” “,”Bibin“,”Aswathy“, ”吉宾“,”Saran“,”Jobin“,”Neethu“,”ammu“,”Ram“(您不使用双重quoates.Please更改所有符号为双引号) – user3515851 2015-02-24 08:10:35

回答

2

enter image description here

您在收听

mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       AlertDialog.Builder alert = new AlertDialog.Builder(context); 
       alert.setTitle("Alert Dialog With EditText"); //Set Alert dialog title here 
       alert.setMessage("Edit Your Name Here"); //Message here 

       final EditText input = new EditText(context); 
       alert.setView(input); 

       alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         String srt = input.getEditableText().toString(); 
         Toast.makeText(context,srt, Toast.LENGTH_LONG).show(); 

        } 
       }); 
       alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         dialog.cancel(); 
        } 
       }); //End of alert.setNegativeButton 
       AlertDialog alertDialog = alert.create(); 
       alertDialog.show(); 
      } 
     }); 

任命这个工作

讲究接口OnItemClickListener

你需要哪一种方法有一个错误覆盖和你写的东西恩

public interface OnItemClickListener { 

     /** 
     * Callback method to be invoked when an item in this AdapterView has 
     * been clicked. 
     * <p> 
     * Implementers can call getItemAtPosition(position) if they need 
     * to access the data associated with the selected item. 
     * 
     * @param parent The AdapterView where the click happened. 
     * @param view The view within the AdapterView that was clicked (this 
     *   will be a view provided by the adapter) 
     * @param position The position of the view in the adapter. 
     * @param id The row id of the item that was clicked. 
     */ 
     void onItemClick(AdapterView<?> parent, View view, int position, long id); 
    } 

拯救你需要这样的变化

mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 
      AlertDialog.Builder alert = new AlertDialog.Builder(context); 
      alert.setTitle("Alert Dialog With EditText"); //Set Alert dialog title here 
      alert.setMessage("Edit Your Name Here"); //Message here 

      final EditText input = new EditText(context); 
      input.setText((String)planetList.get(position)); 
      alert.setView(input); 

      alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        String srt = input.getEditableText().toString(); 
        Toast.makeText(context,srt, Toast.LENGTH_LONG).show(); 
        planetList.set(position, srt); 
        listAdapter.notifyDataSetChanged(); 
       } 
      }); 
      alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        dialog.cancel(); 
       } 
      }); //End of alert.setNegativeButton 
      AlertDialog alertDialog = alert.create(); 
      alertDialog.show(); 
     } 
    }); 

planetList作出最终final ArrayList planetList = new ArrayList();

然而,对于更好地利用泛型名单

+0

其工作谢谢.. – Anoop 2015-02-24 09:43:35

+0

如何将新名称保存到编辑的列表视图中? – Anoop 2015-02-24 09:44:47

+0

查看我的更改。 – SorryForMyEnglish 2015-02-24 09:59:03