简体   繁体   English

如何从自定义ArrayAdapter调用main活动的功能?

[英]How to call main activity's function from custom ArrayAdapter?

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. 我有一个带有这样功能的主类,编辑显示一个对话框,然后在按下按钮时编辑一个List。

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

}

How do I access that function from the getView() in the adapter? 如何从适配器中的getView()访问该函数? Here's the XML for the row 这是行的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" />

Here's the getView() 这是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;
}

I'm totally lost at this point so any help would be appreciated. 我完全迷失了,所以任何帮助都会受到赞赏。 Thanks! 谢谢!

I would recommend providing an interface back to your activity that lets it know when that button is pressed. 我建议为您的活动提供一个interface ,让它知道何时按下该按钮。 I would not recommend calling an activity's method from an ArrayAdapter. 我不建议从ArrayAdapter调用activity的方法。 It is too tightly coupled. 它太紧密耦合了。

Try something like this: 尝试这样的事情:

Your Activity 你的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);
    }

}

Adapter: 适配器:

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

Your EditPlayerAdapter gets a Context passed to it. 您的EditPlayerAdapter获取传递给它的Context Activity extends Context Activity扩展了Context

If the Context passed is your EditPlayers and you store a class-scoped reference to that Context in your Adapter, you can then do: 如果 Context传递的是您的EditPlayers并且您在适配器中存储了对该Context的类范围引用,则可以执行以下操作:

((EditPlayers) yourContextVar).function();

Better yet, make an interface of some sort. 更好的是,创建某种界面。 It will help clarify and organise your code and it applies the same principle. 它将有助于澄清和组织您的代码,并应用相同的原则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从活动中调用自定义视图中的函数 - how to call a function that's in a custom View from an activity Android-如何在“主活动”的自定义视图中调用方法? - Android - How to call method in custom View from Main Activity? 如何从另一个活动调用 ArrayAdapter 以使用 strings.xml 数组从 ArrayAdapter 中删除项目? - How can I call an ArrayAdapter from another activity to delete item from ArrayAdapter with strings.xml array? 如何使用自定义行从ArrayAdapter在onClick方法中启动Activity - How to start an Activity in onClick method from ArrayAdapter with custom rows 如何从主要活动中调用片段方法 - How to call fragment method from main activity 如何使用另一个活动或主类中的活动调用处理程序 - how to use call a handler in an activity from another activity or main class 如何从其他活动中调用主要活动中的方法? - How to call method in main activity from other activity? 如何从主要活动中调用选项卡式活动片段? - How to call a tabbed activity fragment from main activity? 如何从主要活动中调用android蓝牙的ConnectedThread类的write()函数 - How to call write() function of ConnectedThread class of android bluetooth from main activity 如何从主要活动类中的自定义列表视图适配器管理onClick函数 - How can I manage onClick function from my custom listview adapter in main activity class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM