我的应用程序使用包含在片段中的RecyclerView进行编码。我能够使用模拟数据列表成功填充RecyclerView,但是我不知道如何在单击其中一个项目时将Intent发送到另一个Activity。当我尝试从我的片段中调用SELECT p.id, p.person_name, d.department_name, c.phone_number
FROM person p
LEFT JOIN (
SELECT person_id, min(department_name) AS department_name
FROM department
GROUP BY person_id
) d ON d.person_id = p.id
LEFT JOIN (
SELECT person_id, min(phone_number) AS phone_number
FROM contact
GROUP BY person_id
) c ON c.person_id = p.id;
时,Android Studio无法解析该方法。
我的StartActivity
是通过我的viewholder类中的接口实现的。该界面还会生成错误onItemClick
。
我想知道我是否无法从片段中调用inner classes cannot have static declarations
,因为我的RecyclerView适配器或查看者类都没有扩展StartActivity
。
单击RecyclerView中的项目时发送Intent的适当实现是什么?
PlaceHolderFragment
Activity
TaskAdapter
public static class PlaceholderFragment extends Fragment {
Activity mActivity;
RecyclerView mRecyclerView;
TaskAdapter taskAdapter;
public PlaceholderFragment() {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.mActivity = (Activity) activity;
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.cardList);
taskAdapter = new TaskAdapter(createList(30));
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mRecyclerView.setAdapter(taskAdapter);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//Cannot resolve method
taskAdapter.SetItemClickListener(new TaskAdapter.TaskViewHolder.OnItemClickListener() {
@Override
public void onItemClick(View v, int position) {
Intent detailIntent = new Intent(v.getContext(), DetailActivity.class);
//Cannot resolve method
StartActivity(detailIntent);
}
});
}
private List<Task> createList(int size) {
List<Task> result = new ArrayList<>();
for (int i = 1; i<=size; i++) {
Task task = new Task();
task.title = Task.TITLE_PREFIX + i;
task.updated = Task.UPDATED_PREFIX +i;
task.notes = task.NOTES_PREFIX + i;
result.add(task);
}
return result;
}
}
答案 0 :(得分:1)
startActivity()方法以小s开头。试试以下代码行
//Cannot resolve method
taskAdapter.TaskViewHolder.SetItemClickListener(new TaskAdapter.TaskViewHolder.OnItemClickListener() {
@Override
public void onItemClick(View v, int position) {
Intent detailIntent = new Intent(v.getContext(), DetailActivity.class);
//Cannot resolve method
getActivity().startActivity(detailIntent);
}
});
SetOnItemClickListener()应该在TaskViewHolder类之外。尝试下面的一个,看看它是否有帮助。
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.TaskViewHolder> {
private List<Task> taskList;
TaskViewHolder.OnItemClickListener mItemClickListener;
public TaskAdapter(List<Task> taskList) {
this.taskList = taskList;
}
@Override
public int getItemCount() {
return taskList.size();
}
@Override
public void onBindViewHolder(TaskViewHolder taskViewHolder, int i) {
Task task = taskList.get(i);
taskViewHolder.mTitle.setText(task.title);
taskViewHolder.mUpdated.setText(task.updated);
taskViewHolder.mStatus.setChecked(false);
}
@Override
public TaskViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.tasklist_layout, viewGroup, false);
return new TaskViewHolder(itemView);
}
public class TaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected TextView mTitle;
protected TextView mUpdated;
protected CheckBox mStatus;
public TaskViewHolder(View view) {
super(view);
mTitle = (TextView) view.findViewById(R.id.title);
mUpdated = (TextView) view.findViewById(R.id.updated);
mStatus = (CheckBox) view.findViewById(R.id.status);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(v, getPosition());
}
}
//Inner Classes cannot have static declarations
public interface OnItemClickListener {
void onItemClick(View view , int position);
}
}
public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
//cannot resolve symbol
this.mItemClickListener = mItemClickListener;
}
}