2016-11-01 67 views
-3

我想在我的活動申報水平的ListView這樣不能解析符號Horizo​​ntalListView

private LinearLayout lay; 
    HorizontalListView listview; 

但是Horizo​​ntalListView以紅色突出顯示,並出現錯誤「無法解析符號Horizo​​ntalListView」

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)); 
    } 
} 

這是我的xml我的一部分,我有水平的ListView

<?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> 
+1

你想使用庫嗎? – vidulaJ

回答

0

嘗試閱讀this指定如何使用Horizo​​ntalListView的文章。

在你的XML中聲明你的Horizo​​ntalListView並給它一個id。然後你可以在你的java功能中使用你的元素。

編輯:

Here你有一個關於如何輕鬆實現Horizo​​ntalListView教程。如果您想要更加定製的版本,請閱讀this

還有第三個選項,它是here,您可以在其中實現Horizo​​ntalListView的所有方法並使用它們(如mRect,setX,setY,onClickListener ...)。

+0

卡洛斯請檢查我的編輯 – Zidane

+0

@Zidane你能不能請添加你的Java文件呢?謝謝 –

+0

ok Carlos'我添加了它 – Zidane