Open rupalchavda opened 7 years ago
package app.num.http;
import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList; import java.util.List;
/**
Created by Rupal on 24-06-2017.
*/
public class HttpAdpter extends RecyclerView.Adapter
public HttpAdpter(Context context, List
} @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item, parent, false);
return new MyViewHolder(itemView);
}
@Override public void onBindViewHolder(MyViewHolder holder, int position) { // holder.iv.setImageResource(Integer.parseInt(models.get(position).getImage()+"")); Picasso.with(context).load("http://www.move2soft.com/move2soft/image/"+models.get(position).getImage()).into(holder.iv);
}
@Override public int getItemCount() { return models.size(); }
public class MyViewHolder extends RecyclerView.ViewHolder { public ImageView iv;
public MyViewHolder(View view) {
super(view);
iv = (ImageView) view.findViewById(R.id.iv);
}
} }
json static values:
package com.myapp.ms.demonavigation;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log;
import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;
import java.util.ArrayList; import java.util.List;
MainActivity::::
public class JsonParserActivity extends AppCompatActivity {
String json;
List
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json_parser);
makeJsonString();
jsnParser();
setList();
}
private void setList() {
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new weatherAdapter(this,list);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
}
public void makeJsonString() {
try {
JSONObject jsonObject = new JSONObject();
JSONObject sysObject = new JSONObject();
sysObject.put("country", "india");
sysObject.put("sunrise", 10);
sysObject.put("sunset", 12);
jsonObject.put("sys", sysObject);
JSONArray weatherArray = new JSONArray();
JSONObject weatherObject = new JSONObject();
weatherObject.put("id", 120);
weatherObject.put("main", "smoke");
weatherObject.put("description", "smoke");
weatherObject.put("icon", 30);
weatherArray.put(weatherObject);
JSONObject weatherObject2 = new JSONObject();
weatherObject2.put("id", 125);
weatherObject2.put("main", "drink");
weatherObject2.put("description", "drink");
weatherObject2.put("icon", 40);
weatherArray.put(weatherObject2);
jsonObject.put("weather", weatherArray);
JSONObject mainObject = new JSONObject();
mainObject.put("temp", 20);
mainObject.put("pressure", 10);
jsonObject.put("main", mainObject);
json = jsonObject.toString();
Log.e("jso string ", json);
} catch (JSONException e) {
Log.e("JSONException", e.getMessage());
}
}
public void jsnParser() {
try {
JSONObject jsonObject = new JSONObject(json);
JSONObject sys = jsonObject.getJSONObject("sys");
String country = sys.getString("country");
int sunrise = sys.getInt("sunrise");
int sunset = sys.getInt("sunset");
Log.e("country value", country);
Log.e("sunrise value", sunrise + "");
Log.e("sunset value", sunset + "");
JSONArray weather = jsonObject.getJSONArray("weather");
for (int i = 0; i < weather.length(); i++) {
JSONObject jsonObject1 = weather.getJSONObject(i);
com.myapp.ms.demonavigation.weather weathermodel = new weather();
weathermodel.setId(jsonObject1.getInt("id"));
weathermodel.setDescription(jsonObject1.getString("description"));
weathermodel.setMain(jsonObject1.getString("main"));
weathermodel.setIcon(jsonObject1.getInt("icon"));
list.add(weathermodel);
}
JSONObject main = jsonObject.getJSONObject("main");
int temp = main.getInt("temp");
int pressure = main.getInt("pressure");
Log.e("temp value", temp + "");
Log.e("pressure value", pressure + "");
} catch (JSONException e) {
Log.e("JSONException", e.getMessage());
}
}
}
Adpter:::
public class weatherAdapter extends RecyclerView.Adapter
public weatherAdapter(Context context, List<weather> list) {
this.context = context;
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.id.setText(list.get(position).getId()+"");
holder.main.setText(list.get(position).getMain());
holder.description.setText(list.get(position).getDescription());
holder.icon.setText(list.get(position).getIcon()+"");
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView id, main, icon, description;
public MyViewHolder(View view) {
super(view);
id = (TextView) view.findViewById(R.id.id);
main = (TextView) view.findViewById(R.id.main);
icon = (TextView) view.findViewById(R.id.icon);
description = (TextView) view.findViewById(R.id.description);
}
}
}
MODEL::: public class weather { private int id; private String main; private String description; private int icon;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMain() {
return main;
}
public void setMain(String main) {
this.main = main;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
}
Daynamic Value::???:::
MainActivity::::::::::
public class StudentJsonParser extends AppCompatActivity {
String json;
List<Student> list = new ArrayList<>();
EditText et1,et2,et3;
Button bt;
TextView tv;
private RecyclerView recyclerView;
private StudentAdpter mAdapter;
JSONObject jsonObject=new JSONObject();
JSONArray StudentArray = new JSONArray();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_json_parser);
et1= (EditText) findViewById(R.id.et1);
et2= (EditText) findViewById(R.id.et2);
et3= (EditText) findViewById(R.id.et3);
bt= (Button) findViewById(R.id.bt);
tv= (TextView) findViewById(R.id.tv);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MakeJosnString();
jsnParser();
mAdapter.addList(list);
tv.setText(StudentArray.toString());
}
});
setList();
}
private void setList() {
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new StudentAdpter(this,list);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
}
public void MakeJosnString(){
JSONObject StudentObject = new JSONObject();
try {
StudentObject.put("Name", et1.getText().toString());
StudentObject.put("EnrolmentNo", et2.getText().toString());
StudentObject.put("MobileNo", et3.getText().toString());
StudentArray.put(StudentObject);
jsonObject.put("Student", StudentArray);
} catch (Exception e) {
Log.e("JSONException", e.getMessage());
}
}
public void jsnParser() {
try {
JSONArray student=new JSONArray(StudentArray.toString());
for (int i = 0; i < student.length(); i++) {
JSONObject jsonObject1 = student.getJSONObject(i);
com.myapp.ms.demonavigation.Student student1=new Student();
student1.setName(jsonObject1.getString("Name"));
student1.setEnrolmentNo(jsonObject1.getInt("EnrolmentNo"));
student1.setMobileNo(jsonObject1.getInt("MobileNo"));
list.add(student1);
}
} catch (JSONException e) {
Log.e("JSONException", e.getMessage());
}
}
}
StuAdpter:::
public class StudentAdpter extends RecyclerView.Adapter
public StudentAdpter(Context context, List<Student> list) {
this.context = context;
this.list = list;
}
public void addList( List<Student> list) {
this.list=list;
notifyDataSetChanged();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.student, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tvname.setText(list.get(position).getName()+"");
holder.tvenrolmentno.setText(list.get(position).getEnrolmentNo()+"");
holder.tvmobileno.setText(list.get(position).getMobileNo() +"");
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tvname,tvenrolmentno,tvmobileno;
public MyViewHolder(View itemView) {
super(itemView);
tvname = (TextView) itemView.findViewById(R.id.tvname);
tvenrolmentno = (TextView) itemView.findViewById(R.id.tvenrolmentno);
tvmobileno = (TextView) itemView.findViewById(R.id.tvmobileno);
}
}
} MODEL:::::
public class Student { private String name; private int EnrolmentNo; private int MobileNo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getEnrolmentNo() {
return EnrolmentNo;
}
public void setEnrolmentNo(int enrolmentNo) {
EnrolmentNo = enrolmentNo;
}
public int getMobileNo() {
return MobileNo;
}
public void setMobileNo(int mobileNo) {
MobileNo = mobileNo;
}
}
Camera:::
package com.example.rupal.picasso;
import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Handler; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button;
public class Camera extends AppCompatActivity { Button bt_camera, bt_gallery; int CAMERA_REQUEST = 101; int RESULT_LOAD_IMAGE = 102; public static Bitmap photo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
bt_camera = (Button) findViewById(R.id.bt_camera);
bt_gallery = (Button) findViewById(R.id.bt_gallery);
bt_camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
bt_gallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK ) {
photo = (Bitmap) data.getExtras().get("data");
Log.e("bitmap is ", photo + "image name");
Intent i = new Intent(Camera.this, ResultActivity.class);
startActivity(i);
}
if (requestCode == RESULT_LOAD_IMAGE && resultCode == Activity.RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
Log.e("picturePath is ", picturePath + "image");
cursor.close();
photo =BitmapFactory.decodeFile(picturePath) ;
Log.e("bitmap is ", photo + "image");
new Handler().post(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(Camera.this, ResultActivity.class);
startActivity(intent);
}
});
}
}
}
other
public class ResultActivity extends AppCompatActivity { ImageView iv; private Bundle bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
iv= (ImageView) findViewById(R.id.iv);
// bundle=getIntent().getExtras(); // // if(bundle!=null) { // Bitmap photo = (Bitmap) getIntent().getParcelableExtra("BitmapImage"); // Log.e("bitmap is ", photo + "image"); // // }
iv.setImageBitmap(Camera.photo);
}
}
TAB DEMO <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
android:background="@color/blue"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
public class MainActivity extends AppCompatActivity {
ViewPager viewpaser;
TabLayout tabs;
private int[] tabIcons = {
R.drawable.ic_create_new_folder_black_24dp,
R.drawable.ic_sms,
R.drawable.ic_contact_mail_black_24dp,
R.drawable.ic_settings_black_24dp,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpaser = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewpaser);
tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(viewpaser);
setupTabIcons();
}
private void setupTabIcons() {
tabs.getTabAt(0).setIcon(tabIcons[0]);
tabs.getTabAt(1).setIcon(tabIcons[1]);
tabs.getTabAt(2).setIcon(tabIcons[2]);
tabs.getTabAt(3).setIcon(tabIcons[3]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new CreateScheduler(), "Create ModelScheduler");
adapter.addFragment(new Scheduler(), "Sheduler");
adapter.addFragment(new SentSma(), "Sheduled");
adapter.addFragment(new Setting(), "setting");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
FRAGMENT
public class Setting extends Fragment {
public Setting() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_four, container, false); return view }
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Fragment.Setting">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
package app.num.http;
import android.app.Dialog; import android.app.ProgressDialog; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.widget.Toast;
import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.UUID;
import javax.net.ssl.HttpsURLConnection;
public class MainActivity extends AppCompatActivity { private ProgressDialog progress; String json; List models = new ArrayList<>();
private RecyclerView recyclerView;
private HttpAdpter mAdapter;
private List list =new ArrayList<>();
// models.add(new HttpModel(R.drawable.amit)); // models.add(new HttpModel(R.drawable.s)); }
}