Разработка учебного пособия по предмету "Web Технологии"



бет37/37
Дата29.03.2022
өлшемі2,23 Mb.
#29107
түріДиплом
1   ...   29   30   31   32   33   34   35   36   37
Байланысты:
Скроботов

ПРИЛОЖЕНИЕ В


Листинг программы
package project.mobile.web_book;

import android.app.Application;

import android.text.TextUtils;

import com.android.volley.Request;

import com.android.volley.RequestQueue;

import com.android.volley.toolbox.ImageLoader;

import com.android.volley.toolbox.Volley;

import com.parse.Parse;

public class AppController extends Application {

public static final String TAG = AppController.class.getSimpleName();

private RequestQueue mRequestQueue;

private ImageLoader mImageLoader;

LruBitmapCache mLruBitmapCache;

private static AppController mInstance;

@Override

public void onCreate() {

super.onCreate();

mInstance = this;

Parse.initialize(this, "6j3Vv5ykQfv5cV4eBd9vYA6N6Cv81SejwIxmOX7m", "MwprRD7qBNJ43gdeS9snMaZFwCuIMfyrHgLPQxMO");

// Specify an Activity to handle all pushes by default.

//PushService.setDefaultPushCallback(this, MainActivity.class);

/*ParsePush.subscribeInBackground("", new SaveCallback() {

Продолжение приложения В.Листинг программы.

@Override

public void done(ParseException e) {

if (e != null) {

Log.e("com.parse.push", "successfully subscribed to the broadcast channel.");} else {

Log.e("com.parse.push", "failed to subscribe for push", e);}}});

ParseInstallation.getCurrentInstallation().saveInBackground();*/}

public static synchronized AppController getInstance() {return mInstance;}

public RequestQueue getRequestQueue() {

if (mRequestQueue == null) {

mRequestQueue = Volley.newRequestQueue(getApplicationContext());

}return mRequestQueue;}


public ImageLoader getImageLoader() {

getRequestQueue();

if (mImageLoader == null) {

getLruBitmapCache();

mImageLoader = new ImageLoader(this.mRequestQueue, mLruBitmapCache);}
return this.mImageLoader;

public LruBitmapCache getLruBitmapCache() {

if (mLruBitmapCache == null)

mLruBitmapCache = new LruBitmapCache();

return this.mLruBitmapCache;

public void addToRequestQueue(Request req, String tag) {

req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);

getRequestQueue().add(req);

public void addToRequestQueue(Request req) {

req.setTag(TAG);

getRequestQueue().add(req);}

public void cancelPendingRequests(Object tag) {

if (mRequestQueue != null) {

mRequestQueue.cancelAll(tag);}}

package project.mobile.web_book;
import android.content.Context;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context){

this._context = context;}

public boolean isConnectingToInternet(){

ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);

if (connectivity != null)

NetworkInfo[] info = connectivity.getAllNetworkInfo();

if (info != null)

for (int i = 0; i < info.length; i++)

Продолжение приложения В.Листинг программы

if (info[i].getState() == NetworkInfo.State.CONNECTED){

return true;}}

return false;}}

package project.mobile.web_book;


import android.content.Context;

import android.graphics.Typeface;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseExpandableListAdapter;

import android.widget.TextView;
import java.util.HashMap;

import java.util.List;


public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;

private List _listDataHeader; // header titles

// child data in format of header title, child title

private HashMap> _listDataChild;

public ExpandableListAdapter(Context context, List listDataHeader,

HashMap> listChildData) {

this._context = context;

Продолжение приложения В.Листинг программы

this._listDataHeader = listDataHeader;

this._listDataChild = listChildData;

}

@Override



public Object getChild(int groupPosition, int childPosititon) {

return this._listDataChild.get(this._listDataHeader.get(groupPosition))

.get(childPosititon);}

@Override

public long getChildId(int groupPosition, int childPosition) {

return childPosition;

}
@Override

public View getChildView(int groupPosition, final int childPosition,

boolean isLastChild, View convertView, ViewGroup parent) {

final String childText = (String) getChild(groupPosition, childPosition);

if (convertView == null) {

LayoutInflater infalInflater = (LayoutInflater) this._context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = infalInflater.inflate(R.layout.list_item, null);}

TextView txtListChild = (TextView) convertView

.findViewById(R.id.lblListItem);

txtListChild.setText(childText);

return convertView;}

