2016-05-23 33 views
4

我想在android listview项目中添加可点击的链接,我在listview的每一行中添加了5个项目,并且我只希望两个项目在webview中使用putextras()intents创建可点击和打开的sholud我正在使用自定义适配器请任何人hlep我。在列表视图的每一行中添加单击Listner

假设“链接1”是:“视图校对”(proofinglink)(在textview中)和“链接2”是:“获取更多详细信息”(附加链接)我想要点击链接1时,它应该在webview中打开在他自己的网址后面。当我点击链接2时:它也应该在他自己给定的网址中的webview中打开。使用putextras()意图 这里是我的代码。

  package tipster; 

      import java.io.BufferedReader; 
      import java.io.IOException; 
      import java.io.InputStream; 
      import java.io.InputStreamReader; 
      import java.io.UnsupportedEncodingException; 
      import java.net.URI; 
      import java.net.URISyntaxException; 
      import java.util.ArrayList; 
      import java.util.List; 
      import newsletter.Latest_list; 
      import newsletter.Webview_news; 

      import org.apache.http.HttpEntity; 
      import org.apache.http.HttpResponse; 
      import org.apache.http.NameValuePair; 
      import org.apache.http.client.ClientProtocolException; 
      import org.apache.http.client.HttpClient; 
      import org.apache.http.client.methods.HttpGet; 
      import org.apache.http.impl.client.DefaultHttpClient; 
      import org.apache.http.message.BasicNameValuePair; 
      import org.json.JSONArray; 
      import org.json.JSONException; 
      import org.json.JSONObject; 

      import android.app.Activity; 
      import android.app.Dialog; 
      import android.app.ProgressDialog; 
      import android.content.Context; 
      import android.content.Intent; 
      import android.net.ConnectivityManager; 
      import android.net.NetworkInfo; 
      import android.os.AsyncTask; 
      import android.os.Bundle; 
      import android.view.Menu; 
      import android.view.MenuItem; 
      import android.view.View; 
      import android.widget.AdapterView; 
      import android.widget.AdapterView.OnItemClickListener; 
      import android.widget.ListView; 
      import android.widget.Toast; 

      import com.example.newapp.R; 

      public class Tipster extends Activity { 
      ListView lv; 
      ArrayList<Tipster_list>listitem; 
      String title; 
      String serviceUrl; 

      @Override 
      protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_tipster); 

     if (isNetworkAvailable()) { 
     // Execution here 
     // Toast.makeText(this, "Network is Available!", 
        Toast.LENGTH_LONG).show(); 

     execute(); 

     title="Free Tip - Betfans"; 

     lv=(ListView) findViewById(R.id.listView_tipsterID); 

     listitem=new ArrayList<Tipster_list>(); 
    } 

else { 

    Toast.makeText(this, "Network is unavailable!", 
    Toast.LENGTH_LONG).show(); 
} 


} 

private boolean isNetworkAvailable() { 
    ConnectivityManager manager = (ConnectivityManager) 
    getSystemService(Context.CONNECTIVITY_SERVICE); 

    NetworkInfo networkInfo = manager.getActiveNetworkInfo(); 

    boolean isAvailable = false; 
    if (networkInfo != null && networkInfo.isConnected()) { 
     isAvailable = true; 
    } 
    return isAvailable; 
} 

