2013-11-27 143 views
1

我正在開發一個應用程序,它從Web服務獲取數據並將其顯示在列表視圖中。我正在使用自定義列表視圖適配器。在列表的一行中有兩個按鈕。我想顯示一個對話框,當一個按鈕click.and也當一個對話框按鈕按下時,我想對它做一些操作。在自定義列表視圖項中單擊的對話框

這裏是我的適配器類:

public class NewsRowAdapter extends BaseAdapter { 

private Context mContext; 
private Activity activity; 
private static LayoutInflater inflater=null; 
private ArrayList<HashMap<String, String>> data; 
int resource; 
    //String response; 
    //Context context; 
    //Initialize adapter 
    public NewsRowAdapter(Context ctx,Activity act, int resource,ArrayList<HashMap<String, String>> d, DialogCreatorInterface di) { 
     super(); 
     this.resource=resource; 
     this.data = d; 
     this.activity = act; 
     this.mContext = ctx; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 



    public interface DialogCreatorInterface{ 
     public void showDialog(); 
    } 


    DialogCreatorInterface dialogCreatorInterface = new DialogCreatorInterface() { 

     @Override 
     public void showDialog() { 
      //Create and show the dialog code 
      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext); 
      alertDialogBuilder.setTitle("Your Title"); 

      // set dialog message 
      alertDialogBuilder 
       .setMessage("Click yes to exit!") 
       .setCancelable(false) 
       .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int id) { 
         // if this button is clicked, close 
         // current activity 
         //MainActivity.this.finish(); 

         Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show(); 
        } 
        }) 
        .setNegativeButton("No",new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog,int id) { 
          // if this button is clicked, just close 
          // the dialog box and do nothing 
          dialog.cancel(); 
         } 
        }); 
      AlertDialog alertDialog = alertDialogBuilder.create(); 

      // show it 
      alertDialog.show(); 

     } 
    }; 

@Override 
public View getView(final int position, View convertView, final ViewGroup parent) { 


    View vi = convertView; 
    if(convertView==null) 
     vi = inflater.inflate(R.layout.row,null); 

     TextView firstname = (TextView) vi.findViewById(R.id.fname); 
     TextView lastname = (TextView) vi.findViewById(R.id.lname); 
     TextView startTime = (TextView) vi.findViewById(R.id.stime); 
     TextView endTime = (TextView) vi.findViewById(R.id.etime); 
     TextView date = (TextView) vi.findViewById(R.id.blank); 
     ImageView img = (ImageView) vi.findViewById(R.id.list_image); 


     HashMap<String, String> song = new HashMap<String, String>(); 
     song =data.get(position); 

     firstname.setText(song.get(MainActivity.TAG_PROP_FNAME)); 
     lastname.setText(song.get(MainActivity.TAG_PROP_LNAME)); 
     startTime.setText(song.get(MainActivity.TAG_STIME)); 
     endTime.setText(song.get(MainActivity.TAG_ETIME)); 
     date.setText(song.get(MainActivity.TAG_DATE)); 
     //imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), img); 

     Button accept = (Button) vi.findViewById(R.id.button1); 
     accept.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       final int x = (int) getItemId(position); 
       //Toast.makeText(parent.getContext(),"you clicked "+ x , Toast.LENGTH_SHORT).show(); 


       /*Intent zoom=new Intent(mContext, Profile.class); 
       zoom.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 
       zoom.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       mContext.startActivity(zoom);*/ 


       //dialogCreatorInterface.showDialog(); 





      } 
     }); 

     vi.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       //Toast.makeText(parent.getContext(), "view clicked: " , Toast.LENGTH_SHORT).show(); 



       Intent zoom=new Intent(parent.getContext(), Profile.class); 
       parent.getContext().startActivity(zoom); 


      } 
     }); 

     return vi; 


} 



@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return data.size(); 
} 



@Override 
public Object getItem(int possision) { 
    // TODO Auto-generated method stub 
    return possision; 
} 



@Override 
public long getItemId(int possision) { 
    // TODO Auto-generated method stub 
    return possision; 
} 
} 

我曾嘗試一些有dialoginterface..but我couldnt't處理這件事......請有人幫助我..

編輯

這是我Getview方法目前

