Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Mobile Forum > Android - อยากได้คลิกที่รายการแล้วเก็บยอดวิวการเข้าชมครับ ผมทำเป็นแอพดูวีดีโอตามขั้นตอนของไทยครีแอ็ด



 

Android - อยากได้คลิกที่รายการแล้วเก็บยอดวิวการเข้าชมครับ ผมทำเป็นแอพดูวีดีโอตามขั้นตอนของไทยครีแอ็ด

 



Topic : 107933



โพสกระทู้ ( 0 )
บทความ ( 0 )



สถานะออฟไลน์




สวัสดีครับ ผมทำตามขั้นตอนนี้มาครับ ลิ้งนี้เลย
https://www.thaicreate.com/mobile/android-listview-gridview-get-result-from-web-server-and-paging-pagination.html
ของผมทำแบบ Girdview ตามตัวอย่างล่างสุด และได้ประยุกให้เป็นเเอพดูวีดีโอครับ
แต่ที่ต้องการคืออยากได้ยอดวิวในการคลิก ผมต้องใส่ CODE ยังไงครับ ทำไม่เป็นครับ เพิ่งจะหัดทำ ขอบคุณครับ
นี้ Code ผม


Code (Android-Java)
package com.avonline.imageg;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends Activity  {
    
    private GridView gridV;
    private ImageAdapter imageAdapter;
    
    public int currentPage = 1;
    public int TotalPage = 0;
    
	public Button btnNext;
	public Button btnPre;
    
    ArrayList<HashMap<String, Object>> MyArrList = new ArrayList<HashMap<String, Object>>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        // ProgressBar
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        
        setContentView(R.layout.activity_main);

        // GridView and imageAdapter
        gridV = (GridView) findViewById(R.id.gridView1);
        gridV.setClipToPadding(false);
        imageAdapter = new ImageAdapter(getApplicationContext()); 
        gridV.setAdapter(imageAdapter);
        
        
        // Next
        btnNext = (Button) findViewById(R.id.btnNext);
        // Perform action on click
        btnNext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	currentPage = currentPage + 1;
            	ShowData();
            }
        });
        
        // Previous
        btnPre = (Button) findViewById(R.id.btnPre);
        // Perform action on click
        btnPre.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	currentPage = currentPage - 1;
            	ShowData();
            }
        });

        // Show first load
        ShowData();

    }
    
    public void ShowData()
    {
    	btnNext.setEnabled(false);
    	btnPre.setEnabled(false);
    	
        setProgressBarIndeterminateVisibility(true); 
        new LoadContentFromServer().execute();
    }


    class LoadContentFromServer extends AsyncTask<Object, Integer, Object> {
        
        @Override
        protected Object doInBackground(Object... params) {
        	
        	String url = "http://www.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        	
          	JSONArray data;
    			try {
    				data = new JSONArray(getJSONUrl(url));
    				
    		    	MyArrList = new ArrayList<HashMap<String, Object>>();
    				HashMap<String, Object> map;
    				
    				/*
    				 * TotalRows = Show for total rows
    				 * TotalPage = Show for total page
    				 */
    				
    				int displayPerPage = 20;   // Per Page
    				int TotalRows = data.length();
    				int indexRowStart = ((displayPerPage*currentPage)-displayPerPage);
    				
    				if(TotalRows<=displayPerPage)
    				{
    					TotalPage =1;
    				}
    				else if((TotalRows % displayPerPage)==0)
    				{
    					TotalPage =(TotalRows/displayPerPage) ;
    				}
    				else
    				{
    					TotalPage =(TotalRows/displayPerPage)+1;
    					TotalPage = (int)TotalPage;
    				}
    				int indexRowEnd = displayPerPage * currentPage;
    				if(indexRowEnd > TotalRows)
    				{
    					indexRowEnd = TotalRows;
    				}
    				// OnClick
    				gridV.setOnItemClickListener(new OnItemClickListener() {
    					public void onItemClick(AdapterView<?> parent, View v,
    						int position, long id) {
    						
    						String  url = (String) MyArrList.get(position).get("url");
    					    Uri Urls1 = Uri.parse(url);
    						Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
    						intent.setDataAndType(Urls1,"video/mp4");
    						startActivity(intent);
    					}
    		        });
    				
    				for(int i = indexRowStart; i < indexRowEnd; i++){
    	                JSONObject c = data.getJSONObject(i);
    	    			map = new HashMap<String, Object>();
    	    			map.put("ImageID", (String)c.getString("ImageID"));
    	    			map.put("ItemID", (String)c.getString("ItemID"));
    	    			map.put("views", (String)c.getString("views"));
    	    			map.put("url", (String)c.getString("url"));
    	    			
    	    			// Thumbnail Get ImageBitmap To Object
    	    			map.put("ImagePath", (String)c.getString("ImagePath"));
    	    			Bitmap newBitmap = loadBitmap(c.getString("ImagePath"));
    	    			map.put("ImagePathBitmap", newBitmap);

    	    			MyArrList.add(map);
    	    			
    	    			publishProgress(i);
    	    				
    				}
    				
    				
    			} catch (JSONException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
 
            return null;
        }

        @Override
        public void onProgressUpdate(Integer... progress) {
        	imageAdapter.notifyDataSetChanged();
        }
        
        @Override
        protected void onPostExecute(Object result) {
        	
			// Disabled Button Next
			if(currentPage >= TotalPage)
			{
				btnNext.setEnabled(false);
			}
			else
			{
				btnNext.setEnabled(true);
			}
			
			// Disabled Button Previos
			if(currentPage <= 1)
			{
				btnPre.setEnabled(false);
			}
			else
			{
				btnPre.setEnabled(true);
			}
			
            setProgressBarIndeterminateVisibility(false); // When Finish
        }
    }	
  	
	 
    class ImageAdapter extends BaseAdapter {

        private Context mContext; 

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

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

        public Object getItem(int position) { 
            return MyArrList.get(position); 
        } 

        public long getItemId(int position) { 
            return position; 
        } 

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		 
			 
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.activity_column, null); 
			}
			
			// ColPhoto
			ImageView imageView = (ImageView) convertView.findViewById(R.id.ColPhoto);
			imageView.getLayoutParams().height = 150;
			imageView.getLayoutParams().width = 250;
			imageView.setPadding(5, 5, 5, 5);
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        	 try
        	 {
        		 imageView.setImageBitmap((Bitmap)MyArrList.get(position).get("ImagePathBitmap"));
        	 } catch (Exception e) {
        		 // When Error
        		 imageView.setImageResource(android.R.drawable.ic_menu_report_image);
        	 }

 			// ColImageID
 			TextView txtImageID = (TextView) convertView.findViewById(R.id.ColImageID);
 			txtImageID.setPadding(5, 0, 0, 0);
 			txtImageID.setText("" + MyArrList.get(position).get("ImageID").toString());	
 			
			// ColItemID
			TextView txtItemID = (TextView) convertView.findViewById(R.id.ColItemID);
			txtItemID.setPadding(5, 0, 0, 0);
			txtItemID.setText("" + MyArrList.get(position).get("views").toString());	
		 
			return convertView;
				
		}
        
    }
    
    /*** Get JSON Code from URL ***/
  	public String getJSONUrl(String url) {
  		StringBuilder str = new StringBuilder();
  		HttpClient client = new DefaultHttpClient();
  		HttpGet httpGet = new HttpGet(url);
  		try {
  			HttpResponse response = client.execute(httpGet);
  			StatusLine statusLine = response.getStatusLine();
  			int statusCode = statusLine.getStatusCode();
  			if (statusCode == 200) { // Download OK
  				HttpEntity entity = response.getEntity();
  				InputStream content = entity.getContent();
  				BufferedReader reader = new BufferedReader(new InputStreamReader(content));
  				String line;
  				while ((line = reader.readLine()) != null) {
  					str.append(line);
  				}
  			} else {
  				Log.e("Log", "Failed to download file..");
  			}
  		} catch (ClientProtocolException e) {
  			e.printStackTrace();
  		} catch (IOException e) {
  			e.printStackTrace();
  		}
  		return str.toString();
  	}

  	/***** Get Image Resource from URL (Start) *****/
	private static final String TAG = "Image";
	private static final int IO_BUFFER_SIZE = 4 * 1024;
	public static Bitmap loadBitmap(String url) {
	    Bitmap bitmap = null;
	    InputStream in = null;
	    BufferedOutputStream out = null;

	    try {
	        in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

	        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
	        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
	        copy(in, out);
	        out.flush();

	        final byte[] data = dataStream.toByteArray();
	        BitmapFactory.Options options = new BitmapFactory.Options();
	        //options.inSampleSize = 1;

	        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
	    } catch (IOException e) {
	        Log.e(TAG, "Could not load Bitmap from: " + url);
	    } finally {
	        closeStream(in);
	        closeStream(out);
	    }

	    return bitmap;
	}

	 private static void closeStream(Closeable stream) {
	        if (stream != null) {
	            try {
	                stream.close();
	            } catch (IOException e) {
	                android.util.Log.e(TAG, "Could not close stream", e);
	            }
	        }
	    }
	 
	 private static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] b = new byte[IO_BUFFER_SIZE];
        int read;
        while ((read = in.read(b)) != -1) {
            out.write(b, 0, read);
        }
    }
	/***** Get Image Resource from URL (End) *****/
	 @Override
		public boolean onCreateOptionsMenu(Menu menu) {
			// Inflate the menu; this adds items to the action bar if it is present.
			//getMenuInflater().inflate(R.menu.main, menu);
	    	menu.add(0, 0, 0, R.string.refersh); 
	    	return super.onCreateOptionsMenu(menu);
	    	
	    } 
	 @Override
		public void onBackPressed() {
			AlertDialog.Builder qDlg = new AlertDialog.Builder(this);
			qDlg.setTitle(getResources().getString(R.string.quit));
			qDlg.setPositiveButton(getResources().getString(R.string.no),null);
			qDlg.setNegativeButton(getResources().getString(R.string.yes), new OnClickListener() {
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					finish();
				}
			});
			qDlg.show();
		}
}




Tag : Mobile, MySQL, Android







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2014-04-27 16:46:01 By : saranyu00 View : 1100 Reply : 1
 

 

No. 1



โพสกระทู้ ( 0 )
บทความ ( 0 )



สถานะออฟไลน์


รูปภาพประกอบครับ
อันนี้รูปภาพของแอพผมครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-04-27 16:47:25 By : saranyu00
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Android - อยากได้คลิกที่รายการแล้วเก็บยอดวิวการเข้าชมครับ ผมทำเป็นแอพดูวีดีโอตามขั้นตอนของไทยครีแอ็ด
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่