private void execute() { 

    serviceUrl ="http://mobile.betfan.com/api/?  
    Action=top&type=default&key=MEu07MgiuWgXwJOo7Oe1aHL0yM8VvP"; 

    //Toast.makeText(LatestNews.this, serviceUrl, Toast.LENGTH_LONG).show(); 


    class LoginAsync extends AsyncTask<String, Void, String>{ 

     private Dialog loadingDialog; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      loadingDialog = ProgressDialog.show(Tipster.this, "Please 
      while wait", "Loading..."); 
     } 

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

      JSONObject jsonObject = new JSONObject(); 

      String dataString = jsonObject.toString(); 

      InputStream is = null; 
      List<NameValuePair> nameValuePairs = new 
       ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("data", dataString)); 

      String result = null; 

      try{ 



       HttpClient httpClient = new DefaultHttpClient(); 
       HttpGet httpRequest = new HttpGet(); 

       URI apiurl = new URI(serviceUrl); 

       httpRequest.setURI(apiurl); 

       HttpResponse response = httpClient.execute(httpRequest); 

       HttpEntity entity = response.getEntity(); 

       is = entity.getContent(); 

       BufferedReader reader = new BufferedReader(new 
      InputStreamReader(is, "UTF-8"), 8); 
       StringBuilder sb = new StringBuilder(); 

       String line = null; 
       while ((line = reader.readLine()) != null) 
       { 
        sb.append(line + "\n"); 
       } 
       result = sb.toString(); 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (URISyntaxException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return result; 
     } 

     protected void onPostExecute(String result){ 
      String s = result.trim(); 


      loadingDialog.dismiss(); 

       JSONObject respObject; 
       try { 
        respObject = new JSONObject(s); 
        String active = respObject.getString("status_message"); 


        if(active.equalsIgnoreCase("success")){ 
        JSONArray array =  
        respObject.getJSONArray("response"); 
          for (int i =0; i<array.length();i++){ 

         JSONObject jsonObject = array.getJSONObject(i); 
         String icon= jsonObject.getString("image"); 
         String name = jsonObject.getString("name"); 
         String total = jsonObject.getString("total"); 
         String proofinglink = 
         jsonObject.getString("proofinglink"); 
         String sitelink = jsonObject.getString("sitelink"); 

          listitem.add(new 
        Tipster_list(icon,name,"+"+total,proofinglink,sitelink)); 

         } 
         lv.setAdapter(new Tipster_adapter(Tipster.this, 
       listitem));  

        }else { 
         Toast.makeText(Tipster.this, "services received 
        Fail", Toast.LENGTH_LONG).show(); 

        } 

       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       lv.setOnItemClickListener(new OnItemClickListener() { 

        @Override 
        public void onItemClick(AdapterView<?> arg0, View arg1, 
          int arg2, long arg3) { 
         // TODO Auto-generated method stub 

         String getName=listitem.get(arg2).getName(); 
         String 
       getproofing=listitem.get(arg2).getProofinglink(); 
         String getsite=listitem.get(arg2).getSitelink(); 







         Intent intent = new Intent(getApplicationContext(), 
      Tipster_webview.class); 

         intent.putExtra("gettproofing_URl",getproofing); 
         intent.putExtra("getsiteURL",getsite); 
         startActivity(intent); 

        } 
       }); 

     } 

    } 

    LoginAsync la = new LoginAsync(); 
    la.execute(); 

    } 

我想“proofinglink”和“附加链接”可点击以及开放中的WebView

这里是自定义适配器

   public class Tipster_adapter extends BaseAdapter { 
       Context context; 
      ArrayList<Tipster_list>tipslist; 

       public Tipster_adapter(Context context, 
    ArrayList<Tipster_list> tipslist) { 
    super(); 
    this.context = context; 
    this.tipslist = tipslist; 
} 



    @Override 
    public int getCount() { 
    // TODO Auto-generated method stub 
    return tipslist.size(); 
} 

    @Override 
    public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return position; 
    } 

    @Override 
    public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
    } 

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

     LayoutInflater inflater=(LayoutInflater) 
    context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
     convertView=inflater.inflate(R.layout.activity_tipster_adapter 
       ,null); 


     ImageView icon= 
     (ImageView)convertView.findViewById(R.id.icon_tipsterIconID); 

     TextView name=(TextView) 
      convertView.findViewById(R.id.txt_name_tipsterID); 
     TextView total=(TextView) 
    convertView.findViewById(R.id.txt_total_tipsterID); 
     TextView proofingLink=(TextView) 
    convertView.findViewById(R.id.txt_proofinglink_tipsterID); 
     TextView siteLink=(TextView) 
    convertView.findViewById(R.id.txt_sitelink_tipsterID); 

     Tipster_list services=tipslist.get(position); 
     Picasso.with(context).load(services.getImage()).into(icon); 
     Log.d("Url",services.getImage()); 
     name.setText(services.getName()); 
     total.setText(services.getTotal()); 
     proofingLink.setText(services.getProofinglink()); 
     siteLink.setText(services.getSitelink()); 

     } 



    return convertView; 
    } 

} 

