如何在自定义ListView

时间:2016-11-24 17:53:34

标签: android android-layout listview github android-studio-2.2

我是一个新的Android我想制作一个Documents Scanner应用程序。所以我用作裁剪功能GitHub库知道我想在列表View上显示裁剪图像。为此,我也使用github上的通用图像加载器库,但仍然出现错误。这个库我把它放在一个libs文件夹

请给我一些简单的解决方案

这里我的主要活动是

public class MainActivity extends AppCompatActivity {
Button btn;
Context context = this;
public static final int REQUEST_Code = 99;
ImageView mImageView;
int prefrences = ScanConstants.OPEN_CAMERA;
ListView mListView;
ArrayAdapter<String > images_adapter;
ImageLoader imageLoader;
ArrayList images_Uri;
DisplayImageOptions options;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mListView= (ListView) findViewById(R.id.mListView);
    btn = (Button) findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showdialogebox();

        }
    });


}

private void showdialogebox() {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Take Phote");
    final String item[] = {"Take form cam", "Take form file"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,    android.R.layout.select_dialog_item, item);
    AlertDialog.Builder builder1 = builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            if (which == 0) {
                Intent intent = new Intent(getApplicationContext(), ScanActivity.class);
                intent.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE, prefrences);
                startActivityForResult(intent, REQUEST_Code);

            }

        }
    });
    builder.show();
}


@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Uri uri = data.getExtras().getParcelable(ScanConstants.SCANNED_RESULT);

     getContentResolver().delete(uri, null, null);

    images_Uri.add(uri);
    Image_Adapter adapter=new Image_Adapter(getApplicationContext(),R.layout.single_image_view,images_Uri);
    mListView.setAdapter(adapter);
}
}

这是我的Image_Adapter类代码

public class Image_Adapter extends ArrayAdapter<ViewHolderImage> {
Context context;
int resourceid;
public final static String IMAGE_PATH = Environment
        .getExternalStorageDirectory().getPath() + "/scanSample";
ArrayList images=null;
public Image_Adapter(Context context, int resource, ArrayList images) {
    super(context,resource,images);
    this.context=context;
    this.resourceid=resource;
    this.images=images;
}
ViewHolderImage holderImage=null;

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView==null){
        LayoutInflater inflater=((Activity)context).getLayoutInflater();
        convertView=inflater.inflate(resourceid,parent,false);
        holderImage=new ViewHolderImage();
       holderImage.imageView=(ImageView)convertView.findViewById(R.id.mImageView);
        convertView.setTag(holderImage);
    }else 
{
        holderImage=(ViewHolderImage)convertView.getTag();
    }
    holderImage.imageView.setMaxWidth(100);
    holderImage.imageView.setMaxWidth(100);
    ImageLoader imageLoader =null;
    imageLoader.displayImage(IMAGE_PATH,holderImage.imageView);
    return convertView;
}
}

ViewHolderImage类代码

enter code here
  public class ViewHolderImage {
         ImageView imageView;
     }

这是日志猫的错误显示`

    >>>> Normal User
           11-24 22:22:11.180 25628-25628/com.example.hassanjan.documentsscannerapp1 E/dalvikvm:     >>>>> com.example.hassanjan.documentsscannerapp1 [ userId:0 | appId:10236 ]
           11-24 22:22:11.490 25628-25628/com.example.hassanjan.documentsscannerapp1 E/InstantRun: Could not find slices in APK; aborting.
           11-24 22:22:11.720 25628-25628/com.example.hassanjan.documentsscannerapp1 E/dalvikvm:  Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method  android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
           11-24 22:22:16.906 25628-25628/com.example.hassanjan.documentsscannerapp1  E/ViewRootImpl: sendUserActionEvent() mView == null
           11-24 22:22:33.664 25628-25628/com.example.hassanjan.documentsscannerapp1 E/ViewRootImpl: sendUserActionEvent() mView == null
           11-24 22:22:42.223 25628-25628/com.example.hassanjan.documentsscannerapp1 E/AndroidRuntime: FATAL EXCEPTION: main
             Process: com.example.hassanjan.documentsscannerapp1, PID: 25628                                                                                              java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=99, result=-1, data=Intent { (has extras) }} to activity  {com.example.hassanjan.documentsscannerapp1/com.example.hassanjan.documentsscannerapp1.MainActivity}: java.lang.NullPointerException
             at android.app.ActivityThread.deliverResults(ActivityThread.java:3580)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3623)
             at android.app.ActivityThread.access$1400(ActivityThread.java:169)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
             at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:146)
          at android.app.ActivityThread.main(ActivityThread.java:5487)
           at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
         dalvik.system.NativeStart.main(Native Method)
          Caused by: java.lang.NullPointerException
         at    com.example.hassanjan.documentsscannerapp1.MainActivity.onActivityResult(MainActivity.java:114)
          at android.app.Activity.dispatchActivityResult(Activity.java:5643)
          at android.app.ActivityThread.deliverResults(ActivityThread.java:3576)
          at android.app.ActivityThread.handleSendResult(ActivityThread.java:3623) 
             at android.app.ActivityThread.access$1400(ActivityThread.java:169) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
           at android.os.Looper.loop(Looper.java:146) 
       at android.app.ActivityThread.main(ActivityThread.java:5487) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
           at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
     at dalvik.system.NativeStart.main(Native Method) 

**主要活动**上的Hassanjan114错误显示

     images_Uri.add(uri);

1 个答案:

答案 0 :(得分:1)

您可以在listview中设置图像,这样就可以在您的gradel文件中添加compile 'com.github.bumptech.glide:glide:3.7.0',这是我用来将图片视图添加到列表视图的一段代码

 ImageView circleImageView = (ImageView) view.findViewById(R.id.profile_image);

            Mediafileinfo mediafileinfo =(Mediafileinfo)getItem(position);


    Glide.with(getContext()).load(mediafileinfo.getAlbum_art_uri())
            .thumbnail(1f)
            .crossFade()
            .override(50,50)  //(the size of the image)
             .placeholder(R.drawable.music)
             .diskCacheStrategy(DiskCacheStrategy.SOURCE)
             .into(circleImageView);

以下是制作自定义列表视图link

的教程