本文介绍了将数据从ArrayAdapter发送到MainActivity的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 ArrayAdapter ,并且在按下按钮时我想要:
I have an ArrayAdapter and while pressing on a button I want to:
- 从Array中删除一些保存在MainActivity中的数据
- 从txt文件中删除一行(FileManager可以处理删除,但是我必须访问MainActivity中私有的文件)
如何发送此数据?
deleteDeckButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//i've done it with DECK_NAMES being static public in
//MainActivity and i really dont like this solution
FileManager deckNamesManager = new FileManager(DECK_NAMES); deckNamesManager.deleteLine(manager.getFileName());
// deck files is static public in MainActivity as well
deckFiles.remove(getPosition(manager));
manager.deleteFile();
notifyDataSetChanged();
}
});
这就是我希望我的 MainActivity 看起来像这样:
That's how I'd like my MainActivity to look like:
public class MainActivity extends AppCompatActivity {
static final int DECK_NAME_REQUEST = 1;
private ListView mListView;
private FlashCardsAdapter deckAdapter;
// ATM deckFiles is static public
private ArrayList<FileManager> deckFiles;
// ATM DECK_NAMES is static public
private File DECK_NAMES; // ATM it's static public
ArrayAdapter
public class FlashCardsAdapter extends ArrayAdapter<FileManager> {
interface Callback {
void onFileDeleted(String fileName);
}
private final Callback mCallback;
public FlashCardsAdapter(Context context, ArrayList<FileManager> users, final Callback callback) {
super(context, 0, users);
mCallback = callback;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final FileManager manager = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_view_item, parent, false);
}
TextView mainTextView = convertView.findViewById(R.id.deck_name_id);
Button addCardButton = convertView.findViewById(R.id.action_add_card);
Button deleteDeckButton = convertView.findViewById(R.id.action_delete);
mainTextView.setText(manager.getFileName());
mainTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getContext(), FlashCardActivity.class);
intent.putExtra("fileName", manager.getFileName());
getContext().startActivity(intent);
}
});
addCardButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getContext(), AddCardActivity.class);
intent.putExtra("fileName", manager.getFileName());
getContext().startActivity(intent);
}
});
deleteDeckButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
manager.deleteFile();
mCallback.onFileDeleted(manager.getFileName());
}
});
return convertView;
}
}
推荐答案
您可以将回调从适配器发送回活动.
You can send a callback from your adapter back to your activity.
您的适配器会有类似的东西
You adapter would have something like that
class Adapter {
interface Callback {
void onFileDeleted(String fileName);
}
private final Callback callback;
Adapter(final Callback callback) {
this.callback = callback;
}
..
..
..
deleteDeckButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FileManager deckNamesManager = new FileManager(DECK_NAMES); //i've done it with DECK_NAMES being static public in MainActivity and i really dont like this solution
deckNamesManager.deleteLine(manager.getFileName());
deckFiles.remove(getPosition(manager)); // deck files is static public in MainActivity as well
manager.deleteFile();
notifyDataSetChanged();
callback.onFileDeleted(manager.getFileName());
}
});
}
在创建适配器时,您会做类似的事情
And when you are creating your adapter you would do something like that
new Adapter(new Adapter.Callback() {
@Override
public void onFileDeleted(final String fileName) {
// Do your magic in the activity
}
});
这篇关于将数据从ArrayAdapter发送到MainActivity的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!