1

我想根据 db 值创建饼图,在调整滑块时我必须更改饼图,我创建了饼图并获得了滑块可调值。我的代码是,

   PieChartDefalut.java

   package com.exae.url;

   import android.app.Activity;
   import android.database.Cursor;
   import android.database.sqlite.SQLiteDatabase;
   import android.database.sqlite.SQLiteException;
   import android.os.Bundle;
   import android.util.Log;
   import android.view.View;
   import android.webkit.WebView;
   import android.widget.AdapterView;
   import android.widget.AdapterView.OnItemSelectedListener;
   import android.widget.ArrayAdapter;
   import android.widget.Button;
   import android.widget.CheckBox;
   import android.widget.ImageView;
   import android.widget.LinearLayout;
   import android.widget.SeekBar;
   import android.widget.Spinner;
   import android.widget.TextView;
   import android.widget.Toast;


   public class PieChartDefalut extends Activity implements SeekBar.OnSeekBarChangeListener, OnItemSelectedListener
  {
private final String SAMPLE_DB_NAME = "chartdb";
private final String SAMPLE_TABLE_NAME = "piechart3d";
  SQLiteDatabase sampleDB = null;
  String year;
    double value;
    LinearLayout ll,ll1;
    Spinner my_spinner;

    ImageView pieChart;
    View my_vew;
    int count=0;
    String value_arr[]={};
    static String[] my_year={};
    static int[] my_values={};
    static int my_values_length=0;
    static int my_year_length=0;
    String mUrl;
    String label="";
private WebView googleChartView;
static String urlGoogleChart = "http://chart.apis.google.com/chart?cht=p3&chs=300x150&";

 static String[] urlyear;
 static int[] urlvalues;
 SeekBar mSeekBar;
   TextView mProgressText;
  Button bt;
   int progress1=0;
   int progress2=0;
   Cursor c,c1;
   int b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.url);
  mSeekBar = (SeekBar)findViewById(R.id.seek);
  mSeekBar.setOnSeekBarChangeListener(this);
  mProgressText = (TextView)findViewById(R.id.progress);
 bt = (Button)findViewById(R.id.ok);
  ll=(LinearLayout)findViewById(R.id.my);
  ll1=(LinearLayout)findViewById(R.id.my_linear);
  my_spinner=(Spinner)findViewById(R.id.planets_spinner);

try {
    sampleDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);

    sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
            SAMPLE_TABLE_NAME +
            " (Year VARCHAR(5),Value DOUBLE(5));"); 
    sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('2001',5);");
    sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('2002',25);");
    sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('2003',15);");
    sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('2004',25);");
    sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('2005',25);");
     c = sampleDB.rawQuery("SELECT Year,Value FROM " + SAMPLE_TABLE_NAME , null);

     final int Column1 = c.getColumnIndex("Year");
    final int Column2 = c.getColumnIndex("Value");
    if (c != null ) {
        if  (c.moveToFirst()) {
            do {
                 year = c.getString(Column1);
                 value = c.getDouble(Column2);


                 System.out.println(year);
                 System.out.println(value);

            }while (c.moveToNext());

        } 
    }

    int columncount=c.getCount();

    System.out.println("count------>"+columncount);


         my_year = new String[columncount];

        if (c.moveToFirst())
        {                       
            for (int i = 0; i < columncount; i++)
            {
                my_year[i] = c.getString(Column1);
                System.out.println("database value-----"+i);
                System.out.println(my_year[i]);
                c.moveToNext();
            }           
        }



         my_values = new int[columncount];

        if (c.moveToFirst())
        {                       
            for (int j = 0; j < columncount; j++)
            {
                my_values[j] = c.getInt(Column2);
                System.out.println("database value-----"+j);
                System.out.println(my_values[j]);
                c.moveToNext();
            }           
        }


        my_year_length=my_year.length;
        System.out.println("string length----->"+my_year_length);

        for(int k=0;k<my_year_length;k++)
        {
            System.out.println("array stored values----------->"+my_year[k]);
        }


       my_values_length=my_values.length;
        System.out.println("string length----->"+my_values_length);

        for(int k1=0;k1<my_values_length;k1++)
        {
            System.out.println("array stored values----------->"+my_values[k1]);
        }


googleChartView = (WebView)findViewById(R.id.webView1);


StringBuffer urlyear = new StringBuffer();

for(int i=0;i<my_year_length;i++)
{
if(i == (my_year_length - 1 ))
urlyear.append(my_year[i]);
else
urlyear.append(my_year[i]+"|");
}

