OnClick项目不适用于填充到列表视图的SQLite DB

时间:2013-12-02 13:10:36

标签: android sqlite listview listview-adapter

我尝试过这项工作,但它会将我重定向到我为webview活动创建的错误页面。

该应用程序就像一个书籍应用程序,我为“添加到收藏夹”选项卡创建了数据库,当点击添加到收藏夹时,它将当前的网页浏览页面“id”和“title”添加到数据库,我我试图这样做,以便当点击书签列表视图时,它会打开URL。

警报对话框在OnLongItemClick方法中正常工作。 请查看以下代码

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class GoToFav extends Activity {
private GymsDbHelper mHelper;
private SQLiteDatabase dataBase;

private ArrayList<String> gymId = new ArrayList<String>();
private ArrayList<String> htitle = new ArrayList<String>();

private ListView gymList;
private AlertDialog.Builder build;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.favpage);
    gymList = (ListView) findViewById(R.id.gymList);
    mHelper = new GymsDbHelper(this);

    gymList.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            String name = htitle.get(position);
            if (name.equals("XXXXXX")) {
                Intent i = new Intent(GoToFav.this, WebViewActivity.class);
                i.putExtra("keyHTML",
                        "file:///android_asset/gympage1.html");
                startActivity(i);

            } else {
                Intent i = new Intent(getApplicationContext(),
                        WebViewActivity.class);
                i.putExtra("keyHTML",
                        "file:///android_asset/yerrorpage.html");
                startActivity(i);
            }
        }
    });

    // long click to delete data
    gymList.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                final int arg2, long arg3) {

            build = new AlertDialog.Builder(GoToFav.this);
            build.setTitle("Delete " + htitle.get(arg2));
            build.setMessage("Do you want to delete ?");
            build.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int which) {

                            Toast.makeText(getApplicationContext(),
                                    htitle.get(arg2) + " is deleted.",
                                    Toast.LENGTH_LONG).show();

                            dataBase.delete(
                                    GymsDbHelper.TABLE_NAME,
                                    GymsDbHelper.KEY_ID + "="
                                            + gymId.get(arg2), null);
                            displayData();
                            dialog.cancel();
                        }
                    });

            build.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int which) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = build.create();
            alert.show();

            return true;
        }
    });
}

@Override
protected void onResume() {
    displayData();
    super.onResume();
}

/**
 * displays data from SQLite
 */
private void displayData() {
    dataBase = mHelper.getWritableDatabase();
    Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
            + GymsDbHelper.TABLE_NAME, null);

    gymId.clear();
    htitle.clear();
    if (mCursor.moveToFirst()) {
        do {
            gymId.add(mCursor.getString(mCursor
                    .getColumnIndex(GymsDbHelper.KEY_ID)));
            htitle.add(mCursor.getString(mCursor
                    .getColumnIndex(GymsDbHelper.KEY_HTITLE)));

        } while (mCursor.moveToNext());
    }
    FavAdapter favadpt = new FavAdapter(GoToFav.this, gymId, htitle);
    gymList.setAdapter(favadpt);
    mCursor.close();
}

}

谢谢!

0 个答案:

没有答案