【发布时间】:2018-05-21 16:14:51
【问题描述】:
@SuppressWarnings({ "rawtypes" })
public void addAttendance(ArrayList<Properties> attendanceusers) {
//tl.removeView(tr);
tl.removeAllViews();
//addHeaderAttendance();
ctr=0;
for (Iterator i = attendanceusers.iterator(); i.hasNext();) {
Properties p = (Properties) i.next();
property_list.add(p);
/** Create a TableRow dynamically **/
tr = new TableRow(this);
picurl=p.getPic();
profile = new ImageView(this);
profile.setPadding(20,50,20,50);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
InputStream in = new URL(picurl).openStream();
bmp = BitmapFactory.decodeStream(in);
} catch (Exception e) {
// log error
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (bmp != null)
profile.setImageBitmap(bmp);
}
}.execute();
profile.setOnClickListener(this);
//myButton.setPadding(5, 5, 5, 5);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0);
Ll.setPadding(0, 0, 20, 0);
Ll.addView(profile,params);
tr.addView((View)Ll);
ctr++;
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
上面的代码是动态设计的。那里有一个图像,其中我将图像放在我的 android 屏幕上,这些图像是使用 Web 服务从我的数据库中获取的。问题是图像在闪烁,而且它太大了。我认为它正在闪烁,因为我正在线程中创建一个线程,但我仍然对如何修复它感到困惑。我的异步任务是如何获取图像。
public ArrayList<Properties> attendanceUse(String result) {
ArrayList<Properties> attendanceusers = new ArrayList<Properties>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Properties user = new Properties();
user.setPic(json_data.getString("student_ImgUrl"));
attendanceusers.add(user);
//attendanceusers.get(index)
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return attendanceusers;
}
上面的代码是我如何获取与我的 htdocs 中的 php 查询相关的图像。 student_imgurl 是我从我的 mysql 数据库中选择的用于获取 url 的列。
private void update()
{
final Handler handler = new Handler();
Timer timer = new Timer();
String splithis;
splithis=mySpinner.getSelectedItem().toString();
splited = splithis.split(" ");
course = splited[0];
room = splited[1];
sections = splited[2];
TimerTask task = new TimerTask() {
@Override
public void run() {
data = bw.getAttendanceFromDB(term, course,sections,day,room);
handler.post(new Runnable() {
public void run() {
try {
//asyntask class ito
ArrayList<Properties> attendanceusers = attendanceUse(data);
addAttendance(attendanceusers);
} catch (Exception e) {
// error, do something
}
}//run
});//handler mo
}//runmo
};//timer
timer.schedule(task, 0,1000); //timer
}
这是我的更新无效。它有一个计时器,因此图像会实时更新。
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
data ="";
new Thread(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
update();
}
});
}
}).start();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
return;
}
});
上面的代码是我在 Oncreate 中的代码。我有一个微调器/下拉菜单,他们将在其中选择课程室部分,然后当他们选择一个项目时,将显示该课程室部分中学生的图像。 我想要的是停止我的 UI 闪烁并调整图像视图的大小。
【问题讨论】: