我是这个Android世界的新手..请帮助... 我正在使用GridView编写我自己的自定义适配器来填充图像..但每次上面的错误都会导致我的应用程序崩溃。关闭我的MainActivity文件,COdeAdapterFile
这是MainActivityCode com.example.android.internship;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import java.util.ArrayList;
public class Handicrafts extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
//THIS IS ARTIFACTS FILE
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
NavigationView nv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_handicrafts);
nv=(NavigationView)findViewById(R.id.mynavigation);
nv.setNavigationItemSelectedListener(this);
mToolbar=(Toolbar)findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
mDrawerLayout=(DrawerLayout) findViewById(R.id.drawerLayout);
mToggle=new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
final ArrayList<Items> itemObject = new ArrayList<Items>();
itemObject.add(new Items("",R.drawable.a1));
itemObject.add(new Items("",R.drawable.a2));
itemObject.add(new Items("",R.drawable.a3));
itemObject.add(new Items("",R.drawable.a4));
itemObject.add(new Items("",R.drawable.a5));
itemObject.add(new Items("",R.drawable.a6));
itemObject.add(new Items("",R.drawable.a7));
itemObject.add(new Items("",R.drawable.a8));
itemObject.add(new Items("",R.drawable.a9));
itemObject.add(new Items("",R.drawable.a10));
itemObject.add(new Items("",R.drawable.a11));
itemObject.add(new Items("",R.drawable.a12));
itemObject.add(new Items("",R.drawable.a13));
itemObject.add(new Items("",R.drawable.a14));
itemObject.add(new Items("",R.drawable.a15));
itemObject.add(new Items("",R.drawable.a16));
itemObject.add(new Items("",R.drawable.a17));
itemObject.add(new Items("",R.drawable.a18));
itemObject.add(new Items("",R.drawable.a19));
ItemAdapter adapter = new ItemAdapter(this, itemObject);
// Get a reference to the ListView, and attach the adapter to the listView.
GridView newGrdidView = (GridView) findViewById(R.id.gridview);
newGrdidView.setAdapter(adapter);
final int []images={R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6,R.drawable.a7,R.drawable.a8,
R.drawable.a9,R.drawable.a10,R.drawable.a11,R.drawable.a12,R.drawable.a13,R.drawable.a14,R.drawable.a15,R.drawable.a16,R.drawable.a17,
R.drawable.a18,R.drawable.a19,};
newGrdidView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Handicrafts.this,
Main3Activity.class);
int mImageId =0;
intent.putExtra("img",images[position]);
startActivity(intent);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item))
{return true;}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId())
{
case R.id.paint:
Intent paintIntent=new Intent(Handicrafts.this,MainActivity.class);
startActivity(paintIntent);
break;
case R.id.covers:
Intent collageIntent=new Intent(Handicrafts.this,collages.class);
startActivity(collageIntent);
break;
case R.id.artifacts:
Intent craftsIntent=new Intent(Handicrafts.this,Handicrafts.class);
startActivity(craftsIntent);
break;
}
return true;
}
}
这是customAdapter代码
package com.example.android.internship;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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 java.util.ArrayList;
import static android.R.attr.id;
import static android.R.attr.resource;
/**
* Created by hatim on 6/6/2017.
*/
public class ItemAdapter extends ArrayAdapter<Items> {
public ItemAdapter(Activity context,ArrayList<Items> itemObject) {
super(context,0,itemObject);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View gridview=convertView;
if(gridview==null)
{
gridview = LayoutInflater.from(getContext()).inflate(
R.layout.item_layout, parent, false);
}
Items currentItems = getItem(position);
TextView nameTextView = null;
if (gridview != null) {
nameTextView = (TextView) gridview.findViewById(R.id.product_name);
}
// Get the version name from the current AndroidFlavor object and
// set this text on the name TextView
nameTextView.setText(currentItems.getItemName());
ImageView iconView = null;
if (gridview != null) {
iconView = (ImageView) gridview.findViewById(R.id.product_images);
}
// Get the image resource ID from the current AndroidFlavor object and
// set the image to iconView
iconView.setImageResource(currentItems.getImageId());
return gridview;
}
}
这是我的Android清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.internship">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:hardwareAccelerated="false"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" />
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main3Activity" />
<activity
android:name=".collages"
android:label="Cushion covers" />
<activity
android:name=".Handicrafts"
android:label="Artefacts" />
</application>
</manifest>
请帮助我!!
答案 0 :(得分:0)
尝试使用RecyclerView
和ViewHolder
方法,您还可以使用Picasso
或Glide
库来平滑将图片加载到ImageView
。
您想以网格方式显示图像,因此我认为您可以使用RecyclerView
并以堆栈格式制作RecyclerView
LayoutManager
。
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:recyclerview-v7:25.3.1'
看看这个 - https://soundcloud.com/daptone-records/sharon-jones-the-dap-kings-white-christmas 如果出现任何问题,请告诉我们!
<强> your_layout.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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="and.com.bakingtime.activity.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view" />
</LinearLayout>
<强> list_item.xml 强>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="80dp"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="8dp"
android:layout_marginBottom="16dp">
<ImageView
android:id="@+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />
</android.support.v7.widget.CardView>
<强> your_adapter_class.java 强>
public class your_adapter_class extends RecyclerView.Adapter<RecyclerViewHolders> {
private List<ItemObject> itemList;
private Context context;
public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
this.itemList = itemList;
this.context = context;
}
@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
Picasso.with(context).load((itemList.get(position).getPhoto()).into(holder.thumbnail);
}
@Override
public int getItemCount() {
return this.itemList.size();
}
}
<强> your_ViewHolder.java 强>
public class your_ViewHolder extends RecyclerView.ViewHolder{
public ImageView thumbnail;
public RecyclerViewHolders(View itemView) {
super(itemView);
thumbnail = (ImageView)itemView.findViewById(R.id.photo);
}
}
<强> MainActivity.java 强>
在MainActivity onCreate
制作GridLayoutManager
并将其附加到recyclerView
private GridLayoutManager lLayout;
// inside onCreate
List<ItemObject> rowListItem = getAllItemList();
lLayout = new GridLayoutManager(MainActivity.this, 2);
RecyclerView rView = (RecyclerView)findViewById(R.id.recycler_view);
rView.setLayoutManager(lLayout);
RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(MainActivity.this, rowListItem);
rView.setAdapter(rcAdapter);
//getAllItemList method
private List<ItemObject> getAllItemList(){
List<ItemObject> itemObject = new ArrayList<ItemObject>();
itemObject.add(new ItemObject("",R.drawable.a1));
itemObject.add(new ItemObject("",R.drawable.a2));
itemObject.add(new ItemObject("",R.drawable.a3));
itemObject.add(new ItemObject("",R.drawable.a4));
itemObject.add(new ItemObject("",R.drawable.a5));
itemObject.add(new ItemObject("",R.drawable.a6));
itemObject.add(new ItemObject("",R.drawable.a7));
itemObject.add(new ItemObject("",R.drawable.a8));
itemObject.add(new ItemObject("",R.drawable.a9));
itemObject.add(new ItemObject("",R.drawable.a10));
itemObject.add(new ItemObject("",R.drawable.a11));
itemObject.add(new ItemObject("",R.drawable.a12));
itemObject.add(new ItemObject("",R.drawable.a13));
itemObject.add(new ItemObject("",R.drawable.a14));
itemObject.add(new ItemObject("",R.drawable.a15));
itemObject.add(new ItemObject("",R.drawable.a16));
itemObject.add(new ItemObject("",R.drawable.a17));
itemObject.add(new ItemObject("",R.drawable.a18));
itemObject.add(new ItemObject("",R.drawable.a19));
return ItemObject;
}