在片段Microsoft Windows [Version 10.0.14393]
中:
onCreateView
适配器类:
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);
XML项目:
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));
}
}
如何从longitemclicklistener的每个项目中获取textview值,以获取以下内容:
<?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>
答案 0 :(得分: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;
}