当wi-fi关闭时,将图像传递给下一个活动

时间:2016-06-24 09:49:47

标签: android sqlite android-sqlite picasso

在我的应用程序中,我将图像存储在SQLite中并从那里读取它。我可以使用毕加索在recyclerview中显示它。但是,当我通过点击一行来启动新活动时,图像不会显示。

这是我的代码。

从这个适配器我得到图像和数据库中的其他数据。

   data: "name=test",

然后我在新活动中传递数据。

public class MySQLAdapter extends RecyclerView.Adapter<MySQLAdapter.RowHolderClass> {

private List<SQLiteModel> myList;
private Context mContext;
private int focused;

public MySQLAdapter(Activity activity,ArrayList<SQLiteModel> ss) {
    this.myList = ss;
    this.mContext=activity;
}

@Override
public MySQLAdapter.RowHolderClass onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.sqlite_adapter, null);

    final RowHolderClass holder = new RowHolderClass(v);
    return holder;
}

@Override
public void onBindViewHolder(RowHolderClass holder, int position) {

    final SQLiteModel listItems = myList.get(position);
    holder.itemView.setSelected(focused==position);

    Picasso.with(holder.imageView.getContext())
            .load(myList.get(position).getSqliteImage()).into(holder.imageView);

    holder.fName.setText(myList.get(position).getSqliteFirstName());
    holder.lName.setText(myList.get(position).getSqliteSecondName());
    holder.jobRole.setText(myList.get(position).getSqliteRole());
    //holder.imageView.setImageResource(Integer.parseInt(myList.get(position).getSqliteImage()));
    holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String first_name = listItems.getSqliteFirstName();
            String last_name = listItems.getSqliteSecondName();
            String job_role = listItems.getSqliteRole();
            String image_url = listItems.getSqliteImage();
            String id= String.valueOf(listItems.getSqliteId());
            //Log.v("id",id);

            Intent i = new Intent(mContext, NoInternetDetailedActivity.class);
            i.putExtra("firstName",first_name);
            i.putExtra("lastName",last_name);
            i.putExtra("jobRole",job_role);
            i.putExtra("imageUrl",image_url);
            i.putExtra("memberId",id);

            mContext.startActivity(i);

        }
    });


}


@Override
public int getItemCount() {
    return myList.size();
}


//ViewHolder pattern used to read the data of the row fast.
class RowHolderClass extends RecyclerView.ViewHolder {

    protected ImageView imageView;
    protected TextView fName, lName, jobRole;
    protected RelativeLayout relativeLayout;

    public RowHolderClass(View itemView) {
        super(itemView);

        this.relativeLayout = (RelativeLayout) itemView.findViewById(R.id.recLayout);
        this.imageView = (ImageView) itemView.findViewById(R.id.imageView);
        this.fName = (TextView) itemView.findViewById(R.id.firstName);
        this.lName = (TextView) itemView.findViewById(R.id.lastName);
        this.jobRole = (TextView) itemView.findViewById(R.id.roleView);

    }
}
}

我得到的例外是:

public class NoInternetDetailedActivity extends AppCompatActivity {
ImageView imageView;

TextView textView0,textView1,textView2,textView3;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_no_internet_detailed);


    Intent i = getIntent();
    String memberId = i.getExtras().getString("memberId");
    String firstName = i.getExtras().getString("firstName");
    String lastName = i.getExtras().getString("lastName");
    String jobRole = i.getExtras().getString("jobRole");
    String image = i.getExtras().getString("imageUrl");

    //imageView = (ImageView)findViewById(R.id.newImageView);
    textView0 = (TextView)findViewById(R.id.textView0);
    textView1 = (TextView)findViewById(R.id.textView1);
    textView2 = (TextView)findViewById(R.id.textView2);
    textView3 = (TextView)findViewById(R.id.textView3);

    textView0.setText("Member id: "+ memberId);
    textView1.setText(firstName);
    textView2.setText(lastName);
    textView3.setText(jobRole);

    //This line gives me the exception!
    Picasso.with(this).load(image).into(imageView);
  }
}

有什么想法吗?

谢谢,

西奥。

1 个答案:

答案 0 :(得分:1)

/*Use this aproach*/
WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);

 /* wifi is not enabled */

if (!wifi.isWifiEnabled()){

     imageView.buildDrawingCache();
     Bitmap bitmap = imageView.getDrawingCache();

     Intent intent = new Intent(this, NewActivity.class);
     intent.putExtra("BitmapImage", bitmap);
}

/* Receive image on second activity */
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");