使图像在android中作为链接工作

时间:2016-11-18 14:11:22

标签: android listview imageview

我已经做了这个,它列出了网页上的一些链接,但我不想在这张图片上只显示一个箭头screenshot 这是我的代码:

public class Controller extends AppCompatActivity {

    private String TAG = Controller.class.getSimpleName();

    private ProgressDialog pDialog;
    private ListView lv;

    private static String url;

    private static final String TITLE = "title";
    private static final String LINK = "link";

    ArrayList<HashMap<String, String>> linkList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_view);
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);


        linkList = new ArrayList<>();
        url = this.getString(R.string.url);
        lv = (ListView) findViewById(R.id.list);
        new GetLinks().execute();
    }


    private class GetLinks extends AsyncTask<Void, Void, Void> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(Controller.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {

            Document doc = null;
            Elements links;

            try {
                doc = Jsoup.connect(url).get();

                links = doc.getElementsByClass("processlink");

                linkList = ParseHTML(links);
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            if (pDialog.isShowing())
                pDialog.dismiss();

            ListAdapter adapter = new SimpleAdapter(
                    Controller.this, linkList,
                    R.layout.list_item, new String[]{TITLE, LINK}, new int[]{R.id.title});

            lv.setAdapter(adapter);
        }

    }

    private ArrayList<HashMap<String, String>> ParseHTML(Elements links) throws IOException {

        if (links != null) {

            ArrayList<HashMap<String, String>> linkList = new ArrayList<>();

            for (Element link : links) {

                final String linkhref = this.getString(R.string.http) + link.select("a").attr("href");
                String linktext = link.text();

                HashMap<String, String> student = new HashMap<>();

                student.put(TITLE, linktext);
                student.put(LINK, linkhref);

                linkList.add(student);

            }
            return linkList;
        }
        else {
            Log.e(TAG, "Couldn't get html from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get html from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG)
                            .show();
                }
            });

        }

        return null;

    }

}

这是我的listview

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:paddingTop="6dip"
    android:textColor="@color/colorPrimaryDark"
    android:textSize="16sp"
    android:textStyle="bold"
    android:text="Title"/>

<ImageView
    android:id="@+id/link"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="start"

    android:src="@drawable/ic_action_name"
    android:autoLink="web"
    />

如果将imageView更改为textView,它将显示链接并且它可以正常工作,但我不希望链接只是屏幕右侧的箭头,但我不确定如何让我工作。

1 个答案:

答案 0 :(得分:0)

使用以下代码在您的imageview上创建一个onclick:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("YOUR_LINK_HERE"));
startActivity(browserIntent);

如果您不想创建自己的适配器,可以使用它:

listview.setOnItemClickListener(new OnItemClickListener(){   
@Override
public void onItemClick(AdapterView<?>adapter,View v, int position){
    Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("YOUR_LINK_HERE"));
startActivity(browserIntent);
}