在Listview

时间:2016-05-23 05:20:32

标签: android listview listviewitem

我想在android listview项目中添加可点击链接,我在listview的每一行中有5个项目,我想只有两个项目使用putextras()意图使用自定义适配器pls任意一个hlep我在webview中打开sholud

假设“链接1”是:“查看证明”(proofinglink)(在textview中)和“链接2”是:“获取更多详细信息”(附加链接)我想要当我点击链接1然后它应该在webview后面打开他自己的网址。当我点击链接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”和“附加链接”应该可以点击并在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;
   }

}

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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);
          }
      });

答案 2 :(得分: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

    }
});