@Override

public int getChildrenCount(int groupPosition) {

return this._listDataChild.get(this._listDataHeader.get(groupPosition))

.size();

}
@Override

public Object getGroup(int groupPosition) {

return this._listDataHeader.get(groupPosition);

}
@Override

public int getGroupCount() {

return this._listDataHeader.size();

}

@Override



public long getGroupId(int groupPosition) {

return groupPosition;}

@Override

public View getGroupView(int groupPosition, boolean isExpanded,

View convertView, ViewGroup parent) {

String headerTitle = (String) getGroup(groupPosition);

if (convertView == null) {

LayoutInflater infalInflater = (LayoutInflater) this._context

Продолжение приложения В.Листинг программы

getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = infalInflater.inflate(R.layout.list_group, null);}

TextView lblListHeader = (TextView) convertView

.findViewById(R.id.lblListHeader);

lblListHeader.setTypeface(null, Typeface.BOLD);

lblListHeader.setText(headerTitle);

return convertView;}

@Override

public boolean hasStableIds() {

return false;}

@Override

public boolean isChildSelectable(int groupPosition, int childPosition) {

return true;}}

package project.mobile.web_book;
import android.app.Activity;

import android.app.Dialog;

import android.app.ProgressDialog;

import android.content.ActivityNotFoundException;

import android.content.Intent;

import android.net.Uri;

import android.os.AsyncTask;

import android.os.Environment;

import android.support.v4.app.Fragment;

import android.content.Context;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

import android.os.Bundle;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

Продолжение приложения В.Листинг программы
import android.widget.BaseAdapter;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.RelativeLayout;

import android.widget.TextView;

import android.widget.Toast;


import com.parse.FindCallback;

import com.parse.Parse;

import com.parse.ParseAnalytics;

import com.parse.ParseFile;

import com.parse.ParseObject;

import com.parse.ParseQuery;

import com.parse.ParseException;
import com.tjerkw.slideexpandable.library.ActionSlideExpandableListView;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.List;


public class HomeFragment extends Fragment {

TextView tv;

ActionSlideExpandableListView expandableList;

String topics[];

int groupPos;

public HomeFragment(){}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)

{

View rootView = inflater.inflate(R.layout.fragment_home, container, false);



Parse.initialize(getActivity(), "9EIAp5rRdggPxy1sQgCciEAiHruBIgavy21O3JEf", "W8wtRJLGtxNjHJzTOIlhg5JRDgVGZIDGPz2DgTV5");

ParseAnalytics.trackAppOpened(getActivity().getIntent());


File createFile = new File("/sdcard/WEB e-book/Lectures");

createFile.mkdirs();


File createFile2 = new File("/sdcard/WEB e-book/Labs");

createFile2.mkdirs();


topics = getResources().getStringArray(R.array.topics);

expandableList = (ActionSlideExpandableListView)rootView.findViewById(R.id.list123);


//File createFile = new File("/sdcard/WEB e-book/Lectures");

//createFile.mkdirs();


expandableList.setAdapter(new ExpandableListAdapter(getActivity(),topics));
return rootView;

}

public class ExpandableListAdapter extends BaseAdapter{



private String[] topics;

private Activity activity;


public ExpandableListAdapter(Activity activity, String[] topics){

super();

this.activity = activity;

this.topics = topics;

}

@Override



public int getCount() {

return topics.length;

}

public String getItem(int position) {



return topics[position];

}

@Override



public long getItemId(int position) {

return 0;

}
public class ViewHolder

{

public TextView txtViewNames;



public TextView txtViewTopic;

public RelativeLayout rl;

public Button downDocx, downPpt;

public File file;

}

@Override



public View getView(final int position, View convertView, ViewGroup parent1) {

final ViewHolder view;

LayoutInflater inflator = activity.getLayoutInflater();

if(convertView==null)

{

view = new ViewHolder();



convertView = inflator.inflate(R.layout.expandable_list_item, null);

view.txtViewNames = (TextView) convertView.findViewById(R.id.text);

view.txtViewTopic = (TextView) convertView.findViewById(R.id.textTopic);

view.rl = (RelativeLayout) convertView.findViewById(R.id.realLayout);

view.downDocx = (Button) convertView.findViewById(R.id.button);

view.downPpt = (Button) convertView.findViewById(R.id.button2);


convertView.setTag(view);

}

else



{

view = (ViewHolder) convertView.getTag();

}
view.txtViewNames.setText("Лекция "+(position+1));

view.txtViewTopic.setText(topics[position]);

view.downDocx.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View view) {

if(isOnline()){

new Download((position+1),"docx").execute();

}
}


});

