[英]Android is failing to load image with Picasso and ArrayAdapter into SwipeFlingAdapterView
Picasso 在不使用 ArrayAdapter 或 SwipFlingAdapterView 的情况下加载到布局中的 ImageView 时实际上是成功的,但无法加载到使用 ArrayAdapter 加载到 SwipeFlingAdapterView 的视图中。 我不知道有什么区别。
我已经使用 setImageResource() 成功地将图像加载到 SwipFlingAdapter 和 ArrayAdapter 并加载 R.MipMap.ic_launcher。
没有错误会破坏应用程序。
主要活动:
import ...
public class MainActivity extends AppCompatActivity {
private Cards cards_data[];
private MyArrayAdapter arrayAdapter;
ListView listView;
List<Cards> rowItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rowItems = new ArrayList<Cards>();
arrayAdapter = new MyArrayAdapter(this, R.layout.item, rowItems);
SwipeFlingAdapterView flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
flingContainer.setAdapter(arrayAdapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
@Override
public void removeFirstObjectInAdapter() {
// this is the simplest way to delete an object from the Adapter (/AdapterView)
Log.d("LIST", "removed object!");
rowItems.remove(0);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onLeftCardExit(Object dataObject) {
Cards obj = (Cards) dataObject;
Toast.makeText(MatchingActivity.this, "Left!", Toast.LENGTH_SHORT).show();
}
@Override
public void onRightCardExit(Object dataObject) {
Cards obj = (Cards) dataObject;
Toast.makeText(MatchingActivity.this, "Right!", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
}
@Override
public void onScroll(float scrollProgressPercent) {
}
});
}
牌:
public class Cards {
private String imageUrl;
public Cards (String imageUrl) {
this.profileImageUrl = imageUrl;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
自定义适配器:
import ...
public class MyArrayAdapter extends ArrayAdapter<Cards> {
Context context;
public MyArrayAdapter(Context context, int resourceId, List<Cards> items) {
super(context, resourceId, items);
}
public View getView(int position, View convertView, ViewGroup parent) {
Cards card_item = getItem(position);
if(convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false);
}
ImageView image = (ImageView) convertView.findViewById(R.id.image);
Picasso.get().load(card_item.getImageUrl()).into(image);
return convertView;
}
}
XML 项目:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="40sp"
android:paddingRight="40sp"
android:paddingTop="20sp"
android:paddingBottom="20sp"
android:outlineProvider="bounds"
android:clipToPadding="false">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:cardCornerRadius="4dp"
android:elevation="2dp"
android:id="@+id/cardView">
<LinearLayout
android:layout_gravity="center"
android:layout_width="300dp"
android:layout_height="350dp"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/image"
android:scaleType="cen
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Picasso 在不使用 ArrayAdapter 或 SwipFlingAdapterView 的情况下加载到布局中的 ImageView 时实际上是成功的,但无法加载到使用 ArrayAdapter 加载到 SwipeFlingAdapterView 的视图中。 我不知道有什么区别。
我已经使用 setImageResource() 成功地将图像加载到 SwipFlingAdapter 和 ArrayAdapter 并加载 R.MipMap.ic_launcher。
没有错误会破坏应用程序。
主要活动:
import ...
public class MainActivity extends AppCompatActivity {
private Cards cards_data[];
private MyArrayAdapter arrayAdapter;
ListView listView;
List<Cards> rowItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rowItems = new ArrayList<Cards>();
arrayAdapter = new MyArrayAdapter(this, R.layout.item, rowItems);
SwipeFlingAdapterView flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
flingContainer.setAdapter(arrayAdapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
@Override
public void removeFirstObjectInAdapter() {
// this is the simplest way to delete an object from the Adapter (/AdapterView)
Log.d("LIST", "removed object!");
rowItems.remove(0);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onLeftCardExit(Object dataObject) {
Cards obj = (Cards) dataObject;
Toast.makeText(MatchingActivity.this, "Left!", Toast.LENGTH_SHORT).show();
}
@Override
public void onRightCardExit(Object dataObject) {
Cards obj = (Cards) dataObject;
Toast.makeText(MatchingActivity.this, "Right!", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
}
@Override
public void onScroll(float scrollProgressPercent) {
}
});
}
牌:
public class Cards {
private String imageUrl;
public Cards (String imageUrl) {
this.profileImageUrl = imageUrl;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
自定义适配器:
import ...
public class MyArrayAdapter extends ArrayAdapter<Cards> {
Context context;
public MyArrayAdapter(Context context, int resourceId, List<Cards> items) {
super(context, resourceId, items);
}
public View getView(int position, View convertView, ViewGroup parent) {
Cards card_item = getItem(position);
if(convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false);
}
ImageView image = (ImageView) convertView.findViewById(R.id.image);
Picasso.get().load(card_item.getImageUrl()).into(image);
return convertView;
}
}
XML 项目:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="40sp"
android:paddingRight="40sp"
android:paddingTop="20sp"
android:paddingBottom="20sp"
android:outlineProvider="bounds"
android:clipToPadding="false">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:cardCornerRadius="4dp"
android:elevation="2dp"
android:id="@+id/cardView">
<LinearLayout
android:layout_gravity="center"
android:layout_width="300dp"
android:layout_height="350dp"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/image"
android:scaleType="cen
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.