public View getView(final int position, View convertView, final ViewGroup parent) { 


    View vi = convertView; 
    if(convertView==null) 
     vi = inflater.inflate(R.layout.row,null); 

     TextView firstname = (TextView) vi.findViewById(R.id.fname); 
     TextView lastname = (TextView) vi.findViewById(R.id.lname); 
     TextView startTime = (TextView) vi.findViewById(R.id.stime); 
     TextView endTime = (TextView) vi.findViewById(R.id.etime); 
     TextView date = (TextView) vi.findViewById(R.id.blank); 
     ImageView img = (ImageView) vi.findViewById(R.id.list_image); 


     HashMap<String, String> song = new HashMap<String, String>(); 
     song =data.get(position); 

     firstname.setText(song.get(MainActivity.TAG_PROP_FNAME)); 
     lastname.setText(song.get(MainActivity.TAG_PROP_LNAME)); 
     startTime.setText(song.get(MainActivity.TAG_STIME)); 
     endTime.setText(song.get(MainActivity.TAG_ETIME)); 
     date.setText(song.get(MainActivity.TAG_DATE)); 
     //imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), img); 

     Button accept = (Button) vi.findViewById(R.id.button1); 
     accept.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       final int x = (int) getItemId(position); 
       //Toast.makeText(parent.getContext(),"you clicked "+ x , Toast.LENGTH_SHORT).show(); 


       /*Intent zoom=new Intent(mContext, Profile.class); 
       zoom.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 
       zoom.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       mContext.startActivity(zoom);*/ 


       AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext); 

       alertDialogBuilder.setTitle("Your Title"); 
       alertDialogBuilder 
        .setMessage("Click yes to exit!") 
        .setCancelable(false) 
        .setPositiveButton("YES", new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          // TODO Auto-generated method stub 
          Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show(); 
         } 
        }) 
        .setNegativeButton("NO", new DialogInterface.OnClickListener() { 

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

       alertDialogBuilder.show(); 

      } 
     }); 

     vi.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       //Toast.makeText(parent.getContext(), "view clicked: " , Toast.LENGTH_SHORT).show(); 



       Intent zoom=new Intent(parent.getContext(), Profile.class); 
       parent.getContext().startActivity(zoom); 


      } 
     }); 

     return vi; 


} 
+0

刪除該接口的東西。只需將showDialogue邏輯作爲現在在界面中的單獨方法編寫即可。並在按鈕被點擊時調用該方法。您已經編寫了onClick事件。 –

+0

我已經嘗試過你所說的..但有同樣的問題:( –

+0

刪除AlertDialog alertDialog = alertDialogBu​​ilder.create();和alertDialog.show();。只需使用alertDialogBu​​ilder.show();它將工作 –

回答

0

試試這個..

public class NewsRowAdapter extends BaseAdapter { 

private Context mContext; 
private Activity activity; 
private static LayoutInflater inflater=null; 
private ArrayList<HashMap<String, String>> data; 
int resource; 
    //String response; 
    //Context context; 
    //Initialize adapter 
    public NewsRowAdapter(Context ctx,Activity act, int resource,ArrayList<HashMap<String, String>> d, DialogCreatorInterface di) { 
     super(); 
     this.resource=resource; 
     this.data = d; 
     this.activity = act; 
     this.mContext = ctx; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    }   

@Override 
public View getView(final int position, View convertView, final ViewGroup parent) { 


    View vi = convertView; 
    if(convertView==null) 
     vi = inflater.inflate(R.layout.row,null); 

     TextView firstname = (TextView) vi.findViewById(R.id.fname); 
     TextView lastname = (TextView) vi.findViewById(R.id.lname); 
     TextView startTime = (TextView) vi.findViewById(R.id.stime); 
     TextView endTime = (TextView) vi.findViewById(R.id.etime); 
     TextView date = (TextView) vi.findViewById(R.id.blank); 
     ImageView img = (ImageView) vi.findViewById(R.id.list_image); 


     HashMap<String, String> song = new HashMap<String, String>(); 
     song =data.get(position); 

     firstname.setText(song.get(MainActivity.TAG_PROP_FNAME)); 
     lastname.setText(song.get(MainActivity.TAG_PROP_LNAME)); 
     startTime.setText(song.get(MainActivity.TAG_STIME)); 
     endTime.setText(song.get(MainActivity.TAG_ETIME)); 
     date.setText(song.get(MainActivity.TAG_DATE)); 
     //imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), img); 

     Button accept = (Button) vi.findViewById(R.id.button1); 
     accept.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       final int x = (int) getItemId(position); 
       AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this); 

       // set title 
       alertDialogBuilder.setTitle("Your Title"); 

       // set dialog message 
       alertDialogBuilder 
       .setMessage("Click yes to exit!") 
       .setCancelable(false) 
       .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog,int id) { 
       Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show(); 


       }) 
       .setNegativeButton("No",new DialogInterface.OnClickListener() { 
        @SuppressLint("NewApi") 
        public void onClick(DialogInterface dialog,int id) { 

         dialog.cancel(); 

        } 
       }); 

       // create alert dialog 
       AlertDialog alertDialog = alertDialogBuilder.create(); 
       // show it 
       alertDialog.show();  
      } 
     }); 

     return vi; 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return data.size(); 
} 



@Override 
public Object getItem(int possision) { 
    // TODO Auto-generated method stub 
    return possision; 
} 



@Override 
public long getItemId(int possision) { 
    // TODO Auto-generated method stub 
    return possision; 
} 
} 

和你startActivity(zoom);Intent賴特在裏面ID列表視圖中單擊像下面

listview.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> arg0, View v, int position, 
        long id) { 
       Intent zoom=new Intent(Activity.this, Profile.class); 
       startActivity(zoom); 
      } 
     }); 
+0

我得到了這個錯誤在我的日誌c在 android.view.WindowManager $ BadTokenException:無法添加窗口 - 標記null不適用於應用程序 –

+0

@ user3020939什麼是行號。 – Hariharan

+0

alertDialog.show(); 這是行 –

相關問題