0

当我单击 listItem 时,我尝试调用使用当前活动数据的其他活动。但我的代码中有错误。如何解决?

第 105 行: SQLiteCursor cr=(SQLiteCursor)parent.getItemAtPosition(position);

ListAnSco.java


public class ListAnSco extends SherlockActivity{

    protected EditText searchText;
    DatabaseHelper dbhelper;
    SQLiteDatabase db;

    protected Cursor cursor;
    ListAdapterAnSco adapter;
    private static ListView liste;
    TextView ansco;
    String [] from;
    int to[];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.annee_sco_list);
        setTitle(R.string.titre_list_ansco);
        searchText = (EditText) findViewById(R.id.searchText_ansco);
        liste = (ListView) findViewById(R.id.list_ansco);
        ansco = (TextView) findViewById(R.id.label_ansco);
        try {
            LoadListAnsco();
        } catch (Exception e) {
            Toast.makeText(this, "Impossible d'afficher \n"+e.toString(), Toast.LENGTH_SHORT).show();
        }

        liste.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?>  parent, View v, int position,
                    long id) {
                try
                {

                SQLiteCursor cr=(SQLiteCursor)parent.getItemAtPosition(position);
                String ansco=cr.getString(cr.getColumnIndex(DatabaseHelper.ANSCO));
                AnSco an=new AnSco(ansco);
                an.setIdan((int)id);
                AlertDialog diag= Alerts.ShowEditDialogAnSco(ListAnSco.this,an);
                diag.setOnDismissListener(new OnDismissListener() {

                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        // TODO Auto-generated method stub
                        Toast.makeText(getApplicationContext(), R.string.notif_annuler, Toast.LENGTH_SHORT).show();
                        //((SimpleCursorAdapter)grid.getAdapter()).notifyDataSetChanged();
                        LoadListAnsco();
                    }
                });
                diag.show();
                cr.close();
                }
                catch(Exception ex)
                {
                    Alerts.CatchError(ListAnSco.this, ex.toString());
                }
                return false;
            }
        });

        liste.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?>  parent, View v, int position,
                    long id) {
                Intent intent = new Intent(getApplicationContext(),ListPeriode.class);

                SQLiteCursor cr=(SQLiteCursor)parent.getItemAtPosition(position);
                String ansco=cr.getString(cr.getColumnIndex(DatabaseHelper.ANSCO));
                AnSco an=new AnSco(ansco);
                an.setIdan((int)id);

                Toast.makeText(getApplicationContext(), "Année Scolaire: "+an.getAnsco(), Toast.LENGTH_LONG).show();
                intent.putExtra("ANSCO_ID", an.getIdan());
                intent.putExtra("ANSCO_AN", an.getAnsco());
                cr.close();
                startActivity(intent);
                finish();
            }

        });
    }

public void LoadListAnsco() {
        dbhelper = new DatabaseHelper(this);
        boolean ok = true;
        try {
            db = dbhelper.getWritableDatabase();
            adapter = new ListAdapterAnSco(this, dbhelper.getLabelsAnSco());
            liste.setAdapter(adapter);
            Log.i("ListAnsco 3", "OK");

        } catch (Exception ex) {
            ok = false;
            AlertDialog.Builder b = new AlertDialog.Builder(this);
            b.setMessage(ex.toString());
            b.show();
        } finally {
            if (ok) {

                dbhelper.close();
                db.close();
            }
        }
    }

ListAdapterAnSco.java


public class ListAdapterAnSco extends BaseAdapter {

    // Declare Variables
    Context mContext;
    LayoutInflater inflater;
    private List<AnSco> listAnsco = null;
    private ArrayList<AnSco> arraylist;

    public ListAdapterAnSco(Context context, List<AnSco> listAnsco) {
        mContext = context;
        this.listAnsco = listAnsco;
        inflater = LayoutInflater.from(mContext);
        this.arraylist = new ArrayList<AnSco>();
        this.arraylist.addAll(listAnsco);
    }

    public class ViewHolder {
        TextView an;;
    }

    @Override
    public int getCount() {
        return listAnsco.size();
    }

    @Override
    public AnSco getItem(int position) {
        return listAnsco.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.annee_sco_item, null);
            // Locate the TextViews in listview_item.xml
            holder.an = (TextView) view.findViewById(R.id.ansco_item);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // Set the results into TextViews
        holder.an.setText(listAnsco.get(position).getAnsco());
        return view;
    }

    // Filter Class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        listAnsco.clear();
        if (charText.length() == 0) {
            listAnsco.addAll(arraylist);
        } 
        else 
        {
            for (AnSco wp : arraylist) 
            {
                if (wp.getAnsco().toLowerCase(Locale.getDefault()).contains(charText)) 
                {
                    listAnsco.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }


}

AnSco.java


package com.android.moyenne.ansco;

public class AnSco {

    int idan;
    String ansco;

    public AnSco(int idan, String ansco) {
        this.idan = idan;
        this.ansco = ansco;
    }

    public AnSco(String ansco) {
        this.ansco = ansco;
    }

    public AnSco() {
        // TODO Auto-generated constructor stub
    }

    public int getIdan() {
        return idan;
    }
    public void setIdan(int idan) {
        this.idan = idan;
    }

    public String getAnsco() {
        return ansco;
    }
    public void setAnsco(String ansco) {
        this.ansco = ansco;
    }

}

Logcat 异常


10-29 10:57:56.316: E/AndroidRuntime(1554): FATAL EXCEPTION: main
10-29 10:57:56.316: E/AndroidRuntime(1554): java.lang.ClassCastException: com.android.moyenne.ansco.AnSco cannot be cast to android.database.sqlite.SQLiteCursor
10-29 10:57:56.316: E/AndroidRuntime(1554):     at com.android.moyenne.ansco.ListAnSco$3.onItemClick(ListAnSco.java:105)
10-29 10:57:56.316: E/AndroidRuntime(1554):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
10-29 10:57:56.316: E/AndroidRuntime(1554):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
10-29 10:57:56.316: E/AndroidRuntime(1554):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
10-29 10:57:56.316: E/AndroidRuntime(1554):     at android.widget.AbsListView$1.run(AbsListView.java:3463)
10-29 10:57:56.316: E/AndroidRuntime(1554):     at android.os.Handler.handleCallback(Handler.java:730)
10-29 10:57:56.316: E/AndroidRuntime(1554):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-29 10:57:56.316: E/AndroidRuntime(1554):     at android.os.Looper.loop(Looper.java:137)
10-29 10:57:56.316: E/AndroidRuntime(1554):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-29 10:57:56.316: E/AndroidRuntime(1554):     at java.lang.reflect.Method.invokeNative(Native Method)
10-29 10:57:56.316: E/AndroidRuntime(1554):     at java.lang.reflect.Method.invoke(Method.java:525)
10-29 10:57:56.316: E/AndroidRuntime(1554):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-29 10:57:56.316: E/AndroidRuntime(1554):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-29 10:57:56.316: E/AndroidRuntime(1554):     at dalvik.system.NativeStart.main(Native Method)
4

0 回答 0