Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.hugo'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.csc.colloquium1"
Expand All @@ -20,7 +21,10 @@ android {
}

dependencies {
compile 'com.android.support:recyclerview-v7:23.2.1'
compile 'com.android.support:cardview-v7:23.2.1'
compile 'com.android.support:appcompat-v7:23.2.1'
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'

}
21 changes: 20 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.csc.colloquium1">
xmlns:tools="http://schemas.android.com/tools"
package="com.lpaina.colloquium1">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<provider
android:name=".ReaderContentProvider"
android:authorities="com.lpaina.colloquium1"
android:enabled="true"
android:exported="false"
tools:ignore="ExportedContentProvider" />

<activity android:name=".ChangeActivity"></activity>
</application>

</manifest>
41 changes: 41 additions & 0 deletions app/src/main/java/com/lpaina/colloquium1/Card.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.lpaina.colloquium1;

public class Card implements Cloneable {
public static final String TITLE_NAME = "TITLE";
public static final String VALUE_NAME = "VALUE";
public static final String[] NAMES = new String[]{TITLE_NAME, VALUE_NAME};
private String title;
private String value;

public Card() {

}

public Card(String title, String value) {
this.title = title;
this.value = value;
}

@Override
protected Card clone() throws CloneNotSupportedException {
super.clone();
return new Card(title, value);
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}
70 changes: 70 additions & 0 deletions app/src/main/java/com/lpaina/colloquium1/ChangeActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.lpaina.colloquium1;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class ChangeActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change);
Intent intent = getIntent();
String title = intent.getStringExtra(RVAdapter.CardViewHolder.TITLE);
String value = intent.getStringExtra(RVAdapter.CardViewHolder.VALUE);
final float value_float = Float.parseFloat(value.replace(",", "."));

final EditText editText1 = (EditText) findViewById(R.id.first_value);
final EditText editText2 = (EditText) findViewById(R.id.second_value);

TextView textView1 = (TextView) findViewById(R.id.first_title);
TextView textView2 = (TextView) findViewById(R.id.second_title);

editText1.setText("1");
editText2.setText(value);

textView1.setText("RUB");
textView2.setText(title);

editText1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
float new_value = Float.parseFloat(String.valueOf(s));
editText2.setText(Float.toString(value_float * new_value));
}

@Override
public void afterTextChanged(Editable s) {

}
});

editText2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
float new_value = Float.parseFloat(String.valueOf(s));
editText1.setText(Float.toString(new_value / value_float));
}

@Override
public void afterTextChanged(Editable s) {

}
});
}
}
25 changes: 25 additions & 0 deletions app/src/main/java/com/lpaina/colloquium1/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.lpaina.colloquium1;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public class MainActivity extends Activity {
private static final String PATH_TO_COURSE = "http://www.cbr.ru/scripts/RssCurrency.asp";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

Cursor cursor = getContentResolver().query(ReaderContentProvider.CONTENT_URI,
Card.NAMES, PATH_TO_COURSE, null, null);

RVAdapter adapter = new RVAdapter(cursor);
recyclerView.setAdapter(adapter);
}
}
78 changes: 78 additions & 0 deletions app/src/main/java/com/lpaina/colloquium1/RSSParserTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.lpaina.colloquium1;

import android.os.AsyncTask;
import android.util.Log;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import hugo.weaving.DebugLog;

class RSSParserTask extends AsyncTask<String, Void, List<Card>> {

private static final String TAG = "RSSParserTask";

@Override
@DebugLog
protected List<Card> doInBackground(String... params) {
String query = params[0];
ArrayList<Card> cards = null;
try {
URL url = new URL(query);

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser parser = factory.newPullParser();
parser.setInput(url.openStream(), "Windows-1251");

cards = new ArrayList<>();
boolean started = false;
String info = null;
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
String name = parser.getName().toLowerCase();
switch (name) {
case "item":
started = true;
break;
case "description":
if (started) {
info = parser.nextText();
}
break;
}
} else if (eventType == XmlPullParser.END_TAG && parser.getName().equalsIgnoreCase("item")) {
started = false;
String[] strings = info.split("<br>");
for (String string : strings) {
String[] values = string.split("-");
cards.add(new Card(values[0].trim(), values[1].trim()));
}
}

eventType = parser.next(); //move to next element
}

} catch (XmlPullParserException | IOException e) {
Log.e(TAG, "doInBackground: ", e);
}

Collections.sort(cards, new Comparator<Card>() {
@Override
public int compare(Card lhs, Card rhs) {
return lhs.getTitle().compareTo(rhs.getTitle());
}
});
return cards;
}

}
85 changes: 85 additions & 0 deletions app/src/main/java/com/lpaina/colloquium1/RVAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.lpaina.colloquium1;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.CardViewHolder> {

private Cursor cursor;

public RVAdapter(Cursor cursor) {
this.cursor = cursor;
}

public void updateCursor(Cursor newCursor) {
if (cursor != null) {
cursor.close();
}
cursor = newCursor;
}

@Override
public int getItemCount() {
return cursor.getCount();
}

@Override
public CardViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);
return new CardViewHolder(view);
}

@Override
public void onBindViewHolder(CardViewHolder cardViewHolder, int i) {
if (cursor.moveToPosition(i)) {
cardViewHolder.title = cursor.getString(cursor.getColumnIndex(Card.TITLE_NAME));
cardViewHolder.value = cursor.getString(cursor.getColumnIndex(Card.VALUE_NAME));

cardViewHolder.textViewTitle.setText(cardViewHolder.title);
cardViewHolder.textViewValue.setText(cardViewHolder.value);

}
}

public static class CardViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public static final String TITLE = "title";
public static final String VALUE = "value";


final TextView textViewTitle;
final TextView textViewValue;
final CardView cardView;

final Context context;

int id;
String title;
String value;

CardViewHolder(View itemView) {
super(itemView);
textViewTitle = (TextView) itemView.findViewById(R.id.title);
textViewValue = (TextView) itemView.findViewById(R.id.value);
cardView = (CardView) itemView.findViewById(R.id.card_view);
cardView.setOnClickListener(this);
context = itemView.getContext();
}

@Override
public void onClick(View v) {
Intent intent = new Intent(context, ChangeActivity.class);
intent.putExtra(TITLE, title);
intent.putExtra(VALUE, value);
context.startActivity(intent);
}

}

}
Loading