我正在制作供个人使用的电视应用程序,问题是可以在android mobile上正常工作。在android电视上,即使没有通过箭头键选择搜索视图,也不会选择gridview频道,无论鼠标如何工作。所以,请告诉我在电视上工作需要哪些额外的代码/修改。
下面我要提供代码:
public class smarttv extends AppCompatActivity {
/*
Our data object
*/
public class Spacecraft {
/*
INSTANCE FIELDS
*/
private int id;
private String name;
private String mobileurl;
private String imageURL;
private String hdurl;
private String smarttv;
/*
GETTERS AND SETTERS
*/
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getmobileurl() {
return mobileurl;
}
public void setmobileurl(String mobileurl) {
this.mobileurl = mobileurl;
}
public String gethdurl() {
return hdurl;
}
public void sethdurl(String hdurl) { this.hdurl = hdurl; }
public String getsmarttv() {
return smarttv;
}
public void setsmarttv(String smarttv) { this.smarttv = smarttv; }
public String getImageURL() {
return imageURL;
}
public void setImageURL(String imageURL) {
this.imageURL = imageURL;
}
/*
TOSTRING
*/
@Override
public String toString() {
return name;
}
}
class FilterHelper extends Filter {
ArrayList<Spacecraft> currentList;
GridViewAdapter adapter;
Context c;
public FilterHelper(ArrayList<Spacecraft> currentList, GridViewAdapter adapter,Context c) {
this.currentList = currentList;
this.adapter = adapter;
this.c=c;
}
/*
- Perform actual filtering.
*/
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults=new FilterResults();
if(constraint != null && constraint.length()>0)
{
//CHANGE TO UPPER
constraint=constraint.toString().toUpperCase();
//HOLD FILTERS WE FIND
ArrayList<Spacecraft> foundFilters=new ArrayList<>();
Spacecraft spacecraft= null;
//ITERATE CURRENT LIST
for (int i=0;i<currentList.size();i++)
{
spacecraft= currentList.get(i);
//SEARCH
if(spacecraft.getName().toUpperCase().contains(constraint) )
{
//ADD IF FOUND
foundFilters.add(spacecraft);
}
}
//SET RESULTS TO FILTER LIST
filterResults.count=foundFilters.size();
filterResults.values=foundFilters;
}else
{
//NO ITEM FOUND.LIST REMAINS INTACT
filterResults.count=currentList.size();
filterResults.values=currentList;
}
//RETURN RESULTS
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
adapter.setSpacecrafts((ArrayList<Spacecraft>) filterResults.values);
adapter.refresh();
}
}
/*
Our custom adapter class
*/
public class GridViewAdapter extends BaseAdapter implements Filterable {
Context c;
ArrayList<Spacecraft> spacecrafts;
public ArrayList<Spacecraft> currentList;
FilterHelper filterHelper;
public GridViewAdapter(Context c, ArrayList<Spacecraft> spacecrafts) {
this.c = c;
this.spacecrafts = spacecrafts;
this.currentList=spacecrafts;
}
@Override
public int getCount() {
return spacecrafts.size();
}
@Override
public Object getItem(int i) {
return spacecrafts.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view==null)
{
view= LayoutInflater.from(c).inflate(R.layout.model,viewGroup,false);
}
TextView txtName = view.findViewById(R.id.nameTextView);
ImageView spacecraftImageView = view.findViewById(R.id.spacecraftImageView);
final Spacecraft s= (Spacecraft) this.getItem(i);
txtName.setText(s.getName());
//chkTechExists.setEnabled(true);
if(s.getImageURL() != null && s.getImageURL().length()>0)
{
Picasso.get().load(s.getImageURL()).placeholder(R.drawable.placeholder).into(spacecraftImageView);
}else {
Toast.makeText(c, "Empty Image URL", Toast.LENGTH_LONG).show();
Picasso.get().load(R.drawable.placeholder).into(spacecraftImageView);
}
view.setOnClickListener(new AdapterView.OnClickListener() {
@Override
public void onClick(View view) {
if(s.getsmarttv().contains("yes")) {
final Dialog myDialog = new Dialog(c);
myDialog.setContentView(R.layout.messagescreen);
myDialog.setCancelable(true);
TextView text = (TextView) myDialog.findViewById(R.id.bidmessage);
text.setMovementMethod(ScrollingMovementMethod.getInstance());
text.setText("Choose Player Quality");
Button login = (Button) myDialog.findViewById(R.id.mobilequality);
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Toast.makeText(c, s.getmobileurl(), Toast.LENGTH_SHORT).show();
Intent i = new Intent(smarttv.this, jioplayeractivity.class);
i.setData(Uri.parse(s.getmobileurl()));
try {
startActivity(i);
} catch (ActivityNotFoundException e) {
Toast.makeText(getBaseContext(), "Your hardware Not support HLS STREAM.", Toast.LENGTH_SHORT).show();
}
myDialog.dismiss();
}
});
Button createAccount= (Button) myDialog.findViewById(R.id.smarttvquality);
createAccount.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Toast.makeText(c, s.gethdurl(), Toast.LENGTH_SHORT).show();
Intent i = new Intent(smarttv.this, jioplayeractivity.class);
i.setData(Uri.parse(s.gethdurl()));
try {
startActivity(i);
} catch (ActivityNotFoundException e) {
Toast.makeText(getBaseContext(), "localpeers: jio loading Video activity failed.", Toast.LENGTH_SHORT).show();
}
myDialog.dismiss();
}
});
myDialog.show();
} else {
// Toast.makeText(c, s.getmobileurl(), Toast.LENGTH_SHORT).show();
Intent i = new Intent(smarttv.this, PlayerActivity.class);
i.setData(Uri.parse(s.getmobileurl()));
try {
startActivity(i);
} catch (ActivityNotFoundException e) {
Toast.makeText(getBaseContext(), "Loading video in system in system.", Toast.LENGTH_SHORT).show();
}
}
}
});
return view;
}
public void setSpacecrafts(ArrayList<Spacecraft> filteredSpacecrafts)
{
this.spacecrafts=filteredSpacecrafts;
}
@Override
public Filter getFilter() {
if(filterHelper==null)
{
filterHelper=new FilterHelper(currentList,this,c);
}
return filterHelper;
}
public void refresh(){
notifyDataSetChanged();
}
}
/*
Our HTTP Client
*/
public String getCookie(String siteName,String CookieName){
String CookieValue = null;
CookieManager cookieManager = CookieManager.getInstance();
String cookies = cookieManager.getCookie(siteName);
String[] temp=cookies.split(";");
for (String ar1 : temp ){
if(ar1.contains(CookieName)){
String[] temp1=ar1.split("=");
CookieValue = temp1[1];
break;
}
}
return CookieValue;
}
public class JSONDownloader {
//SAVE/RETRIEVE URLS
private static final String JSON_DATA_URL="http://app.localpeers.com/localtvjson.php";
//INSTANCE FIELDS
private final Context c;
public JSONDownloader(Context c) {
this.c = c;
}
/*
Fetch JSON Data
*/
public ArrayList<Spacecraft> retrieve(final GridView myGridView, final ProgressBar myProgressBar)
{
final ArrayList<Spacecraft> downloadedData=new ArrayList<>();
myProgressBar.setIndeterminate(true);
myProgressBar.setVisibility(View.VISIBLE);
AndroidNetworking.get(JSON_DATA_URL)
.setPriority(Priority.HIGH)
.addHeaders("User-Agent", "LocalTV")
.addHeaders("Cookie","LPUSER="+getCookie("https://localpeers.com","LPUSER"))
.build()
.getAsJSONArray(new JSONArrayRequestListener() {
@Override
public void onResponse(JSONArray response) {
JSONObject jo;
Spacecraft s;
try
{
for(int i=0;i<response.length();i++)
{
jo=response.getJSONObject(i);
int id=jo.getInt("id");
String name=jo.getString("name");
String smarttv=jo.getString("smarttv");
String mobileurl=jo.getString("mobileurl");
String hdurl = jo.getString("hdurl");
String imageURL=jo.getString("imageurl");
s=new Spacecraft();
s.setId(id);
s.setName(name);
s.setsmarttv(smarttv);
s.setmobileurl(mobileurl);
s.sethdurl(hdurl);
s.setImageURL(imageURL);
downloadedData.add(s);
}
myProgressBar.setVisibility(View.GONE);
}catch (JSONException e)
{
myProgressBar.setVisibility(View.GONE);
Toast.makeText(c, "DATAA CORRUPTED CONTACT LOCALPEERS SUPPORT. ", Toast.LENGTH_LONG).show();
}
}
//ERROR
@Override
public void onError(ANError anError) {
anError.printStackTrace();
myProgressBar.setVisibility(View.GONE);
Toast.makeText(c, "UNSUCCESSFUL CHECK INTERNET", Toast.LENGTH_LONG).show();
}
});
return downloadedData;
}
}
ArrayList<Spacecraft> spacecrafts = new ArrayList<>();
GridView myGridView;
GridViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smarttv);
myGridView= (GridView) findViewById(R.id.myGridView);
final ProgressBar myProgressBar= findViewById(R.id.myProgressBar);
SearchView mySearchView=findViewById(R.id.mySearchView);
mySearchView.setIconified(true);
mySearchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
mySearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
adapter.getFilter().filter(s);
return false;
}
@Override
public boolean onQueryTextChange(String query) {
adapter.getFilter().filter(query);
return false;
}
});
spacecrafts=new JSONDownloader(smarttv.this).retrieve(myGridView,myProgressBar);
adapter=new GridViewAdapter(this,spacecrafts);
myGridView.setAdapter(adapter);
}
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".smarttv">
<SearchView
android:id="@+id/mySearchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search Channels"
android:padding="5dp"
android:iconifiedByDefault="true"
android:queryHint="Search Channels"
android:textAlignment="center" />
<ProgressBar
android:id="@+id/myProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:indeterminateBehavior="cycle"
android:visibility="gone" />
<GridView
android:id="@+id/myGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:columnWidth="100dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
model.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/grid_color_selector"
android:orientation="vertical"
android:focusable="true"
android:padding="5dp">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/spacecraftImageView"
android:scaleType="fitCenter"
android:src="@drawable/placeholder" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Spacecraft Name"
android:id="@+id/nameTextView"
android:layout_marginTop="5dp"
android:textColor="@color/colorAccent"
android:ellipsize="marquee"
android:gravity="center"
android:maxLines="2"
android:textSize="12sp"/>
</LinearLayout>