view.downPpt.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View view) {

if(isOnline()){

new Download((position+1),"pptx").execute();

}

}
});



if(position%2==1){

view.rl.setBackgroundColor(activity.getResources().getColor(R.color.dark_sand));

}else {

view.rl.setBackgroundColor(activity.getResources().getColor(R.color.light_sand));

}

return convertView;



}

private class Download extends AsyncTask> {

ProgressDialog WaitingDialog;

int pos;


File file;

String fileName;

String fileType;

public Download(int pos,String word){

this.pos = pos;

this.fileType = word;//"lectures_docx"

}
@Override

protected void onPostExecute(List result) {

WaitingDialog.dismiss();

// Toast.makeText(getActivity(), "Success!", Toast.LENGTH_SHORT).show();

}
@Override

protected void onPreExecute() {


Log.i("ref", "true");

WaitingDialog = ProgressDialog.show(getActivity(), "Please wait", "Downloading file...", true);


}
@Override

protected List doInBackground(String... params) {

ParseQuery query = new ParseQuery("store");
query.whereEqualTo("pos", "" + pos);

query.findInBackground(new FindCallback


() {

@Override

public void done(List
scoreList, ParseException e) {

if (e == null) {


for (int i = 0; i < scoreList.size(); i++) {
ParseFile txtFile = scoreList.get(i).getParseFile(fileType);
fileName = "Lecture " + pos +"."+fileType;

// fileName = fileName.substring(0, fileName.indexOf("."));


try {

byte[] txtData = txtFile.getData();


file = new File("/sdcard/WEB e-book/Lectures/" + fileName);

FileOutputStream fosFile;

try {

fosFile = new FileOutputStream(file);



fosFile.write(txtData);
} catch (FileNotFoundException e1) {

e1.printStackTrace();

} catch (IOException ee) {

ee.printStackTrace();

}

} catch (ParseException e1) {



e1.printStackTrace();
}

openFile(fileName);

}

}

}


});

return null;

}

}

public void openFile(String f){



final String fileName = f;

final Dialog openDoc = new Dialog(activity);

Button openDocButton,cancelDocButton;

ImageView image;


openDoc.setContentView(R.layout.single_doc_dialog);

openDoc.setTitle(getResources().getString(R.string.download) + " " + fileName);

openDoc.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
openDocButton = (Button)openDoc.getWindow().findViewById(R.id.buttonOpen);

cancelDocButton = (Button)openDoc.getWindow().findViewById(R.id.buttonCancel);

image = (ImageView)openDoc.getWindow().findViewById(R.id.imageView4);
if(fileName.charAt(fileName.length()-2)=='c'){ //docx pptx

image.setBackgroundDrawable(getResources().getDrawable(R.drawable.word_icon));

}else{

image.setBackgroundDrawable(getResources().getDrawable(R.drawable.powerpoint_icon));



}

openDoc.show();

openDocButton.setOnClickListener(new OnClickListener() {
@Override

Продолжение приложения В.Листинг программы


public void onClick(View v) {

File file = new File(Environment.getExternalStorageDirectory(), "WEB e-book/Lectures/"+fileName);

if(file.exists()){
Uri path = Uri.fromFile(file);

Intent pdfIntent = new Intent();


pdfIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

pdfIntent.setAction(Intent.ACTION_VIEW);

pdfIntent.setDataAndType(path, "application/msword");

pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

try{

startActivity(pdfIntent);



}

catch(ActivityNotFoundException e)

{

Toast.makeText(getActivity(), "Do not found", Toast.LENGTH_SHORT).show();



}

}

}



});

cancelDocButton.setOnClickListener(new OnClickListener() {


@Override

public void onClick(View v) {

openDoc.dismiss();

}

});



}

public boolean isOnline() {

ConnectivityManager connectivity = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);

if (connectivity != null)

