将数据从ArrayAdapter发送到MainActivity的最佳方法

时间:2017-12-05 17:49:20

标签: android android-arrayadapter

我有一个 ArrayAdapter ,同时按下我想要的按钮:

  • 从我的数组中删除一些数据,该数据存放在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 看起来像这样:

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

}

1 个答案:

答案 0 :(得分:1)

您可以将适配器的回调发送回您的活动。

你的适配器会有类似的东西

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

当你创建你的适配器时,你会做那样的事情

new Adapter(new Adapter.Callback() {
            @Override
            public void onFileDeleted(final String fileName) {
                // Do your magic in the activity
            }
        });