回答

0

你的代码是确定需要在添加监听你的适配器为您想要触发的视图例如

inside your adapter getview method paste this code like this 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    Tipster_list services=tipslist.get(position); 
    if(services!=null){ 
     LayoutInflater inflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
     convertView=inflater.inflate(R.layout.activity_custom_adapter,null); 
     ImageView icon=(ImageView)convertView.findViewById(R.id.icon_tipsterIconID); 
     TextView name=(TextView) convertView.findViewById(R.id.txt_name_tipsterID); 
     TextView total=(TextView) convertView.findViewById(R.id.txt_total_tipsterID); 
     TextView proofingLink=(TextView) convertView.findViewById(R.id.txt_proofinglink_tipsterID); 
     TextView siteLink=(TextView) convertView.findViewById(R.id.txt_sitelink_tipsterID); 
     Picasso.with(context).load(services.getImage()).into(icon); 
     Log.d("Url",services.getImage()); 
     name.setText(services.getName()); 
     total.setText(services.getTotal()); 
     proofingLink.setText(services.getProofinglink()); 
     siteLink.setText(services.getSitelink()); 
     convertView.setTag(position); 
     String getName=services.getName(); 
     final String getproofing=services.getProofinglink(); 
     final String getsite=services.getSitelink(); 
     proofingLink.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent(context, Tipster_webview.class); 
       intent.putExtra("gettproofing_URl",getproofing); 
       context.startActivity(intent); 
      } 
     }); 
     siteLink.setOnClickListener(new View.OnClickListener() { 

         @Override 
         public void onClick(View v) { 
          // TODO Auto-generated method stub 
          Intent intent = new Intent(context, Tipster_webview.class); 
          intent.putExtra("site",getsite); 
          context.startActivity(intent); 
         } 
        }); 
     } 



return convertView; 
} 
+0

请给我写一些代码,并告诉我在哪里适配器getview方法中粘贴此代码行,我是新的,感谢名单@Mehroz穆尼尔 –

+0

粘贴这样的代码 –

+0

我已经更新了我的答案来看看 –

0
TextView proofingLink=(TextView) convertView.findViewById(R.id.txt_proofinglink_tipsterID); 

    proofingLink.setOnClickListener(new OnItemClickListener() { 

    @Override 
    public void onClick(View arg0) { 

     String url = services.getProofinglink(); 
     Intent i = new Intent(Intent.ACTION_VIEW); 
     i.setData(Uri.parse(url)); 
     startActivity(i); 
      } 
     }); 
0
TextView proofingLink=(TextView) 
convertView.findViewById(R.id.txt_proofinglink_tipsterID); 
proofingLink.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
    } 
}); 
TextView siteLink=(TextView) 
convertView.findViewById(R.id.txt_sitelink_tipsterID); 
siteLink.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

    } 
}); 
+0

我得到“proofinglink”和“sitelink”形式的json,然后我如何通过id和SetOnclickLister将它们与findview绑定。请适当帮助我,我是新的谢谢@shubham –

+0

在getView()方法中编写此代码,并在该方法中您也有链接。 – Shubham

+0

我也写在getview中,但没有帮助完整它表明错误错误no.1“重复局部变量proofingLink”错误no.2“类型View中的方法setOnClickListener(View.OnClickListener)不适用于参数(new OnClickListener (){})” –

相关问题