{

NetworkInfo[] info = connectivity.getAllNetworkInfo();



if (info != null)

for (int i = 0; i < info.length; i++)

if (info[i].getState() == NetworkInfo.State.CONNECTED)

{
// Toast.makeText(getActivity(), "You have internet access!", Toast.LENGTH_SHORT).show();

return true;

}
}
Toast.makeText(getActivity(), "You don't have internet access!", Toast.LENGTH_SHORT).show();

return false;
}

}

}


package project.mobile.web_book;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.content.Intent;

import android.content.res.TypedArray;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.BaseAdapter;

import android.widget.Button;

import android.widget.GridView;

import android.widget.ImageView;

import android.widget.ListView;

import android.widget.TextView;
import java.util.ArrayList;
public class LabList extends Fragment {
private View parentView;

private ListView listView;

private GridView gridView;

Button listV, gridV;

MyAdapterHandar adapterListView;

MyAdapterHandar adapterGridView;

ArrayList arrayList;

TextView title;

String person;

String storeKh[],storeKhN[],storeNakylSozder[],storeLink[];

TypedArray storeKhImages;

@SuppressLint("ValidFragment")

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

parentView = inflater.inflate(R.layout.calendar, container, false);

listView = (ListView) parentView.findViewById(R.id.listView);

gridView = (GridView) parentView.findViewById(R.id.gridView);

Продолжение приложения В.Листинг программы


listV = (Button) parentView.findViewById(R.id.buttonList);

gridV = (Button) parentView.findViewById(R.id.buttonGrid);

title = (TextView) parentView.findViewById(R.id.editText);

arrayList = new ArrayList();


storeKh = getActivity().getResources().getStringArray(R.array.labtar);

storeKhN = getActivity().getResources().getStringArray(R.array.labs);

storeKhImages = getActivity().getResources().obtainTypedArray(R.array.lab_images);

storeNakylSozder = getActivity().getResources().getStringArray(R.array.khandar_nakyl_sozder);

storeLink = getActivity().getResources().getStringArray(R.array.khandar_links);
title.setText(getResources().getString(R.string.lab_title));
for(int i =0; i < storeKhN.length; i++){

arrayList.add(new Lab(storeKhN[i], storeKh[i], storeKhImages.getResourceId(i, -1), storeNakylSozder[i],storeLink[i]));

}

adapterListView = new MyAdapterHandar(getActivity(), arrayList, R.layout.row_item_khan);



adapterGridView = new MyAdapterHandar(getActivity(), arrayList, R.layout.grid_item_khan);
gridView.setVisibility(View.INVISIBLE);
listView.setAdapter(adapterListView);

gridView.setAdapter(adapterGridView);


listV.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

gridView.setVisibility(View.INVISIBLE);

listView.setVisibility(View.VISIBLE);

}

});



gridV.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {
gridView.setVisibility(View.VISIBLE);

listView.setVisibility(View.INVISIBLE);

}

});


gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView parent, View view, int position, long id) {

Intent intent = new Intent(getActivity(), LabActivity.class);

intent.putExtra("position", position);

intent.putExtra("name", arrayList.get(position).getName());

intent.putExtra("bio", arrayList.get(position).getBio());

intent.putExtra("nakyl", arrayList.get(position).getNakyl());

intent.putExtra("links", arrayList.get(position).getLinks());

intent.putExtra("image", arrayList.get(position).getImageUrl());

startActivity(intent);

}

});


listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView parent, View view, int position, long id) {

Intent intent = new Intent(getActivity(), LabActivity.class);

intent.putExtra("position", position);

intent.putExtra("name", arrayList.get(position).getName());

intent.putExtra("bio", arrayList.get(position).getBio());

intent.putExtra("nakyl", arrayList.get(position).getNakyl());

intent.putExtra("links", arrayList.get(position).getLinks());

intent.putExtra("image", arrayList.get(position).getImageUrl());

startActivity(intent);

}

});


return parentView;

}

