11
AprHow to load internet images in GridView Android example
0 Comments | Posted by nitalpanchal in Android
GridViewConfig.java
package mnp.com;
import java.util.ArrayList;
public class GridViewConfig {
private static ArrayList<String> resim_list=new ArrayList<String>();
public static ArrayList<String> getResim_list() {
return resim_list;
}
public static void setResim_list(ArrayList<String> resim_list) {
GridViewConfig.resim_list = resim_list;
}
public static void addImageUrls(){
resim_list.add(“http://10.0.2.2/homeopathic-7/upload/17.JPG”);
resim_list.add(“http://10.0.2.2/homeopathic-7/upload/16_W.JPG”);
resim_list.add(“http://10.0.2.2/homeopathic-7/upload/17_W.JPG”);
resim_list.add(“http://10.0.2.2/homeopathic-7/upload/17_W.JPG”);
resim_list.add(“http://10.0.2.2/homeopathic-7/upload/42_W.JPG”);
resim_list.add(“http://10.0.2.2/homeopathic-7/upload/43_W.JPG”);
resim_list.add(“http://10.0.2.2/homeopathic-7/upload/472071.JPG”);
resim_list.add(“http://10.0.2.2/homeopathic-7/upload/00BIL471320948F.jpg”);
resim_list.add(“http://10.0.2.2/homeopathic-7/upload/00FORDEXPEDITIONXLTBLACK800J.jpg”);
resim_list.add(“http://10.0.2.2/homeopathic-7/upload/17_W.JPG”);
resim_list.add(“http://10.0.2.2/homeopathic-7/upload/41_W.JPG”);
}
}
ImageAdapter.java
package mnp.com;
import java.io.InputStream;
import java.net.URL;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context context) {
super();
this.context = context;
//Listeye image url si ekliyor
GridViewConfig.addImageUrls();
}
public int getCount() {
// TODO Auto-generated method stub
return GridViewConfig.getResim_list().size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return GridViewConfig.getResim_list().get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView;
if(convertView==null)
{
imageView=new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(100,100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5,5,5,5);
}else{
imageView=(ImageView)convertView;
}
imageView.setImageDrawable(LoadImageFromURL(GridViewConfig.getResim_list().get(position)));
return imageView;
}
private Drawable LoadImageFromURL(String url) {
// TODO Auto-generated method stub
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, “src”);
return d;
}catch (Exception e) {
System.out.println(e);
return null;
}
}
}
main.java
package mnp.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class main extends Activity {
/** Called when the activity is first created. */
private GridView girGridView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
girGridView=(GridView)findViewById(R.id.gridView1_bir);
girGridView.setAdapter(new ImageAdapter(this));
girGridView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),GridViewConfig.getResim_list().get(position), Toast.LENGTH_SHORT).show();
}});
}
}
main.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”horizontal” >
<GridView
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:numColumns=”auto_fit”
android:columnWidth=”90dp”
android:horizontalSpacing=”10dp”
android:verticalSpacing=”10dp”
android:gravity=”center”
android:stretchMode=”columnWidth” android:id=”@+id/gridView1_bir”>
</GridView>
</LinearLayout>
the various control structures in C
Control structures are the statements which controls the overall any program. Control structures are basically of three types –
- Sequence statements
- Iterative statements
- Selection statements
Sequence Statements : All the State in a program except the iterative & statements. They are generally the individual statements which performs the task of input, output, assignment declaration etc.
Iterative Statement are those repeated execution of a particular set of instructions desired number of times. These statements are generally called loops for their execution nature. Loops are basically of two types –
-
- Count Loop
- Event Loop
Count Loop :- Those loops whose no. of times of execution is known prior to the execution of loop are termed as count loops i.e they care countable in nature.
Event Loop :- Those loop whose execution any certain event are called event before the execution.
A loop basically consists of a loop variable which has for the completion of any iterative statement, i.e a loop consists of three main statements:-
Initialization is used to start any loop i.e. it gives some initial value to the loop variable. Which defines that from which value the loop has to get start. Condition is provided to give the final value to the loop variable so that how many times the loop has to get executed. For reaching the loop variable from initial value to the final value there should be some sort of increamentation and that provided by the third component statement of loop and that is incrementation.
If in any loop the incramentation or the final value is not provided then the loop becomes infinite. If the initialization is not done then the garbage value of the loop variable becomes its initial value.
In C language the iterative statements (loops) can be implemented in the three loops and they are
The for loop
Syntax of For Loop –
for (initialization; condition; incrementation)
{ —————– body of loop ————-
}
For Loop will perform its execution until the condition remains satisfied. If the body of the loop consists of more than one statement then these statements are made compound by placing the open and closed curly brackets around the body of the loop. For loop is a count loop. The initialization condition and increamentation may be done in the same statement. For loop will not execute at least once also if the condition is false at the first time itself.
With loops you also have to put commands between curly brackets if there is more than one of them. Here is the solution to the problem that we had with the 24 printf commands:
{
int i;
for (i = 1;i <= 24;i++)
printf("H\n");
return 0;
}
A for loop is made up of 3 parts inside its brackets which are separated by semi-colons. The first part initializes the loop variable. The loop variable controls and counts the number of times a loop runs. In the example the loop variable is called i and is initialized to 1. The second part of the for loop is the condition a loop must meet to keep running. In the example the loop will run while i is less than or equal to 24 or in other words it will run 24 times. The third part is the loop variable incremental. In the example i++ has been used which is the same as saying i = i + 1. This is called incrementing. Each time the loop runs i has 1 added to it. It is also possible to use i– to subtract 1 from a variable in which case it’s called decrementing.
The while loop
Syntax –
Initialization;
While (condition)
{
Body of loop;
Incrementation;
}
In this loop, initialisation, condition and incrementation is done in the three different statements. This loops is count as well as event loop. In case of while loops the body of the loop will consist of more than one statements because each time one statement will be of incrementation. Hence the open and closed curly brackets are required.
{
int i,times;
scanf("%d",&times);
i = 0;
while (i <= times)
{
i++;
printf("%d\n",i);
}
return 0;
}
The do while loop
The third loop statement available in C is do-while statement syntax :-
Initialization;
Do
{
Body of loop;
Increamentation;
}
While (condition);
In this loop the condition is checked at the end, and for this reason this loop will execute at least once whether the condition may be satisfying and unsatisfying.
{
int i,times;
scanf("%d",&times);
i = 0;
do
{
i++;
printf("%d\n",i);
}
while (i <= times);
return 0;
}
Break and continue
You can exit out of a loop at any time using the break statement. This is useful when you want a loop to stop running because a condition has been met other than the loop end condition.
{
int i;
while (i < 10)
{
i++;
if (i == 5)
break;
}
return 0;
}
You can use continue to skip the rest of the current loop and start from the top again while incrementing the loop variable again. The following example will never print “Hello” because of the continue.
{
int i;
while (i < 10)
{
i++;
continue;
printf("Hello\n");
}
return 0;
}
6
JanDiscuss various levels of access protection available for package.
0 Comments | Posted by kananiprabodh in Java
- As you will see, Java provides many levels of protection to allow fine-grained control over the visibility of variables and methods within classes, subclasses, and packages.
- Packages act as containers for classes and other subordinate packages.
- Java addresses four categories of visibility for class members:
• Subclasses in the same package
• Non-subclasses in the same package
• Subclasses in different packages
• Classes that are neither in the same package nor subclasses
- The three access specifiers, private, public, and protected, provide a variety of ways to produce the many levels of access required by these categories.
CLASS MEMBER ACCESS
|
PRIVATE
|
NO MODIFIER |
PROTECTED
|
PUBLIC |
|
|
SAME CLASS
|
YES |
YES |
YES |
YES |
|
SAME PACKAGE SUBCLASS
|
NO |
YES |
YES |
YES |
|
SAME PACKAGE NON-SUBCLASS
|
NO |
YES |
YES |
YES |
|
DIFFERENT PACKAGE SUBCLASS
|
NO |
NO |
YES |
YES |
|
DIFFERENT PACKAGE NON-SUBCLASS |
NO |
NO |
NO |
YES |
1] PUBLIC
- A class has only two possible access levels: default and public.
- When a class is declared as public, it is accessible by any other code.
2] DEFAULT [friendly access]
- If a class has default access, then it can only be accessed by other code within its same package.
- The difference between public access and friendly access is that the public modifier makes fields visible in all classes regardless of their packages, while friendly access modifier makes field’s visibility only in the same package but not in other packages.
3] PRIVATE
- Anything declared private cannot be seen outside of its class.
- Can’t be inherited by the subclasses of other packages.
4] PROTECTED
Protected modifier makes the field visible not only to all classes and subclasses in the same package as well as in other packages.
5] PRIVATE PROTECTED
This modifier makes the fields visible in all sub classes regardless of what package they are in. These fields are not accessible by other classes in the same package.
EXAMPLE
The following example shows all combinations of the access control modifiers.
Following is the source code for the other package, p1.
This is file Protection.java:
package p1;
public class Protection
{ int n = 1;
private int n_pri = 2;
protected int n_pro = 3;
public int n_pub = 4;
public Protection()
{ System.out.println(“base constructor”);
System.out.println(“n = ” + n);
System.out.println(“n_pri = ” + n_pri);
System.out.println(“n_pro = ” + n_pro);
System.out.println(“n_pub = ” + n_pub);
}
}
This is file Derived.java:
package p1;
class Derived extends Protection
{ Derived()
{ System.out.println(“derived constructor”);
System.out.println(“n = ” + n);
// class only
// System.out.println(“n_pri = ” + n_pri);
System.out.println(“n_pro = ” + n_pro);
System.out.println(“n_pub = ” + n_pub);
}
}
This is file SamePackage.java:
package p1;
class SamePackage
{ SamePackage()
{ Protection p = new Protection();
System.out.println(“same package constructor”);
System.out.println(“n = ” + p.n);
// class only
// System.out.println(“n_pri = ” + p.n_pri);
System.out.println(“n_pro = ” + p.n_pro);
System.out.println(“n_pub = ” + p.n_pub);
}
}
Following is the source code for the other package, p2.
This is file Protection2.java:
package p2;
class Protection2 extends p1.Protection
{ Protection2()
{ System.out.println(“derived other package constructor”);
// class or package only
// System.out.println(“n = ” + n);
// class only
// System.out.println(“n_pri = ” + n_pri);
System.out.println(“n_pro = ” + n_pro);
System.out.println(“n_pub = ” + n_pub);
}
}
This is file OtherPackage.java:
package p2;
class OtherPackage
{ OtherPackage()
{ p1.Protection p = new p1.Protection();
System.out.println(“other package constructor”);
// class or package only
// System.out.println(“n = ” + p.n);
// class only
// System.out.println(“n_pri = ” + p.n_pri);
// class, subclass or package only
// System.out.println(“n_pro = ” + p.n_pro);
System.out.println(“n_pub = ” + p.n_pub);
}
}
Here are two test files you can use. The one for package p1 is shown here:
// Demo package p1.
package p1;
// Instantiate the various classes in p1.
public class Demo
{ public static void main(String args[])
{ Protection ob1 = new Protection();
Derived ob2 = new Derived();
SamePackage ob3 = new SamePackage();
}
}
The test file for p2 is shown next:
// Demo package p2.
package p2;
// Instantiate the various classes in p2.
public class Demo
{ public static void main(String args[])
{ Protection2 ob1 = new Protection2();
OtherPackage ob2 = new OtherPackage();
}
}
6
JanWrite a short note on package. Give its significance.
0 Comments | Posted by kananiprabodh in Java
- Package is essentially a group of classes.
- Packages are containers for classes that are used to keep the class name space compartmentalized.
- For example, a package allows you to create a class named List, which you can store in your own package without concern that it will collide with some other class named List stored elsewhere.
- The package is both a naming and a visibility control mechanism.
- You can define classes inside a package that are not accessible by code outside that package.
- You can also define class members that are only exposed to other members of the same package.
Defining package
- To create a package, include a package command as the first statement in a Java source file. –
- Any classes declared within that file will belong to the specified package.
- The package statement defines a name space in which classes are stored. If you will not give the package name then the class names are put into the default package, which has no name.
- This is the general form or syntax of the package statement:
- package packagename;
- Here, packagename is the name of the package.
- For example, the following statement creates a package called MyPackage.
- package MyPackage;
- Java uses file system directories to store packages.
- For example, the .class files for any classes you declare to be part of MyPackage must be stored in a directory called MyPackage.Case is significant, and the directory name must match the package name exactly.
- By default JAVA runtime system uses current working directory as its starting point.
Characteristics of packages:
1] Packages are stored in hierarchical manner and are explicitly imported into new classes
definition.
2] The package is both naming and visibility control mechanism.
3] If a source file doesn’t contain a package statement, its classes and interfaces are placed in a default package.
4] Package statement must appear as the first statement.
5] Two different packages can declare classes have same, by providing this way JAVA provides way for partitioning the class name space into more manageable chunks.
6] We can define classes inside package that are not accessible by code outside that package. We can also define class members that are only exposed to other members of the same package.
By organizing classes into packages following benefits can be achieved.
1] The classes contained in the packages of other programs can be easily reused.
2] In packages, classes can be unique compared with classes in other packages. i.e. two classes in two different packages can have same name.
3] Packages can hide classes that are meant to internal use only from other programs or packages.
4] Packages also provide a way for separating “design” and “coding”.
1st we can design classes and decide their responsibility and then we can implement the java code needed for the methods.
It is possible to change the implementation of any method without affecting the rest of the design.
Hierarchy of packages:
- You can create hierarchy of packages. To do this simply separate each package name from the one above it by use of a period.
Syntax:
package pkg1[.pkg2[.pkg3]];
CLASSPATH variable
- CLASSPATH is an environment variable that determines where the JDK tools such as the JAVA compiler and interpreter search for a .class file.
- It contains an ordered sequence of directories as well as .jar and .zip files.
- It can be set as:
set classpath=.;c:\\project1;c:\\project2
- echo %classpath% displays the current settings of the classpath variable.
Example:
package MyPack;
class Balance
{
String name;
double bal;
Balance(String n, double b)
{
name=n;
bal=b;
}
void show()
{
If(bal<0)
{
System.out.println(“-> “);
System.out.println(name+”:Rs.”+bal);
}
}
}
class AccountBalance
{
public static void main(String args[])
{
Balance current[]=new Balance[5];
Current[0]=new Balance[“liza”,25000.02];
Current[1]=new Balance[“liza”,17000.00];
Current[2]=new Balance[“pushpendu”,12000.00];
for(i=0;i<3;i++)
current[i].show();
}
}
6
JanWrite short note on thread, give the different ways to creating a thread.
0 Comments | Posted by kananiprabodh in Java
- A multithreaded program contains two or more parts that can run concurrently.
- Each part of such a program is called a thread, and each thread defines a separate path of execution.
- The thread is the smallest unit of dispatchable code.
- This means that a single program can perform two or more tasks simultaneously.
- For instance, a text editor can format text at the same time that it is printing, as long as these two actions are being performed by two separate threads.
- Multitasking threads require less overhead than multitasking processes.
- Processes are heavyweight tasks that require their own separate address spaces whereas threads are lightweight. They share the same address space and cooperatively share the same heavyweight process.
- Interprocess communication is expensive and limited whereas Interthread communication is inexpensive.
- Context switching from one process to another is also costly whereas context switching from one thread to the next is low cost.
- Process-based multitasking is not under the control of Java. However, multithreaded multitasking is under control of java.
- Multithreading enables you to write very efficient programs that make maximum use of the CPU, because idle time can be kept to a minimum.
Two types of operating system:
Single threaded
Multithreaded
- In a traditional, single-threaded environment, your program has to wait for each of these tasks to finish before it can proceed to the next one—even though the CPU is sitting idle most of the time.
- Multithreading lets you gain access to this idle time and put it to good use.
![[del.icio.us]](http://www.itshala.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.itshala.com/wp-content/plugins/bookmarkify/digg.png)
![[dzone]](http://www.itshala.com/wp-content/plugins/bookmarkify/dzone.png)
![[Facebook]](http://www.itshala.com/wp-content/plugins/bookmarkify/facebook.png)
![[Google]](http://www.itshala.com/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://www.itshala.com/wp-content/plugins/bookmarkify/linkedin.png)
![[Twitter]](http://www.itshala.com/wp-content/plugins/bookmarkify/twitter.png)
![[Windows Live]](http://www.itshala.com/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://www.itshala.com/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.itshala.com/wp-content/plugins/bookmarkify/email.png)
