2014-06-17 27 views
0

已在我的應用程序中實現了ListFragment。當我單擊List上的一個項目時,我想將TextView中的特定數據存儲到String中。我被困在這一部分,請幫我出來,謝謝 這是我的代碼。如何在ListFragment中獲取數據onListItemClick

public class EventRegisterFragment extends ListFragment { 
String eventname; 
private ProgressDialog pDialog; 
JSONParser jsonParser = new JSONParser(); 
private static final String TAG_SUCCESS = "success"; 
private static String url_create_product = ""; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // TODO Auto-generated method stub  

    String[] events = new String[] { 
      "Eventname 1", 
      "Eventname 2", 
      "Eventname 3", 
      "Eventname 4", 
      "Eventname 5", 
      "Eventname 6", 
      "Eventname 7", 
      "Eventname 8", 
      "Eventname 9", 
      "Eventname 10" 
     }; 
    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>(); 
    for(int i=0;i<10;i++){ 
     HashMap<String, String> hm = new HashMap<String,String>(); 
     hm.put("txt", events[i]); 
     aList.add(hm); 
    } 
    String[] from = { "txt" }; 
    int[] to = {R.id.eventname}; 
    SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.list_item2, from, to); 

    setListAdapter(adapter); 
    return super.onCreateView(inflater, container, savedInstanceState); 


} 
    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 

     // Store the Clicked Item in a String here 

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
      getActivity()); 
    alertDialogBuilder.setTitle("Confirm Registration"); 
    alertDialogBuilder 
    .setMessage("Are you sure you want to Register for this Event?") 
    .setCancelable(false) 
    .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog,int id) { 
      // if this button is clicked, close 
      // current activity 
      new NetCheck().execute(); 
     } 
     }) 
    .setNegativeButton("No",new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog,int id) { 
      // if this button is clicked, just close 
      // the dialog box and do nothing 
      dialog.cancel(); 
     } 
    }); 
    AlertDialog alertDialog = alertDialogBuilder.create(); 

    // show it 
    alertDialog.show(); 
    } 

回答

0

試試這個:

String s = l.getitematposition(position).tostring();