在尝试从MySQL服务器获取一行数据之前,我使用了一个专栏并设法通过教程将其转换为listView
。但是为了从表中连续获取数据,我无法将其放入listView
。
所以我要做的就是将后台工作者的“转移”放到列表视图中。
PHP SQL查询:
$sql = "SELECT id, employee, hours FROM selected_shifts WHERE day = '$day';";
主要活动的导航抽屉:
if (items[0].equals(mExpandableListTitle.get(groupPosition))) {
if (items[0].equals(mExpandableListTitle.get(childPosition))) {
String day = "Monday";
OnChoice(day);
} else if (items[1].equals(mExpandableListTitle.get(childPosition))) {
String day= "Tuesday";
OnChoice(day);
} else if (items[2].equals(mExpandableListTitle.get(childPosition))) {
String day = "Wednesday";
OnChoice(day);
} else if (items[3].equals(mExpandableListTitle.get(childPosition))) {
String day = "Thursday";
OnChoice(day);
} else if (items[4].equals(mExpandableListTitle.get(childPosition))) {
String day = "Friday";
OnChoice(day);
}
}
mDrawerLayout.closeDrawer(GravityCompat.START);
return false;
}
});
}
public void OnChoice(String day) {
String type = "choice";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, day);
}
后台工作者(从MySQL服务器获取数据):
public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx) {
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String shifts_url = "***PHP LINK***";
if(type.equals("choice")) {
try {
String day = params[1];
URL url = new URL(shifts_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("day","UTF-8")+"="+URLEncoder.encode(day,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String shift="";
String line="";
while((line = bufferedReader.readLine())!= null) {
shift += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return shift;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Status");
}
@Override
protected void onPostExecute(String shift) {
//Toast the data as json
Toast.makeText(context, shift, Toast.LENGTH_LONG).show();
}
@Override
protected void onProgressUpdate(Void... values)
{
super.onProgressUpdate(values);
}
}
修改
将其放入ListView:
public void onTaskCompleted(String shift) {
try {
loadIntoListView(shift);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void loadIntoListView(String shift) throws JSONException {
JSONArray jsonArray = new JSONArray(shift);
String[] list = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
list[i] = obj.getString(shift);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter);
}
答案 0 :(得分:0)
在result
处,您应该将“shift”添加到适配器中的dataSet。
答案 1 :(得分:0)
那么你想要传递public interface TaskListener {
void onTaskCompleted(String shift);
}
的方法是使用自定义的“监听器”。
创建此
BackgroundWorker
在你的TaskListener taskListener;
BackgroundWorker(Context context, TaskListener taskListener){
this.context = context;
this.taskListener = taskListener;
}
上更改构造函数如下:
onPostExecute
然后在taskListener.onTaskCompleted(shift)
方法上,执行BackgroundWorker
。
当您调用this
构造函数传递BackgroundWorker backgroundWorker = new BackgroundWorker(this, this)
作为第二个参数时:
TaskListener
然后在Main上实现... MainActivity implements TaskListener
...
@override
onTaskCompleted(String shift) {
// You have your `shift` here to do with as you please
}
并实现该方法。
像这样:
{{1}}