2013-02-11 22 views
10

मैंने बहुत सारे प्रश्नों को देखा है और काम करने के लिए कुछ भी नहीं लग रहा है। मेरे पास इस तरह के एक समारोह के साथ एक मुख्य वर्ग है जो संपादन एक संवाद बॉक्स दिखाता है, फिर एक बटन दबाए जाने पर एक सूची संपादित करता है।कस्टम ऐरेएडाप्टर से मुख्य गतिविधि के फ़ंक्शन को कैसे कॉल करें?

public class EditPlayers extends SherlockFragmentActivity { 

    listPlayerNames.setAdapter(new EditPlayerAdapter(ctx, 
       R.layout.score_row_edit_player, listScoreEdit)); 

public void deletePlayer(final int position) { 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(
      EditPlayers.this); 

    // Setting Dialog Title 
    alertDialog.setTitle("Delete Player"); 

    // Setting Dialog Message 
    alertDialog.setMessage("Are you sure?"); 

    // Setting Delete Button 
    alertDialog.setPositiveButton("Delete", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        listScoreEdit.remove(position); 
        updateListView(); 
       } 
      }); 

    // Setting Cancel Button 
    alertDialog.setNeutralButton("Cancel", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }); 

    // Showing Alert Message 
    alertDialog.show(); 
} 

} 

एडाप्टर में getView() से मैं उस फ़ंक्शन को कैसे एक्सेस करूं? यहाँ पंक्ति

<TextView 
    android:id="@+id/nameEdit" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:paddingBottom="10dp" 
    android:paddingTop="10dp" 
    android:paddingLeft="10dp" 
    android:layout_weight="70" 
    android:text="Name" 
    android:textColor="#666666" 
    android:textSize="22sp" 
    android:textStyle="bold" > 
    </TextView> 

<Button 
    android:id="@+id/deletePlayer" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="30" 
    android:text="Delete" 
    android:focusable="false" /> 

के लिए एक्सएमएल यहाँ getView()

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

    convertView = (LinearLayout) inflater.inflate(resource, null);  
    Score score = getItem(position); 
    TextView txtName = (TextView) convertView.findViewById(R.id.nameEdit); 
    txtName.setText(score.getName()); 
    Button b = (Button)convertView.findViewById(R.id.deletePlayer); 
    b.setTag(position); 
    b.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       //call function here 
      } 
    }); 
    return convertView; 
} 

मैं पूरी तरह इस बात इसलिए किसी भी मदद की सराहना की जाएगी में खो रहा हूँ है। धन्यवाद!

उत्तर

31

मैं आपकी गतिविधि पर interface प्रदान करने की अनुशंसा करता हूं जो यह बटन दबाए जाने पर उसे बताए। मैं एक ऐरेएडाप्टर से गतिविधि की विधि को कॉल करने की अनुशंसा नहीं करता। यह बहुत कसकर युग्मित है।

इस तरह की कोशिश कुछ:

आपका Activity

public class EditPlayers extends SherlockFragmentActivity implements EditPlayerAdapterCallback { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     EditPlayerAdapter adapter = new EditPlayerAdapter(this, 
       R.layout.score_row_edit_player, listScoreEdit); 
     adapter.setCallback(this); 
     listPlayerNames.setAdapter(adapter); 

    } 

    private void deletePlayer(final int position) { 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(
       EditPlayers.this); 

     // Setting Dialog Title 
     alertDialog.setTitle("Delete Player"); 

     // Setting Dialog Message 
     alertDialog.setMessage("Are you sure?"); 

     // Setting Delete Button 
     alertDialog.setPositiveButton("Delete", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         listScoreEdit.remove(position); 
         updateListView(); 
        } 
       }); 

     // Setting Cancel Button 
     alertDialog.setNeutralButton("Cancel", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 
        } 
       }); 

     // Showing Alert Message 
     alertDialog.show(); 
    } 

    @Override 
    public void deletePressed(int position) { 

     deletePlayer(position); 
    } 

} 

एडाप्टर:

public class EditPlayerAdapter extends ArrayAdapter { 

    private EditPlayerAdapterCallback callback; 

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

     convertView = (LinearLayout) inflater.inflate(resource, null);  
     Score score = getItem(position); 
     TextView txtName = (TextView) convertView.findViewById(R.id.nameEdit); 
     txtName.setText(score.getName()); 
     Button b = (Button)convertView.findViewById(R.id.deletePlayer); 
     b.setTag(position); 
     b.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 

        if(callback != null) { 

         callback.deletePressed(position); 
        } 
       } 
     }); 
     return convertView; 
    } 

    public void setCallback(EditPlayerAdapterCallback callback){ 

     this.callback = callback; 
    } 


    public interface EditPlayerAdapterCallback { 

     public void deletePressed(int position); 
    } 
} 
+0

धन्यवाद, यह पूरी तरह से काम किया! – GrilledCheese

+0

वाह, यह जादू था। –

+0

धन्यवाद। मेरे लिए इसका सबसे अच्छा समाधान :) – SAndroidD

12

आपके EditPlayerAdapter को Context पास हो गया। Activity फैली Context

हैंContext पारित कर अपने EditPlayers है और आप अपने एडाप्टर में है कि Context करने के लिए एक वर्ग-दायरे संदर्भ की दुकान, आप तो कर सकते हैं:

((EditPlayers) yourContextVar).function(); 

अभी तक बेहतर, की एक इंटरफेस बनाने किसी प्रकार। यह आपके कोड को स्पष्ट और व्यवस्थित करने में मदद करेगा और यह वही सिद्धांत लागू करता है।