11

我已经查看了很多类似的问题,但似乎无法解决任何问题。我有一个主类,它具有这样的功能,可以编辑显示一个对话框,然后在按下按钮时编辑一个列表。

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() 访问该函数?这是该行的 XML

<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;
}

在这一点上我完全迷失了,所以任何帮助将不胜感激。谢谢!

4

2 回答 2

33

我建议interface您返回您的活动,让它知道何时按下该按钮。我不建议从 ArrayAdapter 调用活动的方法。它耦合得太紧了。

尝试这样的事情:

您的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);
    }
}
于 2013-02-11T23:51:38.667 回答
12

EditPlayerAdapter得到一个Context传递给它。Activity延伸Context

如果Context传递的是你的并且EditPlayers你在你的适配器中存储了一个类范围的引用Context,你可以这样做:

((EditPlayers) yourContextVar).function();

更好的是,制作某种界面。它将有助于澄清和组织您的代码,并应用相同的原则。

于 2013-02-11T23:32:35.390 回答