我正在创建一个数据库中保存数据的应用程序。我没有使用recyclerview adapter在recyclerview中显示数据。不知道应该编写什么代码。请检查。
这是我的文件:MainActivity.java
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView applist;
ArrayList<Guides> guides;
GuideAdapter adapter;
GuideDB guideDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
guideDB = new GuideDB(this);
guides = new ArrayList<>();
applist = (RecyclerView) findViewById(R.id.app_list);
applist.setHasFixedSize(true);
applist.setLayoutManager(new LinearLayoutManager(this));
adapter = new GuideAdapter(this,guides);
applist.setAdapter(adapter);
try {
Cursor cursor = guideDB.getGuides("SELECT * FROM GUIDE_LIST");
while (cursor.moveToNext()){
int guide_id = cursor.getInt(0);
String post_tile = cursor.getString(1);
String post_desc = cursor.getString(2);
String post_address = cursor.getString(3);
byte [] post_image = cursor.getBlob(4);
Guides g = new Guides(guide_id,post_tile,post_desc,post_address,post_image);
guides.add(g);}
}catch (Exception e){e.printStackTrace();}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_add){
startActivity(new Intent(MainActivity.this,PostActivity.class));
}
return super.onOptionsItemSelected(item);
}
}
PostActivity.java
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class PostActivity extends AppCompatActivity {
public ImageButton mSelectImage;
public EditText mPostTitle;
public EditText mPostDesc;
public EditText mPostAddress;
public Button mSubmitbtn;
private static final int GALLERY_REQUEST= 1;
GuideDB guideDB;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
guideDB = new GuideDB(this);
mSelectImage = (ImageButton) findViewById(R.id.imageSelect);
mPostTitle = (EditText) findViewById(R.id.titleField);
mPostDesc = (EditText) findViewById(R.id.descField);
mPostAddress = (EditText) findViewById(R.id.addressField);
mSubmitbtn = (Button) findViewById(R.id.submitButton);
mSelectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent,GALLERY_REQUEST);
}
});
mSubmitbtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
if(!mPostTitle.getText().toString().isEmpty() && !mPostDesc.getText().toString().isEmpty() && !mPostAddress.getText().toString().isEmpty())
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,50,outputStream);
byte[] byteArray = outputStream.toByteArray();
try {
guideDB.addGuide(mPostTitle.getText().toString(),
mPostDesc.getText().toString(),
mPostAddress.getText().toString(),byteArray
);
Toast.makeText(getApplicationContext(),"Added successfully!",Toast.LENGTH_LONG).show();
}catch (Exception e){e.printStackTrace();}
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK) {
Uri mimageUri = data.getData();
try {
InputStream inputstream = getContentResolver().openInputStream(mimageUri);
bitmap = BitmapFactory.decodeStream(inputstream);
mSelectImage.setImageBitmap(bitmap);
}catch (FileNotFoundException e){e.printStackTrace();}
}
}
}
GuideDB.java
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
/**
* Created by 291 on 14.12.2017.
*/
public class GuideDB extends SQLiteOpenHelper {
private static final int DATABASE_VERSION= 1;
private static final String DATABASE_NAME= "guide_db";
private static final String TABLE_NAME = "GUIDE_LIST";
private static String GUIDE_ID = "guide_id";
private static String GUIDE_TITLE = "guide_title";
private static String GUIDE_DESC = "guide_desc";
private static String GUIDE_ADDRESS = "guide_address";
private static String GUIDE_IMG = "guides_image";
public GuideDB(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE" + TABLE_NAME + "("
+ GUIDE_ID + "INTEGER PRIMARY KEY AUTOINCREMENT"
+ GUIDE_TITLE + "TEXT"
+ GUIDE_DESC + "TEXT"
+ GUIDE_ADDRESS + "TEXT"
+ GUIDE_IMG + "BLOB" + ")";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);
onCreate(db);
}
public void addGuide (String title,String desc,String address,byte[] image){
SQLiteDatabase gdb = getWritableDatabase();
try
{
String sql = "INSERT INTO GUIDE_LIST VALUES(NULL,?,?,?)";
SQLiteStatement statement = gdb.compileStatement(sql);
statement.clearBindings();
statement.bindString(1,title);
statement.bindString(2,desc);
statement.bindString(3,address);
statement.bindBlob(4,image);
statement.execute();
}catch (Exception e){e.printStackTrace();}
}
public Cursor getGuides(String sql){
SQLiteDatabase gdb = getReadableDatabase();
return gdb.rawQuery(sql,null);
}
}
GuideAdapter.java
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by 291 on 14.12.2017.
*/
public class GuideAdapter extends RecyclerView.Adapter<GuideAdapter.ViewHolder> {
private Context ctx;
private ArrayList<Guides> guidelist;
public GuideAdapter(Context ctx, ArrayList<Guides> guidelist) {
this.ctx = ctx;
this.guidelist = guidelist;
}
@Override
public GuideAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.guide_row,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(GuideAdapter.ViewHolder holder, int position) {
Guides guides = guidelist.get(position);
holder.psttitle.setText(guides.getPost_title());
holder.pstdesc.setText(guides.getPost_desc());
holder.pstaddres.setText(guides.getPost_address());
byte [] postimg = guides.getPost_image();
Bitmap bitmap = BitmapFactory.decodeByteArray(postimg,0,postimg.length);
holder.pstimg.setImageBitmap(bitmap);
}
@Override
public int getItemCount() {
return guidelist.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public ImageView pstimg;
public TextView psttitle;
public TextView pstdesc;
public TextView pstaddres;
public int id;
public ViewHolder(View itemView) {
super(itemView);
pstimg = (ImageView) itemView.findViewById(R.id.post_image);
psttitle = (TextView) itemView.findViewById(R.id.post_title);
pstdesc = (TextView) itemView.findViewById(R.id.post_title);
pstaddres = (TextView) itemView.findViewById(R.id.post_address);
}
}
}
Guides.java
public class Guides {
private int guide_id;
private String post_title,post_desc,post_address;
private byte [] post_image;
public Guides(int guide_id, String post_title, String post_desc, String post_address, byte [] post_image) {
this.guide_id = guide_id;
this.post_title = post_title;
this.post_desc = post_desc;
this.post_address = post_address;
this.post_image = post_image;
}
public int getGuide_id() {
return guide_id;
}
public void setGuide_id(int guide_id) {
this.guide_id = guide_id;
}
public String getPost_title() {
return post_title;
}
public void setPost_title(String post_title) {
this.post_title = post_title;
}
public String getPost_desc() {
return post_desc;
}
public void setPost_desc(String post_desc) {
this.post_desc = post_desc;
}
public String getPost_address() {
return post_address;
}
public void setPost_address(String post_address) {
this.post_address = post_address;
}
public byte[] getPost_image() {
return post_image;
}
public void setPost_image(byte[] post_image) {
this.post_image = post_image;
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ugurcangursen.guideappsqlite.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/app_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="10dp"></android.support.v7.widget.RecyclerView>
</RelativeLayout>
activity_post.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ugurcangursen.guideappsqlite.PostActivity">
<ImageButton
android:id="@+id/imageSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="#00ffffff"
android:scaleType="centerCrop"
app:srcCompat="@drawable/add_btn" />
<EditText
android:id="@+id/titleField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageSelect"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/input_outline"
android:hint="Post Title..."
android:inputType="textPersonName"
android:padding="10dp"
android:singleLine="true" />
<EditText
android:id="@+id/descField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/titleField"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/input_outline"
android:hint="Post Description..."
android:inputType="textMultiLine"
android:padding="10dp" />
<EditText
android:id="@+id/addressField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/descField"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/input_outline"
android:hint="Adres..."
android:inputType="textMultiLine"
android:padding="10dp" />
<Button
android:id="@+id/submitButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@color/colorPrimary"
android:text="SUBMIT POST"
android:textColor="@android:color/white" />
</RelativeLayout>
guide_row.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/post_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:srcCompat="@drawable/add_btn" />
<TextView
android:id="@+id/post_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:textSize="16dp"
android:textStyle="bold"
tools:text="Başlık" />
<TextView
android:id="@+id/post_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
tools:text="Açıklama" />
<TextView
android:id="@+id/post_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
tools:text="Adres" />
</LinearLayout>
</android.support.v7.widget.CardView>
答案 0 :(得分:0)
我没有查看您发布的所有代码。但是,您确实使用空源(指南)设置了adpater,因此它不会显示任何内容。
以下版本的onCreate
方法可能会导致数据显示
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
guideDB = new GuideDB(this);
guides = new ArrayList<>();
applist = (RecyclerView) findViewById(R.id.app_list);
applist.setHasFixedSize(true);
applist.setLayoutManager(new LinearLayoutManager(this));
//adapter = new GuideAdapter(this,guides); //<<<< MOVED
//applist.setAdapter(adapter); //<<<< MOVED
try {
Cursor cursor = guideDB.getGuides("SELECT * FROM GUIDE_LIST");
while (cursor.moveToNext()){
int guide_id = cursor.getInt(0);
String post_tile = cursor.getString(1);
String post_desc = cursor.getString(2);
String post_address = cursor.getString(3);
byte [] post_image = cursor.getBlob(4);
Guides g = new Guides(guide_id,post_tile,post_desc,post_address,post_image);
guides.add(g);}
}catch (Exception e){e.printStackTrace();}
adapter = new GuideAdapter(this,guides); //<<<< MOVED
applist.setAdapter(adapter); //<<<< MOVED
}