我只需要接收字符串,它是与我单击的gridview项相关联的任何文本视图。
getItemAtPosition给了我一系列随机数,例如:Collectable @ c88c9b和SELECT * FROM TABLE_COLLECTION当我只需要单击的内容时,它仅将每行中的每个名称传递给我,请帮助我,我已经快完成了。
带有gridview的片段
public class CollectionFragment extends Fragment {
GridView gridView;
ArrayList<Collectable> list;
CollectionListAdapter adapter = null;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
setHasOptionsMenu (true);
// inflater.inflate(R.layout.fragment_collection,container,false);
View view = inflater.inflate(R.layout.fragment_collection,null);
gridView = (GridView) view.findViewById(R.id.gridView);
list = new ArrayList<>();
//if (gridView != null) {
adapter = new CollectionListAdapter(getactivity().getapplicationContext(),R.layout.images_carousel,list);
gridView.setadapter(adapter);
//}
final Cursor cursor = ScannerPopup.sqLiteHelper.getData("SELECT * FROM TABLE_COLLECTION");
list.clear();
while(cursor.moveToNext()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
String box = cursor.getString(2);
String prod = cursor.getString(3);
String date = cursor.getString(4);
String store = cursor.getString(5);
String country = cursor.getString(6);
byte[] image = cursor.getBlob(7);
list.add(new Collectable(name,box,prod,date,store,country,image,id));
}
adapter.notifyDataSetChanged();
gridView.setOnItemClicklistener(new AdapterView.OnItemClicklistener() {
@Override
public void onItemClick(AdapterView<?> parent,View view,int position,long l) {
String clickedText = parent.getItemAtPosition(position).toString();
Cursor c = ScannerPopup.sqLiteHelper.getData("SELECT * FROM TABLE_COLLECTION");
while(c.moveToNext()) {
int id = c.getInt(0);
String name = c.getString(1);
String box = c.getString(2);
String prod = c.getString(3);
String date = c.getString(4);
String store = c.getString(5);
String country = c.getString(6);
byte[] image = c.getBlob(7);
Log.d("meme",clickedText);
Log.d("tag2",name);
}
}
});
收藏夹列表适配器
public class CollectionListAdapter extends BaseAdapter {
private Context context;
private int layout;
private ArrayList<Collectable> collectablesList;
public CollectionListAdapter(Context context,int layout,ArrayList<Collectable> collectablesList) {
this.context = context;
this.layout = layout;
this.collectablesList = collectablesList;
}
@Override
public int getcount() {
return collectablesList.size();
}
@Override
public Object getItem(int position) {
return collectablesList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
ImageView imageView;
TextView txtName,txtBox,txtProd,txtDate,txtStore,txtCountry;
}
@Override
public View getView(int position,ViewGroup viewGroup) {
View row = view;
ViewHolder holder = new ViewHolder();
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INflaTER_SERVICE);
row = inflater.inflate(layout,null);
holder.txtName = (TextView) row.findViewById(R.id.txtName);
holder.txtBox = (TextView) row.findViewById(R.id.txtBox);
holder.txtProd = (TextView) row.findViewById(R.id.txtProd);
holder.txtDate = (TextView) row.findViewById(R.id.txtDate);
holder.txtStore = (TextView) row.findViewById(R.id.txtStore);
holder.txtCountry = (TextView) row.findViewById(R.id.txtCountry);
holder.imageView = (ImageView) row.findViewById(R.id.imgCollectable);
row.setTag(holder);
}
else {
holder = (ViewHolder) row.getTag(); //problem child
}
Collectable collectable = collectablesList.get(position);
holder.txtName.setText(collectable.getName());
holder.txtBox.setText(collectable.getBox());
holder.txtProd.setText(collectable.getProd());
holder.txtDate.setText(collectable.getDate());
holder.txtStore.setText(collectable.getStore());
holder.txtCountry.setText(collectable.getcountry());
byte[] collectableImage = collectable.getImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(collectableImage,collectableImage.length);
holder.imageView.setImageBitmap(bitmap);
return row;
}
}
图像轮播
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="400dp"
android:layout_gravity="center"
android:layout_marginTop="20dp">
<ImageView
android:id="@+id/imgCollectable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
app:srcCompat="@drawable/defaultimg" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_favorite_border"
android:layout_alignRight="@id/imgCollectable"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"/>
<View
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="@color/darkgrey"
android:alpha="0.7"
android:layout_alignParentBottom="true"/>
<TextView
android:id="@+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No Name Inserted"
android:textAlignment="center"
android:fontFamily="@font/regular"
android:textColor="@color/white"
android:textSize="14sp"
android:layout_alignParentBottom="true"/>
<!--android:textColor="#00000000"-->
<TextView
android:id="@+id/txtProd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="TextView"
android:textColor="#00000000"/>
<TextView
android:id="@+id/txtBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="TextView"
android:textColor="#00000000"/>
<TextView
android:id="@+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="TextView"
android:textColor="#00000000"/>
<TextView
android:id="@+id/txtStore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="TextView"
android:textColor="#00000000"/>
<TextView
android:id="@+id/txtCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="TextView"
android:textColor="#00000000"/>
</RelativeLayout>
</LinearLayout>
getData()方法
public void insertData(String name,String box,String prod,String date,String store,String country,byte[] image) {
SQLiteDatabase database = getwritabledatabase();
String sql = "INSERT INTO TABLE_COLLECTION VALUES (NULL,?,?) ";
SQLiteStatement statement = database.compileStatement(sql);
statement.clearBindings();
statement.bindString(1,name);
statement.bindString(2,box);
statement.bindString(3,prod);
statement.bindString(4,date);
statement.bindString(5,store);
statement.bindString(6,country);
statement.bindBlob(7,image);
statement.executeInsert();
}
public Cursor getData(String sql) {
SQLiteDatabase database = getReadableDatabase();
return database.rawQuery(sql,null);
}
没有错误,只需要帮助:)
预先感谢