StringBuffer urlvalues = new StringBuffer();

for(int j=0;j<my_values_length;j++) 
{
if(j == (my_values_length - 1 ))
urlvalues.append(my_values[j]);
else
urlvalues.append(my_values[j]+",");
}


mUrl = urlGoogleChart+"chl="+urlyear+"&chd=t:"+urlvalues;
googleChartView.loadUrl(mUrl);  



 ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,my_year);

 dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);

 my_spinner.setAdapter(dataAdapter);

 my_spinner.setOnItemSelectedListener(this);
 System.out.println("seek bar value----->"+progress1);

 bt.setOnClickListener(new View.OnClickListener() 
 {
     public void onClick(View v) 
    {
         if (c != null ) 
         {
                if  (c.moveToFirst()) 
                {
                    do 
                    {                       
                         year = c.getString(Column1);

                         System.out.println(year);
                         if(label.equalsIgnoreCase(year))
                         {
                             Log.i("","values compared....");
                             System.out.println("Lable.."+label);
                             System.out.println("year.."+year);
                             b=1;

                            }

                    }while (c.moveToNext());

                } 
            }
    }

});

} 
catch (SQLiteException se )
{
    Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally
{
    if (sampleDB != null) 
        sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
        sampleDB.close();
}
}


public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch)
{
    progress1=progress;
       mProgressText.setText(progress + " " +getString(R.string.seekbar_from_touch) + "=" + fromTouch);
   }

   public void onStartTrackingTouch(SeekBar seekBar)
   {

   }

   public void onStopTrackingTouch(SeekBar seekBar)
   {

       progress2=progress1;
       System.out.println("seek bar value---->"+progress2);
      // upDate();
       sampleDB.execSQL("UPDATE "+SAMPLE_TABLE_NAME+ "SET Value= "+ progress2+ "WHERE Year="+label,null);



   }


public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {

     label = arg0.getItemAtPosition(arg2).toString();
    System.out.println("selected values--->"+label);


 //  Toast.makeText(arg0.getContext(), "You selected: " + label,Toast.LENGTH_LONG).show();
}


public void onNothingSelected(AdapterView<?> arg0) 
{


}
public void upDate()
{
    if(b==1&&progress2>0)
     {
      b=0;

      sampleDB.execSQL("UPDATE "+SAMPLE_TABLE_NAME+ "SET Value= "+ progress2+ "WHERE Year="+label,null);
        Log.i("","database updated");
     }
}


}

现在我的问题是我想从微调器和可调节滑块值中选择年份来更新数据库。否则,我必须根据特定选定年份的可调整值更改饼图。它显示了一些错误。

     09-24 13:06:14.245: ERROR/AndroidRuntime(1907): FATAL EXCEPTION: main
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907): java.lang.IllegalArgumentException: Empty bindArgs
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1793)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at com.exae.url.PieChartDefalut.onStopTrackingTouch(PieChartDefalut.java:283)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at android.widget.SeekBar.onStopTrackingTouch(SeekBar.java:115)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:302)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at android.view.View.dispatchTouchEvent(View.java:3885)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at android.os.Handler.dispatchMessage(Handler.java:99)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at android.os.Looper.loop(Looper.java:123)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at android.app.ActivityThread.main(ActivityThread.java:3683)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at java.lang.reflect.Method.invokeNative(Native Method)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at java.lang.reflect.Method.invoke(Method.java:507)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
     09-24 13:06:14.245: ERROR/AndroidRuntime(1907):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

我该如何解决这个问题?有谁能够帮我?提前致谢。

4

1 回答 1

1

错误是抱怨第二个参数execSQL(String sql, Object[] bindArgs)为空。

public void onStopTrackingTouch(SeekBar seekBar) {
    progress2=progress1;
    System.out.println("seek bar value---->"+progress2);
    sampleDB.execSQL("UPDATE "+SAMPLE_TABLE_NAME+ "SET Value= "+ progress2+ "WHERE Year="+label, null);
}

你可以execSQL(String sql)改用。

public void onStopTrackingTouch(SeekBar seekBar) {
    progress2=progress1;
    System.out.println("seek bar value---->"+progress2);
    sampleDB.execSQL("UPDATE "+SAMPLE_TABLE_NAME+ "SET Value= "+ progress2+ "WHERE Year="+label);
}
于 2012-09-24T08:23:07.297 回答