列表(myclass)初始化程序vb.net

时间:2016-08-23 05:08:31

标签: .net initializer

我有一个这样的课程

enter codepackage com.mohit.mom.multipleimage;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
    private Button upload, pick;
    private ProgressDialog dialog;
    MultipartEntity entity;
    GridView gv;
    int count = 0;
    public ArrayList<String> map = new ArrayList<String>();
    Bundle b;
    TextView noImage;
    HttpEntity resEntity;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
        b = getIntent().getExtras();

        noImage = (TextView) findViewById(R.id.noImage);
        upload = (Button) findViewById(R.id.btnUpload);
        pick = (Button) findViewById(R.id.btnPicture);
        gv = (GridView) findViewById(R.id.gridview);
        gv.setAdapter(new ImageAdapter(this));

        if (b != null)
        {
            ArrayList<String> ImgData = b.getStringArrayList("IMAGE");
            for (int i = 0; i < ImgData.size(); i++)
            {
                map.add(ImgData.get(i).toString());
            }
        } else
        {
            noImage.setVisibility(View.VISIBLE);
        }

        upload.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v)
            {
                new ImageUploadTask().execute(count + "", "pk" + count + ".jpg");
            }
        });

        pick.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent i3 = new Intent(MainActivity.this, UploadActivity.class);
                startActivity(i3);
            }
        });

    }

    class ImageUploadTask extends AsyncTask<String, Void, String> {

        String sResponse = null;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            dialog = ProgressDialog.show(MainActivity.this, "Uploading",
                    "Please wait...", true);
            dialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            try {

                String url = "yours url";
                int i = Integer.parseInt(params[0]);
                Bitmap bitmap = decodeFile(map.get(i));
                HttpClient httpClient = new DefaultHttpClient();
                HttpContext localContext = new BasicHttpContext();
                HttpPost httpPost = new HttpPost(url);
                entity = new MultipartEntity();

                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmap.compress(CompressFormat.JPEG, 50, bos);
                byte[] data = bos.toByteArray();

                entity.addPart("user_id", new StringBody("199"));
                entity.addPart("club_id", new StringBody("10"));
                entity.addPart("club_image", new ByteArrayBody(data,
                        "image/jpeg", params[1]));

                httpPost.setEntity(entity);
                HttpResponse response = httpClient.execute(httpPost);
                resEntity = response.getEntity();
                sResponse = EntityUtils.toString(resEntity);
                //HttpResponse response = httpClient.execute(httpPost,localContext);
                //sResponse = EntityUtils.getContentCharSet(response.getEntity());

                System.out.println("sResponse : " + sResponse);
            } catch (Exception e) {
                if (dialog.isShowing())
                    dialog.dismiss();
                Log.e(e.getClass().getName(), e.getMessage(), e);

            }
            return sResponse;
        }

        @Override
        protected void onPostExecute(String sResponse) {
            try {
                if (dialog.isShowing())
                    dialog.dismiss();

                if (sResponse != null) {
                    Toast.makeText(getApplicationContext(),
                            sResponse + " Photo uploaded successfully",
                            Toast.LENGTH_SHORT).show();
                    count++;
                    if (count < map.size()) {
                        new ImageUploadTask().execute(count + "", "hm" + count
                                + ".jpg");
                    }
                }

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.getMessage(),
                        Toast.LENGTH_LONG).show();
                Log.e(e.getClass().getName(), e.getMessage(), e);
            }

        }
    }

    public Bitmap decodeFile(String filePath) {
// Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
        final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2);
        return bitmap;
    }

    private class ImageAdapter extends BaseAdapter
    {
        private Context mContext;

        public ImageAdapter(Context c)
        {
            mContext = c;
        }

        public int getCount()
        {
            return map.size();
        }

        public Object getItem(int position)
        {
            return null;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null)
            { // if it's not recycled, initialize some
                // attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85,
                        Gravity.CENTER));
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setPadding(1, 1, 1, 1);

            }
            else
            {
                imageView = (ImageView) convertView;
            }
            //Bitmap bm=BitmapFactory.decodeFile(map.get(position));
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;
            Bitmap bmp = BitmapFactory.decodeFile(map.get(position),options);
            imageView.setImageBitmap(bmp);
            return imageView;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }
    }

    @Override
    public void onBackPressed() {
// TODO Auto-generated method stub
        super.onBackPressed();
        MainActivity.this.finish();
    }
}

我如何用两个属性的字符串数组初始化这个类?

这就是我正在尝试的,这显然是错误的

Class Myclass
  itemdata as string
  name as string
End Class

初始化后我需要这样的东西,列表中每个项目的值就像是像这样手动分配

Dim ls As New List(Of Myclass)(New Myclass() {("A1,A2,A3,A4".Split(","))})

2 个答案:

答案 0 :(得分:1)

Myclass开始是一个保留名称,因此我使用了Myclass2

这是我最接近您的代码:

Dim dicOpts = New Dictionary(Of String, String) From {{"foo", "bar,woo"}}
Dim key = "foo"
Dim ls As New List(Of Myclass2) From { New Myclass2() With { .itemdata = dicOpts(key).Split(","c)(0), .name = dicOpts(key).Split(","c)(1) } }

这给出了:

result

根据您的修改,我认为这更符合您的要求:

Dim text = "A1,A2,A3,A4"
Dim ls = _
    text _
        .Split(","c) _
        .Select(Function (x) New Myclass2() With { .itemdata = x, .name = x }) _
        .ToList()

我现在知道了:

result2

答案 1 :(得分:-1)

我已将您的班级重命名为......希望您能理解您的意思:

创建您的班级:

Class yyy
    Private lf As New List(Of xxx)
    Sub CallOrWhatEver()
        lf.Add(New xxx("itemdata", "name"))
        lf.Add(New xxx("p1value","p2value","p3value"))
    End Sub
End Class

我用2次重载改变了SubNew-Method ...现在你需要用参数(或没有)来调用类的实例:

EmployeeModel