您好,我正在尝试学习Android开发,并且正在构建一个新的应用程序。我已经实现了ListFragment和适配器以显示一些注释。这些注释会按我的意愿显示,但是现在当我单击某个项目时,我无法触发其他活动,并且在日志中看到了上面的错误。我的代码如下所示。

这是主要片段

 public class MainListFragment extends ListFragment {

private ArrayList<NoteStructure> notes;
private NoteAdapter noteAdapter;
public MainListFragment() {
    // Required empty public constructor
}


public void onActivityCreated(Bundle savedInstanceState){

    super.onActivityCreated(savedInstanceState);

     notes = new ArrayList<NoteStructure>();
    notes.add(new NoteStructure("malakia", "sdfsfsdfffffffsdfsfsfdfsfsds"));
    notes.add(new NoteStructure("sou gamw", "gamithikessssssssssssssssssssssssssssssssss"));
    noteAdapter = new NoteAdapter(getActivity(),notes);
    setListAdapter(noteAdapter);
}
public void onListItemClick(ListView listView, View view, int position, long id){

    super.onListItemClick(listView,view,position,id);
    Log.e("Item" ,"click"+ position);
    procceedToOtherActivity(position);
}
private void procceedToOtherActivity (int position){

    NoteStructure note = (NoteStructure) getListAdapter().getItem(position);
    Intent intent = new Intent(getActivity().getApplicationContext(),DetailActivity.class);
    intent.putExtra(MainActivity.NOTEID,note.getNoteId() );
    intent.putExtra(MainActivity.NOTETITLE,note.getTitle());
    intent.putExtra(MainActivity.NOTESUMMARY,note.getSummary());
    startActivity(intent);
}


}

这是Note适配器:

public class NoteAdapter extends ArrayAdapter<NoteStructure> {

public static class ViewRef {

    TextView noteTitle;
    TextView noteSummary;
}
public NoteAdapter(Context context, ArrayList<NoteStructure> notes){

    super(context,0,notes);
}

public View getView(int position, View convertView, ViewGroup parent){

    NoteStructure note = getItem(position);
    ViewRef viewRef;
    if(convertView==null) {

        viewRef = new ViewRef();
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_row,parent,false);
        viewRef.noteTitle=(TextView)convertView.findViewById(R.id.noteTitle);
        viewRef.noteSummary=(TextView)convertView.findViewById(R.id.noteSummary);
        convertView.setTag(viewRef);
    }
    else {

        viewRef = (ViewRef) convertView.getTag();
    }



    viewRef.noteTitle.setText(note.getTitle());
    viewRef.noteSummary.setText(note.getSummary());

    return convertView;
}


}

我还有一个NoteStructure类,该类仅包含id,title和summary变量以及getter和setter,并且mainActivity和DetailActivity现在什么都不做。我进行了很多搜索,但无法弄清楚为什么DetaiLActivity无法启动的问题。有人可以帮帮我吗?

这是我与一个临时侦听器的实现

public class MainListFragment extends Fragment {

private View view;
private ListView list;
private ArrayList<NoteStructure> notes;
private NoteAdapter noteAdapter;
public MainListFragment() {
    // Required empty public constructor
}


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    view = inflater.inflate(R.layout.main_fragment_layout, container, false);

    list= (ListView) view.findViewById(R.id.listView);
     notes = new ArrayList<NoteStructure>();
    notes.add(new NoteStructure("malakia", "sdfsfsdfffffffsdfsfsfdfsfsds"));
    notes.add(new NoteStructure("sou gamw", "gamithikessssssssssssssssssssssssssssssssss"));

    noteAdapter = new NoteAdapter(getActivity(),notes);
    list.setAdapter(noteAdapter);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            procceedToOtherActivity(position);
        }
    });

    return view;
}

private void procceedToOtherActivity (int position){

    NoteStructure note = (NoteStructure) list.getAdapter().getItem(position);
    Intent intent = new Intent(getActivity().getApplicationContext(),DetailActivity.class);
    intent.putExtra(MainActivity.NOTEID,note.getNoteId() );
    intent.putExtra(MainActivity.NOTETITLE,note.getTitle());
    intent.putExtra(MainActivity.NOTESUMMARY,note.getSummary());
    startActivity(intent);
}


}

最佳答案

您的itemClickListener无法启动。您应该像这样重写监听器:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO implement some logic
}


或者您应该扩展正常片段

public class MainListFragment extends Fragment{


并为listview设置临时的简单侦听器:

listview.setAdapter(noteAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            procceedToOtherActivity(position);
        }
    });

09-30 12:27