如何调用从自定义ArrayAdapter主要活动的功能? [英] How to call main activity's function from custom ArrayAdapter?

查看:194
本文介绍了如何调用从自定义ArrayAdapter主要活动的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看了很多类似的问题,并似乎无法得到任何工作。我有一个主类,像这样的编辑功能,然后显示一个对话框,修改时一个按钮是pressed列表。

I've looked at a lot of similar questions and can't seem to get anything to work. I have a main class with a function like this that edits shows a dialog box then edits a List when a button is pressed.

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的行

How do I access that function from the getView() in the adapter? Here's the XML for the row

<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()

Here's the 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;
}

我完全失去了在这一点上,因此任何帮助将是AP preciated。谢谢!

I'm totally lost at this point so any help would be appreciated. Thanks!

推荐答案

我会建议提供了一个接口返回到您的活动,让它知道什么时候该按钮是pressed。我不建议从ArrayAdapter呼叫活动的方法。实在是太紧密耦合。

I would recommend providing an interface back to your activity that lets it know when that button is pressed. I would not recommend calling an activity's method from an ArrayAdapter. It is too tightly coupled.

尝试是这样的:

活动

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

这篇关于如何调用从自定义ArrayAdapter主要活动的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