質問

For some reason, my Long id is storing the position of the row, rather than the _id

I am looking to get the _id so that I can perform some other functions onClick

Here is my code:

public class WorkoutsFragment extends Fragment {

    WorkoutsDatabaseHelper db;
    SimpleCursorAdapter adapter;
    ListView listContent;
    Cursor cursor;
    private Context mContext;
    WorkoutsAdapter workoutsAdapter;

    public WorkoutsFragment(){}


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContext = getActivity();
        db = new WorkoutsDatabaseHelper(mContext);

        View rootView = inflater.inflate(R.layout.activity_workouts, container, false);

        listContent = (ListView) rootView.findViewById(R.id.list);

        // Create the Adapter
        workoutsAdapter = new WorkoutsAdapter(mContext, cursor);

        // Set the adapter to ListView
        listContent.setAdapter(workoutsAdapter);

        listContent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            String idString;

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("Clicked item _id = ", ""+id);
            }
        });
        return rootView;
    }




    private void addItemsToList(){
        Cursor cursor = db.getWorkoutNames();
        Log.d("history.java", "finished Cursor cursor = db.getAllLogs();");
        String[] from = {"_id", "workouts_name"};
        int[] to = {R.id.tv_id, R.id.tv_label};
        adapter = new SimpleCursorAdapter(mContext, R.layout.single_item, cursor, from, to, 0);
        listContent.setAdapter(adapter);

    }

    public void onResume(){
        super.onResume();
        workoutsAdapter = new WorkoutsAdapter(mContext, cursor);
        listContent.setAdapter(workoutsAdapter);

    }

    public void onSaveInstanceState(Bundle savedState) {
        super.onSaveInstanceState(savedState);
    }


}

anyone understand what I may be doing wrong, and how to correct this?

Thanks

If you'd like to see additional code, please let me know.

役に立ちましたか?

解決

I realized I was implementing this in the wrong file. once I implemented in the Adapter rather than the fragment, it worked perfectly.

view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, id, Toast.LENGTH_LONG).show();
                Log.d("Clicked item _id = ", "" + id);
                }
        });
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top