tab2.java | |
File Size: | 5 kb |
File Type: | java |
package c.test2;
import android.app.Activity;
import android.content.Context;
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;
/*********************************************************************************************
* Tab2 Class/Activity
* This activity is not complete because I was having trouble with getting another listView
object working. everything is like Tab1 activity but there are two buttons for the
list_item instead of just one Map button.
* executes asyncTask ConnectTask
*********************************************************************************************/
public class Tab2 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_tab2);
Bundle extras = getIntent().getExtras();
me = extras.getString("user");
new ConnectTask().execute(friends);
ListView lv2 = (ListView) findViewById(R.id.listView2);
lv2.setAdapter(new MyListAdapter(this, R.layout.list_item2, data));
}
private class MyListAdapter extends ArrayAdapter<String>{
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
@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_thumbnail2);
viewHolder.title = (TextView) convertView.findViewById(R.id.list_item_text2);
viewHolder.title.setText(getItem(position));
viewHolder.buttonDecline = (Button) convertView.findViewById(R.id.btn_decline);
viewHolder.buttonAccept = (Button) convertView.findViewById(R.id.btn_accept);
viewHolder.buttonDecline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//make php script to call from here to write to friends list
}
});
viewHolder.buttonAccept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//make php script to call from here to write to friends list
}
});
convertView.setTag(viewHolder);
}
else{
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.title.setText(getItem(position));
}
return convertView;
}
}
public class ViewHolder{
ImageView thumbnail;
TextView title;
Button buttonDecline;
Button buttonAccept;
}
public class ConnectTask extends AsyncTask<String, String, String>
{
@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(";");
data.add(user[1]);
}
return "success";
} 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.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;
/*********************************************************************************************
* Tab2 Class/Activity
* This activity is not complete because I was having trouble with getting another listView
object working. everything is like Tab1 activity but there are two buttons for the
list_item instead of just one Map button.
* executes asyncTask ConnectTask
*********************************************************************************************/
public class Tab2 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_tab2);
Bundle extras = getIntent().getExtras();
me = extras.getString("user");
new ConnectTask().execute(friends);
ListView lv2 = (ListView) findViewById(R.id.listView2);
lv2.setAdapter(new MyListAdapter(this, R.layout.list_item2, data));
}
private class MyListAdapter extends ArrayAdapter<String>{
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
@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_thumbnail2);
viewHolder.title = (TextView) convertView.findViewById(R.id.list_item_text2);
viewHolder.title.setText(getItem(position));
viewHolder.buttonDecline = (Button) convertView.findViewById(R.id.btn_decline);
viewHolder.buttonAccept = (Button) convertView.findViewById(R.id.btn_accept);
viewHolder.buttonDecline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//make php script to call from here to write to friends list
}
});
viewHolder.buttonAccept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//make php script to call from here to write to friends list
}
});
convertView.setTag(viewHolder);
}
else{
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.title.setText(getItem(position));
}
return convertView;
}
}
public class ViewHolder{
ImageView thumbnail;
TextView title;
Button buttonDecline;
Button buttonAccept;
}
public class ConnectTask extends AsyncTask<String, String, String>
{
@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(";");
data.add(user[1]);
}
return "success";
} 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);
}
}
}