- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个程序,屏幕上有大约 50 个按钮,它看起来相当难看。每个按钮都有一个异步下载,它只是从一个按钮复制到另一个按钮,并保存更改后的 URL 和文件名。我希望能够从使用按钮更改为使用一个微调器,希望只需一个异步下载任务,并且微调器作为选择下载 URL 和另存为的文件名的方式进行操作。这可能吗?在阅读了一些有关它的文档后,我感到很困惑。谢谢您的帮助。
更新:
使用下面建议的代码,我创建了这个:
public class SpinnerActivity extends Activity{
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private Button startBtn;
private ProgressDialog mProgressDialog;
Spinner spDownloadFrom;
private ArrayAdapter<CharSequence> spinnerArrayAdapter;
String url[]= {"www.abc.com/download/a.txt",
"www.abc.com/download/a.txt",
"www.abc.com/download/a.txt",
"www.abc.com/download/a.txt",
"www.abc.com/download/a.txt",
"www.abc.com/download/a.txt",
"www.abc.com/download/a.txt"
};
String links[]= {"Server 1",
"Server 2",
"Server 3",
"Server 4",
"Server 5",
"Server 6",
"Server 7",
};
public void onCreate(Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mProgressDialog = new ProgressDialog(SpinnerActivity.this);
mProgressDialog.setMessage("Please be patient, file downloading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
spDownloadFrom=(Spinner)findViewById(R.id.Spinner01);
spinnerArrayAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, links);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spDownloadFrom.setAdapter(spinnerArrayAdapter);
spDownloadFrom.setOnItemSelectedListener(new SpinnerListener(spDownloadFrom));
}
public class SpinnerListener implements OnItemSelectedListener
{
Spinner sp;
public SpinnerListener(View v)
{
sp=(Spinner)findViewById(v.getId());
}
@Override
public void onItemSelected(AdapterView<?> arg0, View v, int arg2,
long arg3) {
//Call to download class
new DownloadFile().equals(url[arg2].toString());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
class DownloadFile extends AsyncTask<String, Integer, String> { //put your download code
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
mProgressDialog.setProgress(progress[0]);
}
@Override
protected String doInBackground(String... aurl) {
try {
URL url = new URL(aurl[0]);
URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
int tickSize = 2 * fileLength / 100;
int nextProgress = tickSize;
Log.d(
"ANDRO_ASYNC", "Lenght of file: " + fileLength);
InputStream input = new BufferedInputStream(url.openStream());
String path = Environment.getExternalStorageDirectory()
+ "/Android/Data/"
+ getApplicationContext().getPackageName() + "/files";
File file = new File(path);
file.mkdirs();
File outputFile = new File(file, "test1.pdf");
OutputStream output = new FileOutputStream(outputFile);
byte data[] = new byte[1024 * 1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
if (total >= nextProgress) {
nextProgress = (int) ((total / tickSize + 1) * tickSize);
this.publishProgress((int) (total * 100 / fileLength));
}
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
mProgressDialog.dismiss();
} catch (Exception e) {
}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("Downloading",progress[0]);
}
@Override
protected void onPostExecute(String unused) {
File file = new File(Environment.getExternalStorageDirectory()
+ "/Android/Data/" + getApplicationContext().getPackageName()
+ "/files/test1.pdf");
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(SpinnerActivity .this,
"No Application Available to View PDF", Toast.LENGTH_LONG).show();
}
}
}
}
这对于我想要实现的目标是否正确?
最佳答案
public class Example extends Activity{
Spinner spDownloadFrom;
private ArrayAdapter<CharSequence> spinnerArrayAdapter;
String url[]= {"www.abc.com/download/a.txt",
"www.abc.com/download/a.txt",
"www.abc.com/download/a.txt",
"www.abc.com/download/a.txt",
"www.abc.com/download/a.txt",
"www.abc.com/download/a.txt",
"www.abc.com/download/a.txt"
};
String links[]= {"Server 1",
"Server 2",
"Server 3",
"Server 4",
"Server 5",
"Server 6",
"Server 7",
};
public void onCreate(Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spDownloadFrom=(Spinner)findViewById(R.id.addQuotation_spinnerProduct);
spinnerArrayAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, links);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spDownloadFrom.setAdapter(spinnerArrayAdapter);
spDownloadFrom.setOnItemSelectedListener(new SpinnerListener(spDownloadFrom));
}
public class SpinnerListener implements OnItemSelectedListener
{
Spinner sp;
public SpinnerListener(View v)
{
sp=(Spinner)findViewById(v.getId());
}
@Override
public void onItemSelected(AdapterView<?> arg0, View v, int arg2,
long arg3) {
//Call to download class
new DownloadFile().equals(url[arg2].toString());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
class DownloadFile extends AsyncTask<String, String, String> { //put your download code
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... aurl) {
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("Downloading",progress[0]);
}
@Override
protected void onPostExecute(String unused) {
}
}
}
这是一个满足您要求的示例类。我没有运行它,如果有一些错误,很抱歉,但基本思想在这里。
关于java - 安卓- "A spinner does not support item click events. Calling this method will raise an exception.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10458618/
CREATE OR REPLACE FUNCTION mover(src text, dst text, cpquery text, conname text, ifbin boolean) retu
在python中,raise和raise e在except block 中有区别吗? dis 向我显示不同的结果,但我不知道这是什么意思。 两者的最终行为是什么? import dis def a()
当使用 pytest.raises 测试由 try/except block 捕获的错误时,由于未引发错误而失败。 如果我使用字典查找进行简单测试,并且不对其进行 try/except 操作,则 py
我有以下代码 if self.download_format == 'mp3': raise NotImplementedError elif self.downloa
在技术、哲学、概念或其他方面有什么区别 raise "foo" 和 raise Exception.new("foo") ? 最佳答案 从技术上讲,第一个引发 RuntimeError,消息设置为 "
定义无参数异常: class MyException(Exception): pass 当提出时,有什么区别: raise MyException 和 raise MyException()
标题很容易理解——它们之间的区别是什么 raise Exception, "foo" 和 raise Exception("foo") 它做的事情是否完全相同,只是语法不同? 我使用的是 Python
为了处理 Rails 异常,我看到人们使用“raise SomeException.new”或“raise SomeException”,有什么区别? 说如果我有课 class UnableToCr
当我从终端运行我的 .py 文件时,我没有收到任何错误,但是当我作为可执行文件(完全相同的应用程序)运行时,我在这一行收到错误 raise RuntimeError(f'Line lengths in
我在编写一小段代码时遇到了困难: act_therm.sa_handler=handler_therm; sigaction(SIGUSR1,&act_thermom,NULL); w
我试图在此页面中显示四张照片: http://progenygenealogy.com/products/family-tree-charts/photo-gallery-2.aspx 我怎样才能让左
什么是raise A, B做?它与raise A有何不同?是吗? 一些例子(运行在python 2.7的解释器上): class E(Exception): pass e = E() rais
如何优雅地实现 "Samurai principle" (返回胜利,或者根本没有)我的功能? return if else raise 最佳答案 如果你绝对想在表达式中 raise,你可以这样做
Python中的raise和raise from有什么区别? try: raise ValueError except Exception as e: raise IndexError
Python中try块可以捕获测试代码块中的错误。except块可以处理错误。finally块可以执行代码,而不管try-和except块的结果如何。本文主要介绍Python 抛出引发异常(rais
我正在用 d3js 构建一个网络(基本上是节点和链接) 当我将鼠标悬停在一个节点上时,我想突出显示关联的链接并将它们置于父节点的顶部以使其真正可见 在鼠标悬停时,我做了这样的东西 // get lin
在 WPF 中,我正在尝试使用 moq 来引发一个事件,该事件具有与其 Hook 的异步监听器: 我的代码: public class Provider { private I
我在这段代码(来自 https://www.django-rest-framework.org/tutorial/3-class-based-views/ )上有一条 pylint 消息(w0707)
我有一个带有 Let 函数的类模块,该函数会引发自定义错误,示例如下所示 Private pValue As Double Public Property Let Value(v As Double)
我试图将表限制为只有一条记录,并禁止所有添加更多记录的尝试。我创建了此触发器:CREATE TRIGGER abort_insert_to_my_tbl BEFORE INSERT ON my_tbl
我是一名优秀的程序员,十分优秀!