我在主要活动中使用asynctask并从服务器数据填充列表项。现在我在每个列表项旁边放置了一个下载符号,现在我想为它实现onclick监听器。我没有使用任何自定义适配器将下载图标放在列表项旁边,我在xml中使用了imageview。现在我又制作了一个“下载”类,它从服务器下载一个特定的文件。当我在主要活动中点击图片(下载图标)时,它显示一个空指针异常。 我的主要活动:
public class MainActivity extends Activity {
TextView uid;
Button Btngetdata;
//URL to get JSON Array
private static String url = "myurl";
//JSON Node Names
private static final String TAG_POSTS = "allposts";
private static final String TAG_SINGLEPOST = "post";
JSONArray user = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Btngetdata = (Button)findViewById(R.id.getdata);
Btngetdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new JSONParse().execute();
}
});
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Getting JSON Array
user = json.getJSONArray(TAG_POSTS);
String[] allposts = new String[user.length()];
for(int i = 0; i < user.length(); i++) {
JSONObject c = user.getJSONObject(i);
allposts[i]=c.getString(TAG_SINGLEPOST);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this,
R.layout.singlepost,
R.id.singlepostid,
allposts
);
ListView list = (ListView) findViewById(R.id.allposts);
list.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
我的下载课程:
public class download extends Activity {
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// startBtn = (Button)findViewById(R.id.downloadid);
ImageView img = (ImageView) findViewById(R.id.downloadid);
img.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
startDownload();
}
});
}
public void startDownload() {
String url = "urlforfile";
new DownloadFileAsync().execute(url);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/fileonserver.pdf");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
}
现在我想为listview中的每个下载图标实现onclick监听器。有人可以帮忙吗?
答案 0 :(得分:0)
对于listview项目视图的子项,您希望每次回调的次数(或多或少)相同吗?您可以采取几种方法。您已经在适配器中实现了getView()
,对吗?让我们在那里设置回调。
public View getView (int position, View convertView, ViewGroup parent){
...
converView.findViewbyId(R.id.ID_OF_YOUR_DOWNLOAD_ICON).setOnClickListener(new OnClickListener() {
//new DownloadFileAsync().execute(url);?
});
return convertView;
}
我不知道您的NPE在哪里,如果您发布堆栈跟踪,我们也可以提供帮助。