private class MyAdapterHandar extends BaseAdapter {



Activity activity;

ArrayList khan;

LayoutInflater inflater;

int viewLayout;

Продолжение приложения В.Листинг программы
public MyAdapterHandar(Activity activity, ArrayList khan, int viewLayout) {

this.activity = activity;

this.khan = khan;

this.viewLayout = viewLayout;

inflater = LayoutInflater.from(activity);

}
@Override

public int getCount() {

return khan.size();

}
@Override

public Object getItem(int position) {

return khan.get(position);

}
@Override

public long getItemId(int position) {

return 0;

}
@Override

public View getView(int position, View convertView, ViewGroup parent) {


ViewHolder view;

view = new ViewHolder();

convertView = inflater.inflate(viewLayout, null);

view.name = (TextView) convertView.findViewById(R.id.textViewName);

view.bio = (TextView) convertView.findViewById(R.id.textViewBio);

view.imageView = (ImageView) convertView.findViewById(R.id.imageViewKhan);

convertView.setTag(view);
view.name.setText(khan.get(position).getName());

view.bio.setText(khan.get(position).getBio());

view.imageView.setBackgroundDrawable(getResources().getDrawable(khan.get(position).getImageUrl()));
return convertView;

}
public class ViewHolder {

public TextView name, bio;

public ImageView imageView;

}

}

}



package project.mobile.web_book;
import android.content.Intent;

import android.os.Bundle;

import android.os.CountDownTimer;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.TextView;

import java.util.ArrayList;

import java.util.Collections;


public class TestFragment extends Fragment implements View.OnClickListener {
private View parentView;

Button bt1, bt2, bt3, bt4, bt5;

int questionVars[] = {R.array.q1, R.array.q2, R.array.q3, R.array.q4, R.array.q5, R.array.q6, R.array.q7, R.array.q8, R.array.q9, R.array.q10,

R.array.q11, R.array.q12, R.array.q13, R.array.q14, R.array.q15, R.array.q16, R.array.q17, R.array.q18, R.array.q19, R.array.q20};

String questions[], answers[];
ArrayList variantsStore = new ArrayList();
String correctAns;

int count = 0, correctCount = 0;

TextView upai, textViewQuestion;

Продолжение приложения В.Листинг программы


String id;
@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

parentView = inflater.inflate(R.layout.test_fragment, container, false);
upai = (TextView) parentView.findViewById(R.id.textUpai);

textViewQuestion = (TextView) parentView.findViewById(R.id.textViewQuestion);

bt1 = (Button) parentView.findViewById(R.id.bt1);

bt2 = (Button) parentView.findViewById(R.id.bt2);

bt3 = (Button) parentView.findViewById(R.id.bt3);

bt4 = (Button) parentView.findViewById(R.id.bt4);

bt5 = (Button) parentView.findViewById(R.id.bt5);
bt1.setOnClickListener(this);

bt2.setOnClickListener(this);

bt3.setOnClickListener(this);

bt4.setOnClickListener(this);

bt5.setOnClickListener(this);
questions = getResources().getStringArray(R.array.questions);

answers = getResources().getStringArray(R.array.answers);


changeQuestion();

return parentView;

}
public void changeQuestion() {

variantsStore.clear();

String variants [] = getResources().getStringArray(questionVars[count]);

for(String w: variants) variantsStore.add(w);

textViewQuestion.setText(questions[count]);
Collections.shuffle(variantsStore);
bt1.setText(variantsStore.get(0));

bt2.setText(variantsStore.get(1));

bt3.setText(variantsStore.get(2));

bt4.setText(variantsStore.get(3));

bt5.setText(variantsStore.get(4));

Продолжение приложения В.Листинг программы


bt1.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design));

bt2.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design));

bt3.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design));

bt4.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design));

bt5.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design));

}
public void checkCorrect(Button bt) {

correctAns = answers[count];

if (bt.getText().toString().equals(correctAns)) {

bt.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design_correct));

correctCount++;

upai.setText("" + correctCount);
} else {

bt.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design_not_correct));


if (bt1.getText().toString().equals(correctAns)) {

bt1.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design_correct));

} else if (bt2.getText().toString().equals(correctAns)) {

bt2.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design_correct));

} else if (bt3.getText().toString().equals(correctAns)) {

bt3.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design_correct));

} else if (bt4.getText().toString().equals(correctAns)) {

bt4.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design_correct));

} else if (bt5.getText().toString().equals(correctAns)) {
bt5.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design_correct));

}

// dyris emes jeri



}

}

public void timer() {



new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {

}
public void onFinish() {

changeQuestion();

}

}.start();



}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.bt1:

checkCorrect(bt1);

break;

case R.id.bt2:



checkCorrect(bt2);

break;


case R.id.bt3:

checkCorrect(bt3);

break;

