2

首先,我在这里看到很多解决方案,但无法理解它。 我很困惑我如何在OnTextClick()内执行AsyncTask。 ,并想知道我们如何实现这一点。
请帮忙。如何在ViewHolder的RecyclerView.Adapter的TextView上添加OnClickListener来执行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

当我试图让onTextClick内的DownloadTask对象无法给出上下文 –

+0

你可以使用getActivity()作为上下文 –

回答

0

代码在自己的.java文件AsyncTask,然后用它在你的TextClick()

0

我认为最好的解决方案是在DownloadFormActivity上实施onTextClick()。这里是如何做到这一点:

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()

相关问题