我试图使用Picasso库,但它无法正常工作。我认为我的错误是Picasso.with(Context)
我可能使用了错误的参数。我包含了MainActivity和ListClass的代码。图片未加载,并且不会在应用程序中显示为空格。
UPDATE !! "我错过了与错误无关。只是错误的副本。它给了我this
MainActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.widget.FrameLayout;
import android.widget.Button;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
ListView list;
imageHandler imagess = new imageHandler();
String[] web = {"Trial "};
Integer[] imageId = { R.drawable.ic_menu_camera };
//Drawable[] imageId = { imagess.LoadImageFromWebOperations("http://egyptianstreets.com/wp-content/uploads/2016/08/satelliteegypt-400x240.jpg")};
Handler handler;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
listClass adapter = new
listClass(MainActivity.this, web, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
}
我使用Picasso库从URL加载图片的ListClass;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.webkit.WebView;
import com.squareup.picasso.Picasso;
public class listClass extends ArrayAdapter<String>{
//Next three lines introduces Variables to use in the class
private final Activity context;
private final String[] web;
private final Integer[] imageId;
//Constructor to get Web, Image and context to set adapter
public listClass(Activity context, String[] web, Integer [] imageId) {
super(context, R.layout.list, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
//Public method to ...
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
Picasso.with(this).load("http://egyptianstreets.com/wp-content/uploads/2016/08/egyptischemoefti.jpg").resize(60,60).into(imageView").resize(50,50).centerCrop().
into(imageView);
return rowView;
}
}
答案 0 :(得分:1)
看起来你得到了&#34;错误。您的图片网址似乎已被错误复制。像&#39; resize(60,60).into(imageView)&#39;是/不应该是图像的URL的一部分。他们可能偶然来了。
使用此
Picasso.with(this).load("http://egyptianstreets.com/wp-content/uploads/2016/08/egyptischemoefti.jpg").resize(60,60).centerCrop().into(imageView);
编辑: 也许你给毕加索的背景是不对的。您需要为它在构造函数中初始化的活动上下文。
也许这个:
Picasso.with(this.context).load("http://egyptianstreets.com/wp-content/uploads/2016/08/egyptischemoefti.jpg").resize(60,60).centerCrop().into(imageView);
答案 1 :(得分:1)
这里的错误是你以错误的方式使用库。
"
在这里无效。
您还使用了.into(imageView)
和resize(..)
两次。
试试这个。
Picasso.with(context).load("http://egyptianstreets.com/wp-content/uploads/2016/08/egyptischemoefti.jpg").resize(60,60).centerCrop().
into(imageView);