2015-09-18 75 views
1

我有一個ListView的Baseadapter,當其中1個元素被點擊時它應該執行一個AsyncTask。的onclick裏面Baseadapter和工作然而異步執行不在這裏工作是我的baseAdapterandroid asynctask沒有執行內部baseadapter

public class LocalFeed_CustomView extends BaseAdapter { 

    JSONObject names; 
    Context ctx; 
    LayoutInflater myiflater; 

public LocalFeed_CustomView(){} 


    public LocalFeed_CustomView(JSONObject arr,Context c) { 
     ctx = c; 
     names = arr; 
    // myiflater = (LayoutInflater)c.getSystemService(c.LAYOUT_INFLATER_SERVICE); 
     // System.err.println("vv:" + arr); 
    } 


    @Override 
    public int getCount() { 
     try { 
      JSONArray jaLocalstreams = names.getJSONArray("localstreams"); 
      return jaLocalstreams.length(); 
     } catch (Exception e) { 
      Toast.makeText(ctx,"Error: Please try again",Toast.LENGTH_LONG).show(); 
      return names.length(); 
     } 


    } 




    @Override 
    public Object getItem(int position) { 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position,View convertView, ViewGroup parent) { 
     try { 
      if(convertView==null) { 

       LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = li.inflate(R.layout.customadapter, null); 
      } 

       TextView votes= (TextView)convertView.findViewById(R.id.votes); 

       JSONArray jaLocalstreams = names.getJSONArray("localstreams"); 
       final JSONObject jsonObject = jaLocalstreams.getJSONObject(position); 
       jsonObject.getInt("id"); 
    // the click works because the toast message fires 
      votes.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        try { 
         int Stream_ID= jsonObject.getInt("id"); 
         SharedPreferences myaccount = ctx.getSharedPreferences("userInfo", ctx.MODE_PRIVATE); 
         int Profile_id=myaccount.getInt("id", 0); 
         Toast.makeText(ctx, "click worked", Toast.LENGTH_SHORT).show(); 
      // the execute below is not firing off 
         new Add_Votes(Stream_ID,Profile_id).execute(); 
        } 
        catch (Exception e) 
        { 
         e.getCause(); 
        } 

       } 
      }); 




      return convertView; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return convertView; 


    } 
} 

正如你可以看到執行不工作在他們兩人中的int值有一個數字。這是我的AsyncTask

public class Add_Votes extends AsyncTask<String,String,String> { 
    HttpURLConnection conn; 
    URL url; 
    String result=""; 
    DataOutputStream wr; 
    String Stream_URL; 
    Activity m; 
    int stream_id,profile_id; 


    public Add_Votes(int stream_id,int profile_id) 
    { 
     this.stream_id=stream_id; 
     this.profile_id=profile_id; 

    } 


    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     Stream_URL= m.getResources().getString(R.string.PathUrl)+"/api/addvote"; 
//this Toast never fires off 
    Toast.makeText(m.getApplicationContext(),"clicked",Toast.LENGTH_SHORT); 
    } 

    @Override 
    protected String doInBackground(String... params) { 


     BufferedReader reader=null; 
     try{ 
      url = new URL(Stream_URL); 
      conn = (HttpURLConnection) url.openConnection(); 
      conn.setDoOutput(true); 
      conn.setRequestMethod("POST"); 
      conn.connect(); 

      conn.setReadTimeout(10000); 
      conn.setConnectTimeout(15000); 
      String cert="id="+profile_id+"&stream_id="+stream_id; 
      wr = new DataOutputStream(conn.getOutputStream()); 
      wr.writeBytes(cert); 
      wr.flush(); 
      wr.close(); 

      reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      StringBuilder sBuilder = new StringBuilder(); 

      String line = ""; 
      while ((line = reader.readLine()) != null) { 
       sBuilder.append(line + "\n"); 
      } 

      result = sBuilder.toString(); 
      reader.close(); 
      conn.disconnect(); 
      return result; 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

     return result; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 

     try { 

      Toast.makeText(m.getApplicationContext(),"Voted",Toast.LENGTH_SHORT).show(); 

     } 
     catch (Exception e) { 
      Toast.makeText(m.getApplicationContext(),"Inconclusive",Toast.LENGTH_SHORT).show(); 
     } 

    } 
} 

Add_Votes只是一個AsyncTask沒有任何活動與它關聯。關於如何從baseadapter調用AsyncTask的任何建議都會很棒。它需要來自baseadapter,因爲每行都有不同的值,具體取決於點擊的項目,然後傳遞給異步任務。

回答

1

我注意到的一件事是您正在使用變量活動m並且您尚未在您的AsyncTask中初始化它。嘗試從您的BaseAdapter傳遞上下文到AsyncTask。

在Add_Votes:

private Context context; 

public Add_Votes(Context context ,int stream_id,int profile_id) 
{ 
    this.stream_id=stream_id; 
    this.profile_id=profile_id; 
    this.context = context; 

} 



@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    Toast.makeText(context,"clicked",Toast.LENGTH_SHORT).show(); 

} 

在你BaseAdapter:

Add_Votes add_Votes = new Add_Votes(ctx,Stream_ID,Profile_id); 
add_Votes.execute();