在 Listview 的每一行中添加点击监听器

add click Listner in each item of row in Listview

我想在 android listview 项目中添加可点击的 link,我在 listview 的每一行中有 5 个项目,我只想使用 putextras() intents 在 webview 中使两个项目可点击并打开 sholud我正在使用自定义适配器,请任何人帮助我。

假设 "link 1" 是:"view Proofing"(proofinglink)(在文本视图中)并且 "link 2" 是:"Get More Detail"(sitelink ) 我想当我单击 link 1 时,它应该在他自己给定的 url 后面的 webview 中打开。当我点击 link 2 时:它也应该在他自己给定的 url 的 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" 和 "sitelink" 应该可以点击并在网络视图中打开

这是自定义适配器

                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;
   }

}

你的代码没问题你需要在你的适配器中添加一个侦听器,例如你想在其上触发意图的视图

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;
}
    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);
          }
      });
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

    }
});