首先,我在这里看到了许多解决方案,但无法理解。 我对如何在OnTextClick()中执行AsyncTask感到困惑。 并想知道我们如何实现这一点 请帮忙。
DownloadFormActivity。
public class DownloadFormActivity extends AppCompatActivity
{
private String[] link_1;
private Bitmap[] company_logo;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download_form);
Bitmap bitmap_1 = BitmapFactory.decodeResource(getResources(), R.drawable.***********);
Bitmap bitmap_2 = BitmapFactory.decodeResource(getResources(), R.drawable.***********);
Bitmap bitmap_3 = BitmapFactory.decodeResource(getResources(), R.drawable.***********);
link_1 = getResources().getStringArray(R.array.array_claimForm);
company_logo = new Bitmap[]{bitmap_1, bitmap_2, bitmap_3};
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
FormListAdapter adapter = new FormListAdapter(this, getdata());
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
//FormListAdapter fla =new FormListAdapter(context,getdata())
}
private List<FormList> getdata() {
List<FormList> listdata = new ArrayList<>();
for (int i = 0; i < link_1.length; i++) {
FormList formdata = new FormList();
formdata.setCompany_logo(company_logo[i]);
formdata.setLink_1(link_1[i]);
listdata.add(formdata);
}
return listdata;
}
public class DownloadTask extends AsyncTask<String,Integer,String> {
private ProgressDialog mProgressDialog;
@Override
protected void onPreExecute()
{
super.onPreExecute();
mProgressDialog = new ProgressDialog(getApplicationContext());
mProgressDialog.setMessage("Please Wait");
mProgressDialog.setTitle("Downloading File");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
}
@Override
protected String doInBackground(String... urls) {
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
int count = urls.length;
if(count == 1) {
try {
URL url = new URL(urls[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
int fileLength = connection.getContentLength();
input = connection.getInputStream();
output = new FileOutputStream("/sdcard/file_name.extension");
byte data[] = new byte[4096];
long total = 0;
int datacount;
while ((datacount = input.read(data)) != -1) {
if (isCancelled()) {
input.close();
return null;
}
total += datacount;
if (fileLength > 0)
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
mProgressDialog.dismiss();
if (result != null)
Toast.makeText(context, "Download error: " + result, Toast.LENGTH_LONG).show();
else
Toast.makeText(context, "File downloaded", Toast.LENGTH_SHORT).show();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgress(progress[0]);
}
}
}
FormListAdapter
ublic class FormListAdapter extends RecyclerView.Adapter<FormListAdapter.MyViewHolder> {
List<FormList> data = Collections.emptyList();
Context context;
LayoutInflater inflater;
public FormListAdapter(Context context,List<FormList> data)
{
this.context = context;
this.data = data;
this.inflater = LayoutInflater.from(context);
}
@Override
public MyViewHolder onCreateViewHolder(final ViewGroup parent, int viewType)
{
View view = inflater.inflate(R.layout.download_form_list, parent,false);
return new MyViewHolder(view, new MyViewHolder.ViewHolderClicks() {
@Override
public void onTextClick()
{
// how to call AsyncTask here
}
});
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
FormList current = data.get(position);
holder.imageView.setImageBitmap(current.getCompany_logo());
holder.textView_1.setText(current.getLink_1());
holder.textView_2.setText(current.getLink_2());
holder.textView_3.setText(current.getLink_3());
}
@Override
public int getItemCount() {
return data.size();
}
static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView imageView;
private TextView textView_1,textView_2,textView_3;
private Context context;
public ViewHolderClicks mClickListener;
public MyViewHolder(View itemView, final ViewHolderClicks mListener) {
super(itemView);
this.mClickListener = mListener;
imageView = (ImageView) itemView.findViewById(R.id.imageView_companyLogo);
textView_1 = (TextView) itemView.findViewById(R.id.textView_link_1);
textView_2 = (TextView) itemView.findViewById(R.id.textView_link_2);
textView_3 = (TextView) itemView.findViewById(R.id.textView_link_3);
textView_1.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
context = v.getContext();
mClickListener.onTextClick();
}
public interface ViewHolderClicks
{
void onTextClick();
}
}
}
答案 0 :(得分:0)
在AsyncTask
文件中对.java
进行编码,然后在TextClick()
中使用它。
答案 1 :(得分:0)
我认为最好的解决方案是在onTextClick()
上实施DownloadFormActivity
。以下是如何做到这一点:
<强> FormListAdapter 强>
public class FormListAdapter extends RecyclerView.Adapter<FormListAdapter.MyViewHolder> {
List<FormList> data = Collections.emptyList();
Context context;
LayoutInflater inflater;
private static ClickListener mClickListener;
public FormListAdapter(Context context,List<FormList> data)
{
this.context = context;
this.data = data;
this.inflater = LayoutInflater.from(context);
}
@Override
public MyViewHolder onCreateViewHolder(final ViewGroup parent, int viewType)
{
View view = inflater.inflate(R.layout.download_form_list, parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
FormList current = data.get(position);
holder.imageView.setImageBitmap(current.getCompany_logo());
holder.textView_1.setText(current.getLink_1());
holder.textView_2.setText(current.getLink_2());
holder.textView_3.setText(current.getLink_3());
}
@Override
public int getItemCount() {
return data.size();
}
public interface ClickListener {
void onItemClick(int position, View v);
}
public void setOnItemClickListener(ClickListener clickListener) {
mClickListener = clickListener;
}
static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView imageView;
private TextView textView_1,textView_2,textView_3;
private Context context;
public MyViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.imageView_companyLogo);
textView_1 = (TextView) itemView.findViewById(R.id.textView_link_1);
textView_2 = (TextView) itemView.findViewById(R.id.textView_link_2);
textView_3 = (TextView) itemView.findViewById(R.id.textView_link_3);
textView_1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
mClickListener.onItemClick(getAdapterPosition(), v);
}
}
}
然后在DownloadFormActivity
工具ForumListAdapter.ClickListener
中。要使用监听器:
FormListAdapter adapter = new FormListAdapter(this, getdata());
mAdapter.setOnItemClickListener(this);
然后你只需要使用界面:
@Override
public void onItemClick(int position, View v) {
new DownloadTask().execute(YOUR_DATA);
}
这是我在我的应用程序中实现它的方式。
onItemClick
适用于任何商品,或者您只需将其重命名为onTextClick()
。