2016-10-15 1 views
0

В Фрагмент onCreateView: классКак получить значение из onlongitemclicklistener в 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); 

адаптер:

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

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> 

Как получить TextView значения из каждого элемента onlongitemclicklistener, для следующего:

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

ответ

1

Вы хотите этого?

@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

Да, я сделал ... простую и легкую. Мне пришлось отредактировать ваш ответ, чтобы сделать его правильным на 100%. Благодарю. – Si8

Смежные вопросы