Java 无法解析符号水平列表视图
我试图在我的活动中声明一个水平listview,如下所示Java 无法解析符号水平列表视图,java,android,android-layout,listview,android-studio,Java,Android,Android Layout,Listview,Android Studio,我试图在我的活动中声明一个水平listview,如下所示 private LinearLayout lay; HorizontalListView listview; 然而,HorizontalListView以红色突出显示,我得到错误“无法解析符号HorizontalListView” Java文件 package com.xera.deviceinsight.home; import java.text.DecimalFormat; import java.util.Arrays
private LinearLayout lay;
HorizontalListView listview;
然而,HorizontalListView以红色突出显示,我得到错误“无法解析符号HorizontalListView”
Java文件
package com.xera.deviceinsight.home;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class SensorCharts extends Activity{
private LinearLayout lay;
HorizontalListView listview;
private double highest;
private int[] grossheight;
private int[] netheight;
private Double[] grossSal= {15000.0,15000.0,15000.25,15000.1,
15000.0,15000.0,15000.0,15000.0,
15000.25,15000.1,15000.0,15000.0};
private Double[] netSal = {12000.0,13000.0,14000.25,10000.1,
10000.0,9000.0,12000.0,13000.0,
14000.25,10000.1,10000.0,9000.0};
private String[] datelabel = {"Jan 12","Feb 12","Mar 12",
"Apr 12","May 12","Jun 12",
"Jul 12","Aug 12","Sep 12",
"Oct 12","Nov 12","Dec 12"};
public void onCreate(Bundle savedInstance)
{
super.onCreate(savedInstance);
setContentView(R.layout.main);
lay = (LinearLayout)findViewById(R.id.linearlay);
listview = (HorizontalListView) findViewById(R.id.listview);
List<Double> b = Arrays.asList(grossSal);
highest = (Collections.max(b));
netheight = new int[netSal.length];
grossheight= new int[grossSal.length];
//updateSizeInfo();
}
public class bsAdapter extends BaseAdapter
{
Activity cntx;
String[] array;
public bsAdapter(Activity context,String[] arr)
{
// TODO Auto-generated constructor stub
this.cntx=context;
this.array = arr;
}
public int getCount()
{
// TODO Auto-generated method stub
return array.length;
}
public Object getItem(int position)
{
// TODO Auto-generated method stub
return array[position];
}
public long getItemId(int position)
{
// TODO Auto-generated method stub
return array.length;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row=null;
LayoutInflater inflater=cntx.getLayoutInflater();
row=inflater.inflate(R.layout.list, null);
DecimalFormat df = new DecimalFormat("#.##");
final TextView title = (TextView)row.findViewById(R.id.title);
TextView tvcol1 = (TextView)row.findViewById(R.id.colortext01);
TextView tvcol2 = (TextView)row.findViewById(R.id.colortext02);
TextView gt = (TextView)row.findViewById(R.id.text01);
TextView nt = (TextView)row.findViewById(R.id.text02);
tvcol1.setHeight(grossheight[position]);
tvcol2.setHeight(netheight[position]);
title.setText(datelabel[position]);
gt.setText(df.format(grossSal[position]/1000)+" k");
nt.setText(df.format(netSal[position]/1000)+" k");
tvcol1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(SensorCharts.this, "Month/Year: "+title.getText().toString()+"\nGross Sal: "+grossSal[position], Toast.LENGTH_SHORT).show();
}
});
tvcol2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(SensorCharts.this, "Month/Year: "+title.getText().toString()+"\nNet Sal: "+netSal[position], Toast.LENGTH_SHORT).show();
}
});
return row;
}
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
updateSizeInfo();
}
private void updateSizeInfo() {
/** This is onWindowFocusChanged method is used to allow the chart to
* update the chart according to the orientation.
* Here h is the integer value which can be updated with the orientation
*/
int h;
if(getResources().getConfiguration().orientation == 1)
{
h = (int) (lay.getHeight()*0.5);
if(h == 0)
{
h = 200;
}
}
else
{
h = (int) (lay.getHeight()*0.3);
if(h == 0)
{
h = 130;
}
}
for(int i=0;i<netSal.length;i++)
{
netheight[i] = (int)((h*netSal[i])/highest);
grossheight[i] = (int)((h*grossSal[i])/highest);
System.out.println("net width[i] "+netheight[i]+"gross width[i] "+grossheight[i]);
}
listview.setAdapter(new bsAdapter(this,datelabel));
}
}
package com.xera.deviceinsight.home;
导入java.text.DecimalFormat;
导入java.util.array;
导入java.util.Collections;
导入java.util.List;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.LinearLayout;
导入android.widget.TextView;
导入android.widget.Toast;
公共课堂活动{
私人线路布局;
水平视图列表视图;
私人双最高;
格罗斯赫特私人酒店;
私人互联网【】净高;
私人双[]格罗萨尔={15000.015000.015000.2515000.1,
15000.0,15000.0,15000.0,15000.0,
15000.25,15000.1,15000.0,15000.0};
私有双[]netSal={12000.013000.014000.2510000.1,
10000.0,9000.0,12000.0,13000.0,
14000.25,10000.1,10000.0,9000.0};
私有字符串[]日期标签={“1月12日”、“2月12日”、“3月12日”,
“4月12日”、“5月12日”、“6月12日”,
“7月12日”、“8月12日”、“9月12日”,
“10月12日”、“11月12日”、“12月12日”};
创建时的公共void(Bundle savedInstance)
{
super.onCreate(savedInstance);
setContentView(R.layout.main);
布局=(线性布局)findViewById(R.id.linearlay);
listview=(HorizontalListView)findViewById(R.id.listview);
列表b=Arrays.asList(Grossal);
最高=(Collections.max(b));
净高=新整数[netSal.length];
grossheight=新整数[总长度];
//updateSizeInfo();
}
公共类bsAdapter扩展了BaseAdapter
{
活性碳纳米管;
字符串[]数组;
公共bsAdapter(活动上下文,字符串[]arr)
{
//TODO自动生成的构造函数存根
this.cntx=上下文;
this.array=arr;
}
public int getCount()
{
//TODO自动生成的方法存根
返回数组长度;
}
公共对象getItem(int位置)
{
//TODO自动生成的方法存根
返回数组[位置];
}
公共长getItemId(int位置)
{
//TODO自动生成的方法存根
返回数组长度;
}
公共视图getView(最终整数位置、视图转换视图、视图组父视图)
{
视图行=空;
LayoutInflater充气机=cntx.getLayoutInflater();
row=充气机充气(R.layout.list,空);
DecimalFormat df=新的DecimalFormat(“#.###”);
final TextView title=(TextView)row.findViewById(R.id.title);
TextView tvcol1=(TextView)row.findViewById(R.id.colortext01);
TextView tvcol2=(TextView)row.findViewById(R.id.colortext02);
TextView gt=(TextView)row.findViewById(R.id.text01);
TextView nt=(TextView)row.findViewById(R.id.text02);
tvcol1.设置高度(总高度[位置]);
tvcol2.设置高度(净高[位置]);
title.setText(日期标签[位置]);
gt.setText(df.format(grossal[position]/1000)+“k”);
setText(df.format(netSal[position]/1000)+“k”);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
Toast.makeText(SensorCharts.this,“月/年:”+title.getText().toString()+“\nRoss Sal:”+grossal[position],Toast.LENGTH_SHORT.show();
}
});
setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
Toast.makeText(SensorCharts.this,“月/年:”+title.getText().toString()+“\nNet Sal:”+netSal[position],Toast.LENGTH\u SHORT.show();
}
});
返回行;
}
}
@凌驾
WindowFocusChanged上的公共无效(布尔hasFocus){
//TODO自动生成的方法存根
super.onWindowFocusChanged(hasFocus);
updateSizeInfo();
}
私有void updateSizeInfo(){
/**这是一个onWindowFocusChanged方法,用于允许图表
*根据方向更新图表。
*这里h是整数值,可以用方向更新
*/
int-h;
if(getResources().getConfiguration().orientation==1)
{
h=(int)(lay.getHeight()*0.5);
如果(h==0)
{
h=200;
}
}
其他的
{
h=(int)(lay.getHeight()*0.3);
如果(h==0)
{
h=130;
}
}
对于(inti=0;i请尝试阅读指定如何使用HorizontalListView的文章
在XML中声明HorizontalListView并给它一个id。然后可以在java功能中使用元素
编辑:
您有一个关于如何轻松实现HorizontalListView的教程。如果您想要更自定义的版本,请阅读
还有第三个选项,即,您可以在其中实现HorizontalListView的所有方法并使用它们(如mRect、setX、setY、onClickListener…).你想使用库吗?@Zidane你能不能也添加你的java文件?Thanks@Zidane我已经阅读了GitHub Meetme文档,今天,2016年10月,Android Studio不支持旧的HorizontalListView版本,因为他们已经很久没有更新了。所以…我编辑了我的帖子,你可以找到两个实现您自己的HorizontalListView的方法。我尝试使用他们允许的URI导入库,但不起作用,所以我给您的第二个选择是使用HorizontalListView的独特方法。祝您有一天愉快,齐达内。非常感谢Carlos,让我看看
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:id="@+id/linearlay">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:gravity="center"
android:layout_gravity="center"
android:textColor="#000"
android:text="Bar Chart with out any jar"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:background="#ddd"
android:orientation="horizontal">
</RelativeLayout>
<com.xera.deviceinsight.home.HorizontalListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ddd"
/>
</LinearLayout>