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
是的,我做了...簡單而容易。我不得不編輯你的答案,使其100%正確。謝謝。 – Si8