package com.andromeda.smartresearch.adapter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.andromeda.smartresearch.AdminResearchManage;
import com.andromeda.smartresearch.R;
import com.andromeda.smartresearch.vo.ResearchVO;
public class ResearchManageListAdapter extends ArrayAdapter<ResearchVO> {
LayoutInflater inflater = null;
int resourceId = 0;
List<ResearchVO> arrList;
ProgressDialog pd;
// .
public ResearchManageListAdapter(Context context, int resourceId,
List<ResearchVO> objects) {
super(context, resourceId, objects);
arrList= objects;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.resourceId = resourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int index=position;
LinearLayout view = null;
if(convertView != null){
view = (LinearLayout) convertView ;
}else{
view = (LinearLayout) inflater.inflate(resourceId, parent, false);
}
ResearchVO vo = getItem(position);
TextView subject = (TextView) view.findViewById(R.id.subjectText);
subject.setText(vo.getSubject()); //
String startDay=dayFormatChange(vo.getStartDay());
String endDay=dayFormatChange(vo.getEndDay());
TextView period = (TextView) view.findViewById(R.id.periodText);
period.setText(startDay + " ~ " + endDay); //
TextView progressCheck = (TextView) view.findViewById(R.id.check);
long current_day=System.currentTimeMillis();
long end_day=Date.parse(endDay);
if(end_day<current_day){ //
progressCheck.setText(" ");
progressCheck.setTextColor(Color.RED);
}else{
progressCheck.setText(" ");
progressCheck.setTextColor(Color.GREEN);
}
view.findViewById(R.id.btnDelResearch).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(getContext())
.setTitle("")
.setMessage(" ?")
.setPositiveButton("(YES)", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
pd = ProgressDialog.show(getContext(), "", " .\n .");
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
pd.dismiss();
String result = msg.getData().getString("RESULT");
Toast.makeText(getContext(), result, Toast.LENGTH_SHORT).show();
if(result.equals("resultDeleteSuccess_questionDeleteSuccess_researchDeleteSuccess")){
Toast.makeText(getContext(), " .", Toast.LENGTH_SHORT).show();
remove(arrList.get(index));
AdminResearchManage.research_rowCount.setText(""+arrList.size());
}else if(result.equals("resultDeleteSuccess_questionDeleteSuccess_researchDeleteFail")){
Toast.makeText(getContext(), " .", Toast.LENGTH_SHORT).show();
}else if(result.equals("resultDeleteFail_questionDeleteSuccess_researchDeleteSuccess")){
Toast.makeText(getContext(), " .", Toast.LENGTH_SHORT).show();
remove(arrList.get(index));
AdminResearchManage.research_rowCount.setText(""+arrList.size());
}else if(result.equals("resultDeleteFail_questionDeleteFail_researchDeleteSuccess")){
Toast.makeText(getContext(), " .", Toast.LENGTH_SHORT).show();
remove(arrList.get(index));
AdminResearchManage.research_rowCount.setText(""+arrList.size());
}else if(result.equals("resultDeleteFail_questionDeleteFail_researchDeleteFail")){
Toast.makeText(getContext(), " .", Toast.LENGTH_SHORT).show();
}
}
};
final ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
@Override
public String handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
String result = null;
HttpEntity entity = response.getEntity();
result = parsingData(entity.getContent());
Message message =handler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putString("RESULT", result);
message.setData(bundle);
handler.sendMessage(message);
return result;
}
};
new Thread(new Runnable() {
@Override
public void run() {
try {
String url="http://172.16.1.37:8080/SmartDB/researchDelete.do";
HttpClient http = new DefaultHttpClient();
HttpParams params = http.getParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);
ArrayList<NameValuePair> researchParam = new ArrayList<NameValuePair>();
researchParam.add(new BasicNameValuePair("researchNO", ""+arrList.get(index).getNo()));
Log.i("researchNO", ""+arrList.get(index).getNo());
HttpPost httpPost = new HttpPost(url);
UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(researchParam, "UTF-8");
httpPost.setEntity(encodedFormEntity);
http.execute(httpPost, responseHandler);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
})
.setNegativeButton("(No)", null)
.setIcon(R.drawable.smarticon)
.show();
}
});
return view;
}
public String dayFormatChange(String day){ // 20110108 -> 2011/01/08
day = day.substring(0,4)+"/"+day.substring(4,6)+"/"+day.substring(6);
return day;
}
protected String parsingData(InputStream input) {
String parsingResult=null;
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(new InputStreamReader(input));
while(parser.next()!=XmlPullParser.END_DOCUMENT){
String name = parser.getName();
if(name != null && name.equals("result")){
parsingResult=parser.nextText();
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return parsingResult;
}
}
|