[英]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.