case R.id.bt4:



checkCorrect(bt4);

break;


case R.id.bt5:

checkCorrect(bt5);

break;

}

count++;



timer();

}

}


package project.mobile.web_book;
import android.os.Bundle;

import android.os.CountDownTimer;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.TextView;

import java.util.ArrayList;

import java.util.Collections;


public class TestFragment2 extends Fragment implements View.OnClickListener {
private View parentView;

Button bt1, bt2, bt3, bt4, bt5;

int tests[] = {R.array.q1, R.array.q2, R.array.q3, R.array.q4, R.array.q5, R.array.q6, R.array.q7, R.array.q8, R.array.q9, R.array.q10,

R.array.q11, R.array.q12, R.array.q13, R.array.q14, R.array.q15, R.array.q16, R.array.q17, R.array.q18, R.array.q19, R.array.q20};

String questions[];
ArrayList variantsStore = new ArrayList();

ArrayList testStore = new ArrayList();


String correctAns;

String question;

int count = 0, correctCount = 0;

TextView upai, textViewQuestion;

String id;
@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

parentView = inflater.inflate(R.layout.test_fragment, container, false);
upai = (TextView) parentView.findViewById(R.id.textUpai);

textViewQuestion = (TextView) parentView.findViewById(R.id.textViewQuestion);

bt1 = (Button) parentView.findViewById(R.id.bt1);

bt2 = (Button) parentView.findViewById(R.id.bt2);

bt3 = (Button) parentView.findViewById(R.id.bt3);

bt4 = (Button) parentView.findViewById(R.id.bt4);

bt5 = (Button) parentView.findViewById(R.id.bt5);
bt1.setOnClickListener(this);

bt2.setOnClickListener(this);

bt3.setOnClickListener(this);

bt4.setOnClickListener(this);

bt5.setOnClickListener(this);
for(int i = 0; i < tests.length; i++){

questions = getResources().getStringArray(tests[i]);

Question q = new Question(questions[0],questions[1],questions[2],questions[3],questions[4],questions[5]);

testStore.add(q);

}
Collections.shuffle(testStore);

Collections.shuffle(testStore);


changeQuestion();

return parentView;

}
public void changeQuestion() {
Question current = testStore.get(count);

question = current.getQ();

correctAns = current.getV1();
variantsStore.clear();

variantsStore.add(current.getV1());

variantsStore.add(current.getV2());

variantsStore.add(current.getV3());

variantsStore.add(current.getV4());

variantsStore.add(current.getV5());


textViewQuestion.setText(question);
Collections.shuffle(variantsStore);

Collections.shuffle(variantsStore);


bt1.setText(variantsStore.get(0));

bt2.setText(variantsStore.get(1));

bt3.setText(variantsStore.get(2));

bt4.setText(variantsStore.get(3));

bt5.setText(variantsStore.get(4));
bt1.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design));

bt2.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design));

bt3.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design));

bt4.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design));

bt5.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design));

}
public void checkCorrect(Button bt) {

if (bt.getText().toString().equals(correctAns)) {

bt.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design_correct));

correctCount++;

upai.setText("" + correctCount);


} else {

bt.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design_not_correct));


if (bt1.getText().toString().equals(correctAns)) {

bt1.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design_correct));

} else if (bt2.getText().toString().equals(correctAns)) {

bt2.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design_correct));

} else if (bt3.getText().toString().equals(correctAns)) {

bt3.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design_correct));


} else if (bt4.getText().toString().equals(correctAns)) {

bt4.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design_correct));

} else if (bt5.getText().toString().equals(correctAns)) {

bt5.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_design_correct));

}

}

}



public void timer() {

new CountDownTimer(2000, 1000) {


public void onTick(long millisUntilFinished) {

}
public void onFinish() {

changeQuestion();

}

}.start();



}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.bt1:

checkCorrect(bt1);

break;

case R.id.bt2:



checkCorrect(bt2);

break;


case R.id.bt3:

checkCorrect(bt3);

break;

case R.id.bt4:



checkCorrect(bt4);

break;


case R.id.bt5:

checkCorrect(bt5);



break;

}

count++;



timer();}}




Достарыңызбен бөлісу:
1   ...   29   30   31   32   33   34   35   36   37




©emirsaba.org 2024
әкімшілігінің қараңыз

    Басты бет