Wanna make to open website when I click it from ListView. I made it with some youtube videos. Mixed up those youtube videos and of course, It’s not working. What should fix or add more code? I put my every codes to understand it. Need you guys help a lot.
JavaScript
x
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Teams">
<ListView
android:id="@+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
JavaScript
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/teamImage"
android:src="@mipmap/ic_launcher"
android:layout_width="60dp"
android:layout_height="60dp"/>
<TextView
android:id="@+id/teamName"
android:layout_toRightOf="@id/teamImage"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextAppearance.AppCompat.Title"
android:text="Name"/>
<TextView
android:id="@+id/teamDivision"
android:layout_toRightOf="@id/teamImage"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/teamName"
style="@style/TextAppearance.AppCompat.SearchResult.Subtitle"
android:text="Division"/>
</RelativeLayout>
Two upper codes are for layout.
JavaScript
package com.example.gridiron;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
* Use the {@link Settings#newInstance} factory method to
* create an instance of this fragment.
*/
public class Teams extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public Teams() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Settings.
*/
// TODO: Rename and change types and number of parameters
public static Teams newInstance(String param1, String param2) {
Teams fragment = new Teams();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_teams, container, false);
ListView listView = (ListView) view.findViewById(R.id.listView);
ArrayList<TeamsList> arrayList = new ArrayList<>();
arrayList.add(new TeamsList(R.drawable.arizona, "Arizona Cardinals", "NFC West", "https://namu.wiki/w/%EC%95%A0%EB%A6%AC%EC%A1%B0%EB%82%98%20%EC%B9%B4%EB%94%94%EB%84%90%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.atlanta, "Atlanta Falcons", "NFC South", "https://namu.wiki/w/%EC%95%A0%ED%8B%80%EB%9E%9C%ED%83%80%20%ED%8C%B0%EC%BB%A8%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.baltimore, "Baltimore Ravens", "AFC North", "https://namu.wiki/w/%EB%B3%BC%ED%8B%B0%EB%AA%A8%EC%96%B4%20%EB%A0%88%EC%9D%B4%EB%B8%90%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.buffalo, "Buffalo Bills", "AFC East", "https://namu.wiki/w/%EB%B2%84%ED%8C%94%EB%A1%9C%20%EB%B9%8C%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.carolina, "Carolina Panthers", "NFC South", "https://namu.wiki/w/%EC%BA%90%EB%A1%A4%EB%9D%BC%EC%9D%B4%EB%82%98%20%ED%8C%AC%EC%84%9C%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.chicago, "Chicago Bears", "NFC North", "https://namu.wiki/w/%EC%8B%9C%EC%B9%B4%EA%B3%A0%20%EB%B2%A0%EC%96%B4%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.cincinnati, "Cincinnati Bengals", "AFC North", "https://namu.wiki/w/%EC%8B%A0%EC%8B%9C%EB%82%B4%ED%8B%B0%20%EB%B2%B5%EA%B3%A8%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.cleveland, "Cleveland Browns", "AFC North", "https://namu.wiki/w/%ED%81%B4%EB%A6%AC%EB%B8%94%EB%9E%9C%EB%93%9C%20%EB%B8%8C%EB%9D%BC%EC%9A%B4%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.dallas, "Dallas Cowboys", "NFC East", "https://namu.wiki/w/%EB%8C%88%EB%9F%AC%EC%8A%A4%20%EC%B9%B4%EC%9A%B0%EB%B3%B4%EC%9D%B4%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.denver, "Denver Broncos", "AFC West", "https://namu.wiki/w/%EB%8D%B4%EB%B2%84%20%EB%B8%8C%EB%A1%B1%EC%BD%94%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.detroit, "Detroit Lions", "NFC North", "https://namu.wiki/w/%EB%94%94%ED%8A%B8%EB%A1%9C%EC%9D%B4%ED%8A%B8%20%EB%9D%BC%EC%9D%B4%EC%96%B8%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.greenbay, "Green Bay Packers", "NFC North", "https://namu.wiki/w/%EA%B7%B8%EB%A6%B0%EB%B2%A0%EC%9D%B4%20%ED%8C%A8%EC%BB%A4%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.houston, "Houston Texans", "AFC South", "https://namu.wiki/w/%ED%9C%B4%EC%8A%A4%ED%84%B4%20%ED%85%8D%EC%82%B0%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.indianapolis, "Indianapolis Colts", "AFC South", "https://namu.wiki/w/%EC%9D%B8%EB%94%94%EC%95%A0%EB%82%98%ED%8F%B4%EB%A6%AC%EC%8A%A4%20%EC%BD%9C%EC%B8%A0"));
arrayList.add(new TeamsList(R.drawable.jacksonville, "Jacksonville Jaguars", "AFC South", "https://namu.wiki/w/%EC%9E%AD%EC%8A%A8%EB%B9%8C%20%EC%9E%AC%EA%B7%9C%EC%96%B4%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.kansascity, "Kansas City Chiefs", "AFC West", "https://namu.wiki/w/%EC%BA%94%EC%9E%90%EC%8A%A4%EC%8B%9C%ED%8B%B0%20%EC%B9%98%ED%94%84%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.lasvegas, "Las Vegas Raiders", "AFC West", "https://namu.wiki/w/%EB%9D%BC%EC%8A%A4%EB%B2%A0%EC%9D%B4%EA%B1%B0%EC%8A%A4%20%EB%A0%88%EC%9D%B4%EB%8D%94%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.losangeleschargers, "Los Angeles Chargers", "AFC West", "https://namu.wiki/w/%EB%A1%9C%EC%8A%A4%EC%95%A4%EC%A0%A4%EB%A0%88%EC%8A%A4%20%EC%B0%A8%EC%A0%80%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.losangelesrams, "Los Angeles Rams", "NFC West", "https://namu.wiki/w/%EB%A1%9C%EC%8A%A4%EC%95%A4%EC%A0%A4%EB%A0%88%EC%8A%A4%20%EB%9E%A8%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.miami, "Miami Dolphins", "AFC East", "https://namu.wiki/w/%EB%A7%88%EC%9D%B4%EC%95%A0%EB%AF%B8%20%EB%8F%8C%ED%95%80%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.minnesota, "Minnesota Vikings", "NFC North", "https://namu.wiki/w/%EB%AF%B8%EB%84%A4%EC%86%8C%ED%83%80%20%EB%B0%94%EC%9D%B4%ED%82%B9%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.newengland, "New England Patriots", "AFC East", "https://namu.wiki/w/%EB%89%B4%EC%9E%89%EA%B8%80%EB%9E%9C%EB%93%9C%20%ED%8C%A8%ED%8A%B8%EB%A6%AC%EC%96%B4%EC%B8%A0"));
arrayList.add(new TeamsList(R.drawable.neworleans, "New Orleans Saints", "NFC South", "https://namu.wiki/w/%EB%89%B4%EC%98%AC%EB%A6%AC%EC%96%B8%EC%8A%A4%20%EC%84%B8%EC%9D%B8%EC%B8%A0"));
arrayList.add(new TeamsList(R.drawable.newyorkgiants, "New York Giants", "NFC East", "https://namu.wiki/w/%EB%89%B4%EC%9A%95%20%EC%9E%90%EC%9D%B4%EC%96%B8%EC%B8%A0"));
arrayList.add(new TeamsList(R.drawable.newyorkjets, "New York Jets", "AFC East", "https://namu.wiki/w/%EB%89%B4%EC%9A%95%20%EC%A0%9C%EC%B8%A0"));
arrayList.add(new TeamsList(R.drawable.philadelphia, "Philadelphia Eagles", "NFC East", "https://namu.wiki/w/%ED%95%84%EB%9D%BC%EB%8D%B8%ED%94%BC%EC%95%84%20%EC%9D%B4%EA%B8%80%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.pittsburgh, "Pittsburgh Steelers", "AFC North", "https://namu.wiki/w/%ED%94%BC%EC%B8%A0%EB%B2%84%EA%B7%B8%20%EC%8A%A4%ED%8B%B8%EB%9F%AC%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.sanfrancisco, "San Francisco 49ers", "NFC West", "https://namu.wiki/w/%EC%83%8C%ED%94%84%EB%9E%80%EC%8B%9C%EC%8A%A4%EC%BD%94%20%ED%8F%AC%ED%8B%B0%EB%82%98%EC%9D%B4%EB%84%88%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.seattle, "Seattle Seahawks", "NFC West", "https://namu.wiki/w/%EC%8B%9C%EC%95%A0%ED%8B%80%20%EC%8B%9C%ED%98%B8%ED%81%AC%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.tampabay, "Tampa Bay Buccaneers", "NFC South", "https://namu.wiki/w/%ED%83%AC%ED%8C%8C%EB%B2%A0%EC%9D%B4%20%EB%B2%84%EC%BB%A4%EB%8B%88%EC%96%B4%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.tennessee, "Tennessee Titans", "AFC South", "https://namu.wiki/w/%ED%85%8C%EB%84%A4%EC%8B%9C%20%ED%83%80%EC%9D%B4%ED%83%84%EC%8A%A4"));
arrayList.add(new TeamsList(R.drawable.washington, "Washington Football Team", "NFC East", "https://namu.wiki/w/%EC%9B%8C%EC%8B%B1%ED%84%B4%20%ED%92%8B%EB%B3%BC%ED%8C%80"));
TeamsListAdapter teamsListAdapter = new TeamsListAdapter(getActivity(), R.layout.list_row, arrayList);
listView.setAdapter(teamsListAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.valueOf(arrayList)));
if(intent.resolveActivity(requireContext().getPackageManager()) != null){
startActivity(intent);
}
}
});
return view;
}
}
JavaScript
package com.example.gridiron;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class TeamsListAdapter extends ArrayAdapter<TeamsList> {
private Context mContext;
private int mResource;
public TeamsListAdapter(@NonNull Context context, int resource, @NonNull ArrayList<TeamsList> objects) {
super(context, resource, objects);
this.mContext = context;
this.mResource = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(mResource, parent, false);
ImageView teamImage = convertView.findViewById(R.id.teamImage);
teamImage.setImageResource(getItem(position).getTeamImage());
TextView teamName = convertView.findViewById(R.id.teamName);
teamName.setText(getItem(position).getTeamName());
TextView teamDivision = convertView.findViewById(R.id.teamDivision);
teamDivision.setText(getItem(position).getTeamDivision());
return convertView;
}
}
JavaScript
package com.example.gridiron;
public class TeamsList {
int TeamImage;
String TeamName;
String TeamDivision;
String TeamURL;
public TeamsList(int teamImage, String teamName, String teamDivision, String teamURL) {
TeamImage = teamImage;
TeamName = teamName;
TeamDivision = teamDivision;
TeamURL = teamURL;
}
public int getTeamImage() {
return TeamImage;
}
public void setTeamImage(int teamImage) {
TeamImage = teamImage;
}
public String getTeamName() {
return TeamName;
}
public void setTeamName(String teamName) {
TeamName = teamName;
}
public String getTeamDivision() {
return TeamDivision;
}
public void setTeamDivision(String teamDivision) {
TeamDivision = teamDivision;
}
public String getTeamURL() {
return TeamURL;
}
public void setTeamURL(String teamURL) {
TeamURL = teamURL;
}
}
And last 3 codes are for class. It’s really hard to make it. Please, Help me!
Advertisement
Answer
Currently according to your code above when you click on a ListView item you are trying to open a url using the below Intent which is failing to construct a url from an ArrayList:
JavaScript
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.valueOf(arrayList)));
You should get the clicked item url first to be able to construct a valid url like below:
JavaScript
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.valueOf(arrayList.get(position).getTeamURL())));
An example of a sample url link based on your code redirecting to the specific url will be like below:
JavaScript
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.valueOf("https://namu.wiki/w/%EC%9D%B8%EB%94%94%EC%95%A0%EB%82%98%ED%8F%B4%EB%A6%AC%EC%8A%A4%20%EC%BD%9C%EC%B8%A0")));
if(intent.resolveActivity(requireContext().getPackageManager()) != null){
getActivity().startActivity(intent);
}
}
});