2015-06-17 97 views
-3
public class Titles extends Fragment { 
    View view; 
    SimpleCursorAdapter adapter; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     MySqlite db=new MySqlite(getActivity()); 
     view= inflater.inflate(R.layout.fragment_titles, container, false); 
     ListView list=(ListView)view.findViewById(R.id.list); 
     Cursor all_titles = db.getTitle(); 
     adapter = new SimpleCursorAdapter(getActivity().getBaseContext(), 
       R.layout.fragment_titles, 
       all_titles, 
       new String[] { db.KEY_TITLE}, 
       new int[] { R.id.tv_title }, 0); 
     //Attach cursor adapter to the ListView 
     list.setAdapter(adapter); 

     list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Toast.makeText(getActivity().getApplicationContext(),"ListView Clicked Go Ahead",Toast.LENGTH_LONG).show(); 
       TextView tv=(TextView)view.findViewById(R.id.tv_title); 
       String selected_title=tv.getText().toString(); 

       // Launching new Activity on selecting single List Item 
       Intent i = new Intent(getActivity().getApplicationContext(), Poem.class); 
       // sending data to new activity 
       i.putExtra("title", selected_title); 
       startActivity(i); 

      } 
     }); 

    return view; 

    } 
} 

我剛剛開始Android開發,和我的工作得到具有點擊的片段的Lis​​tView項目問題複選框working.I'm自定義列表視圖。在模擬器中,listview click事件不起作用。我正在使用片段。 任何人都可以幫忙。 在此先感謝列表視圖onItemClickListener不工作

回答

0

我編輯了部分代碼。整個代碼波紋管:

public class Titles extends Fragment { 

    View view; 
    SimpleCursorAdapter adapter; 

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

     return inflater.inflate(R.layout.fragment_titles, container, false); 

    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     View view = getView(); 

     // Inflate the layout for this fragment 
     MySqlite db = new MySqlite(getActivity()); 

     ListView list = (ListView) view.findViewById(R.id.list); 

     Cursor all_titles = db.getTitle(); 

     adapter = new SimpleCursorAdapter(getActivity().getBaseContext(), 
       R.layout.fragment_titles, 
       all_titles, 
       new String[] { db.KEY_TITLE}, 
       new int[] { R.id.tv_title }, 0); 

     //Attach cursor adapter to the ListView 
     list.setAdapter(adapter); 

     list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      Toast.makeText(getActivity().getApplicationContext(),"ListView Clicked Go Ahead",Toast.LENGTH_LONG).show(); 
      TextView tv=(TextView)view.findViewById(R.id.tv_title); 
      String selected_title=tv.getText().toString(); 

      // Launching new Activity on selecting single List Item 
      Intent i = new Intent(getActivity().getApplicationContext(), Poem.class); 
      // sending data to new activity 
      i.putExtra("title", selected_title); 
      getActivity().startActivity(i); 

     } 

     }); 

     return view; 

    } 

} 

基本上,我只動了你的代碼onActivityCreated,改變了這部分startActivity(i)這個getActivity().startActivity(i)