2016-10-15 1 views
0

Dans Fragment onCreateView:Comment obtenir la valeur de onlongitemclicklistener au point GridView

GridView gv = (GridView)view.findViewById(R.id.gvEntries); 
gv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
    @Override 
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
     SharedPreferences.Editor editor = getActivity().getSharedPreferences("com.myapp", MODE_PRIVATE).edit(); 
     editor.putString("theDate", ""); //get the "the_date" from position 
     editor.putString("TheValue", ""); //get the "the_value" from position 
     editor.commit(); 
     ((MainActivity)getActivity()).openDialog(1); 
     return false; 
    } 
}); 
Cursor crs = dbh.getC("1"); 
PopulateGridView pgv = new PopulateGridView(getActivity(), crs); 
gv.setAdapter(pgv); 

classe Adaptateur:

public class PopulateGridView extends CursorAdapter { 
    public PopulateGridView(Context context, Cursor cursor) { 
     super(context, cursor, 0); 
    } 
    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     return LayoutInflater.from(context).inflate(R.layout.disp_data_in_grid, parent, false); 
    } 
    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     // Find fields to populate in inflated template 
     TextView tvVal = (TextView) view.findViewById(R.id.tvGVal); 
     TextView tvDate = (TextView) view.findViewById(R.id.tvGDate); 
     // Extract properties from cursor 
     String strVal = cursor.getString(cursor.getColumnIndexOrThrow("the_value")); 
     String strDate = cursor.getString(cursor.getColumnIndexOrThrow("the_date")); 
     // Populate fields with extracted properties 
     tvVal.setText(strVal); 
     tvDate.setText(String.valueOf(strDate)); 
    } 
} 

élément XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="10dp" 
    android:background="@drawable/grid_selector"> 

    <TextView 
     android:text="TextView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/tvGVal" 
     android:layout_marginBottom="10dp" 
     android:gravity="center" 
     android:textSize="18dp" /> 

    <TextView 
     android:text="TextView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/tvGDate" /> 

</LinearLayout> 

Comment puis-je obtenir le textview valeurs de chaque élément onlongitemclicklistener, pour les éléments suivants:

editor.putString("theDate", ""); //get the "the_date" from position 
editor.putString("TheValue", ""); //get the "the_value" from position 

Répondre

1

Voulez-vous ça?

@Override 
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
TextView tvVal = (TextView) view.findViewById(R.id.tvGVal); 
     TextView tvDate = (TextView) view.findViewById(R.id.tvGDate); 

     SharedPreferences.Editor editor = getActivity().getSharedPreferences("com.myapp", MODE_PRIVATE).edit(); 
     editor.putString("theDate", tvVal.getText().toString()); //get the "the_date" from position 
     editor.putString("TheValue", tvDate.getText().toString()); //get the "the_value" from position 
     editor.commit(); 
     ((MainActivity)getActivity()).openDialog(1); 
     return false; 
    } 
+0

Oui, je l'ai fait ... simple et facile. J'ai dû modifier votre réponse pour la corriger à 100%. Merci. – Si8