package com.example.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class Teacher_subject_list extends Activity {
private ListView lstView;
private ImageAdapter imageAdapter;
ArrayList<HashMap<String, Object>> MyArrList = new ArrayList<HashMap<String, Object>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ProgressBar
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.teacher_subject_list);
Intent intent = getIntent();
final String Username = intent.getStringExtra("Username");
// ListView and imageAdapter
lstView = (ListView) findViewById(R.id.listView1);
lstView.setClipToPadding(false);
imageAdapter = new ImageAdapter(getApplicationContext());
lstView.setAdapter(imageAdapter);
setProgressBarIndeterminateVisibility(true);
new LoadContentFromServer().execute();
final Button btn1 = (Button) findViewById(R.id.submit_stat);
// Perform action on click
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setProgressBarIndeterminateVisibility(true);
new LoadContentFromServer().execute();
}
});
final Button btnBack = (Button) findViewById(R.id.stat_back);
// Perform action on click
btnBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent newActivity = new Intent(Teacher_subject_list.this,Login.class);
newActivity.putExtra("Username", Username);
startActivity(newActivity);
finish();
}
});
}
class LoadContentFromServer extends AsyncTask<Object, Integer, Object> {
@Override
protected Object doInBackground(Object... params1) {
Intent intent = getIntent();
final String Username = intent.getStringExtra("Username");
final EditText inputText = (EditText)findViewById(R.id.txtid);
String url = "http://jojomoofy.890m.com/android/getListSubject.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("txtKeyword", inputText.getText().toString()));
params.add(new BasicNameValuePair("txtUsername", Username));
JSONArray data;
try {
data = new JSONArray(getHttpPost(url,params));
MyArrList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
String name = c.getString("subjectname");
String check=CheckStatus(name);
if(check.equals("1"))
{
map = new HashMap<String, Object>();
map.put("subjectname", c.getString("subjectname"));
// Full (for View Popup)
//map.put("ImagePathFull", (String)c.getString("ImagePath_FullPhoto"));
MyArrList.add(map);
}
publishProgress(i);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public void onProgressUpdate(Integer... progress) {
imageAdapter.notifyDataSetChanged();
}
@Override
protected void onPostExecute(Object result) {
setProgressBarIndeterminateVisibility(false); // When Finish
}
}
class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
public int getCount() {
return MyArrList.size();
}
public Object getItem(int position) {
return MyArrList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.column_subject_list, null);
}
// ColImgID
TextView txtPicName = (TextView) convertView.findViewById(R.id.ColName2);
txtPicName.setText(MyArrList.get(position).get("subjectname").toString());
Intent intent = getIntent();
final String Username = intent.getStringExtra("Username");
final int num =1;
final Button bttest = (Button) convertView.findViewById(R.id.bt_test);
// Perform action on click
bttest.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String strsubjectname = MyArrList.get(position).get("subjectname")
.toString();
CreatePointAllTable(strsubjectname);
CreateInfoTestTable(strsubjectname);
Intent newActivity = new Intent(Teacher_subject_list.this,Teacher_question_list.class);
newActivity.putExtra("Subjectname", strsubjectname );
newActivity.putExtra("Username", Username );
newActivity.putExtra("num", num);
startActivity(newActivity);
finish();
}
});
return convertView;
}
}
public void CreatePointAllTable(String strsubjectname)
{
Intent intent = getIntent();
final String Username = intent.getStringExtra("Username");
String url = "http://jojomoofy.890m.com/android/CreatePointAllTable.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sUsername", Username));
params.add(new BasicNameValuePair("sSubjectname", strsubjectname));
String resultServer = getHttpPost(url,params);
String strStatusID = "0";
String strError = "Unknow Status!";
JSONObject c;
try {
c = new JSONObject(resultServer);
strStatusID = c.getString("StatusID");
strError = c.getString("Error");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void CreateInfoTestTable(String strsubjectname)
{
Intent intent = getIntent();
final String Username = intent.getStringExtra("Username");
String url = "http://jojomoofy.890m.com/android/CreateInfoTestTable.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sUsername", Username));
params.add(new BasicNameValuePair("sSubjectname", strsubjectname));
String resultServer = getHttpPost(url,params);
String strStatusID = "0";
String strError = "Unknow Status!";
JSONObject c;
try {
c = new JSONObject(resultServer);
strStatusID = c.getString("StatusID");
strError = c.getString("Error");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String CheckStatus(String subjectname)
{
Intent intent = getIntent();
final String Username = intent.getStringExtra("Username");
String url = "http://jojomoofy.890m.com/android/CheckStatus.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sUsername", Username));
params.add(new BasicNameValuePair("sSubjectname", subjectname));
String resultServer = getHttpPost(url,params);
String Check = "0";
JSONObject c;
try {
c = new JSONObject(resultServer);
Check = c.getString("Check");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Check;
}
/*** Get JSON Code from URL ***/
public String getHttpPost(String url,List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Status OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content,"UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
/***** Get Image Resource from URL (End) *****/
}
ส่วนอันนี้ Logcat Code
05-10 18:08:00.391: E/InputEventReceiver(2995): Exception dispatching input event.
05-10 18:08:00.391: E/MessageQueue-JNI(2995): Exception in MessageQueue callback: handleReceiveCallback
05-10 18:08:00.395: E/MessageQueue-JNI(2995): java.lang.IllegalStateException: The content of the adapter has changed
but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread,
but only from the UI thread. [in ListView(2131296278, class android.widget.ListView) with Adapter(class
com.example.test.Teacher_question_list$ImageAdapter)]
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at android.widget.ListView.layoutChildren(ListView.java:1544)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at android.widget.AbsListView.onTouchModeChanged(AbsListView.java:3213)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at android.view.ViewTreeObserver.dispatchOnTouchModeChanged(ViewTreeObserver.java:712)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at android.view.ViewRootImpl.ensureTouchModeLocally(ViewRootImpl.java:3074)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at android.view.ViewRootImpl.ensureTouchMode(ViewRootImpl.java:3058)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3202)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at android.os.MessageQueue.nativePollOnce(Native Method)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at android.os.MessageQueue.next(MessageQueue.java:125)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at android.os.Looper.loop(Looper.java:124)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at java.lang.reflect.Method.invokeNative(Native Method)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at java.lang.reflect.Method.invoke(Method.java:511)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-10 18:08:00.395: E/MessageQueue-JNI(2995): at dalvik.system.NativeStart.main(Native Method)
05-10 18:08:00.407: E/AndroidRuntime(2995): at android.os.MessageQueue.next(MessageQueue.java:125)