tab1.java | |
File Size: | 7 kb |
File Type: | java |
package c.test2;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/*********************************************************************************************
* Tab1 Class/Activity
* This activity connects to the users file on the compsci02 server and pulls that users
friends and places them in an ArrayList. Then it is shown in the UI through a list_item
placed in a listView object.
* executes asyncTask ConnectTask
*********************************************************************************************/
public class Tab1 extends Activity {
private ArrayList<String> data = new ArrayList<String>();
private String me; //get from login pages
public String[] friends = new String[100];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab1);
Bundle extras = getIntent().getExtras();
me = extras.getString("user");
new ConnectTask().execute(friends);
ListView lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(new MyListAdapter(this, R.layout.list_item, data));
}
/******************************************************************************************
* MyListAdapter
* This class creates an adapter for the listView object as well as the View for the list
in the UI. The adapters role is to connect the data of the list to the actual View
of the listView object in the UI.
* MyListAdapter and getView functions
*****************************************************************************************/
private class MyListAdapter extends ArrayAdapter<String>{
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
/**************************************************************
* getView
* This function creates a specific list_item for the listView
for each item in the ArrayList. it sets the thumbnail,
title, and button for each item and places them in the
listView object in the UI.
* Calls button's onClickListener
**************************************************************/
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.list_item_thumbnail);
viewHolder.title = (TextView) convertView.findViewById(R.id.list_item_text);
viewHolder.title.setText(getItem(position));
viewHolder.button = (Button) convertView.findViewById(R.id.btn_Map);
/**************************************************************
* button OnClickListener
* When map button is clicked for any one of the list_items,
onClickListener starts the MapsActivity for that specific
user.
**************************************************************/
viewHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//go to MapActivity with variables for current user and index of button pressed
Intent i = new Intent(getApplicationContext(), MapsActivity.class);
i.putExtra("user", me);
i.putExtra("otheruser", getItem(position));
startActivity(i);
}
});
convertView.setTag(viewHolder);
}
else{
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.title.setText(getItem(position));
}
return convertView;
}
}
public class ViewHolder{
ImageView thumbnail;
TextView title;
Button button;
}
/******************************************************************************************
* ConnectTask
* This AsyncTask uses an HttpURLConnection to contact the compsci02 server and read the
data from the users text file.
* Has background function and OnPostExecute function
*****************************************************************************************/
public class ConnectTask extends AsyncTask<String, String, String>
{
/**************************************************************
* doInBackground
* Creates a connection with the file on the server and reads
each line one by one until the end of the file. for each
line it adds that user to the ArrayList object.
**************************************************************/
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
String user[]; //find user by reading line in file
try {
URL url = new URL("http://compsci02.snc.edu/cs460/2016/lewicj/WhereAreYou/UserFolders/" +me+".txt");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
String line = "";
while ((line = reader.readLine()) != null)
{
user = line.split(";");
if(Objects.equals(user[2],"1"))
{
data.add(user[1]);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "error";
}
@Override
protected void onPostExecute(String line) {
super.onPostExecute(line);
}
}
}
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/*********************************************************************************************
* Tab1 Class/Activity
* This activity connects to the users file on the compsci02 server and pulls that users
friends and places them in an ArrayList. Then it is shown in the UI through a list_item
placed in a listView object.
* executes asyncTask ConnectTask
*********************************************************************************************/
public class Tab1 extends Activity {
private ArrayList<String> data = new ArrayList<String>();
private String me; //get from login pages
public String[] friends = new String[100];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab1);
Bundle extras = getIntent().getExtras();
me = extras.getString("user");
new ConnectTask().execute(friends);
ListView lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(new MyListAdapter(this, R.layout.list_item, data));
}
/******************************************************************************************
* MyListAdapter
* This class creates an adapter for the listView object as well as the View for the list
in the UI. The adapters role is to connect the data of the list to the actual View
of the listView object in the UI.
* MyListAdapter and getView functions
*****************************************************************************************/
private class MyListAdapter extends ArrayAdapter<String>{
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
/**************************************************************
* getView
* This function creates a specific list_item for the listView
for each item in the ArrayList. it sets the thumbnail,
title, and button for each item and places them in the
listView object in the UI.
* Calls button's onClickListener
**************************************************************/
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.list_item_thumbnail);
viewHolder.title = (TextView) convertView.findViewById(R.id.list_item_text);
viewHolder.title.setText(getItem(position));
viewHolder.button = (Button) convertView.findViewById(R.id.btn_Map);
/**************************************************************
* button OnClickListener
* When map button is clicked for any one of the list_items,
onClickListener starts the MapsActivity for that specific
user.
**************************************************************/
viewHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//go to MapActivity with variables for current user and index of button pressed
Intent i = new Intent(getApplicationContext(), MapsActivity.class);
i.putExtra("user", me);
i.putExtra("otheruser", getItem(position));
startActivity(i);
}
});
convertView.setTag(viewHolder);
}
else{
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.title.setText(getItem(position));
}
return convertView;
}
}
public class ViewHolder{
ImageView thumbnail;
TextView title;
Button button;
}
/******************************************************************************************
* ConnectTask
* This AsyncTask uses an HttpURLConnection to contact the compsci02 server and read the
data from the users text file.
* Has background function and OnPostExecute function
*****************************************************************************************/
public class ConnectTask extends AsyncTask<String, String, String>
{
/**************************************************************
* doInBackground
* Creates a connection with the file on the server and reads
each line one by one until the end of the file. for each
line it adds that user to the ArrayList object.
**************************************************************/
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
String user[]; //find user by reading line in file
try {
URL url = new URL("http://compsci02.snc.edu/cs460/2016/lewicj/WhereAreYou/UserFolders/" +me+".txt");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
String line = "";
while ((line = reader.readLine()) != null)
{
user = line.split(";");
if(Objects.equals(user[2],"1"))
{
data.add(user[1]);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "error";
}
@Override
protected void onPostExecute(String line) {
super.onPostExecute(line);
}
}
}