如何在ListFragment中获取关于ListItemClick的数据

时间:2014-06-17 14:28:05

标签: java android android-listfragment

在我的应用中实现了ListFragment。当我单击List上的项目时,我想将TextView中的特定数据存储到String中。我坚持这一部分,请帮我解决这个问题,谢谢 这是我的代码。

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

1 个答案:

答案 0 :(得分:0)

试试这个:

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