0

我从互联网上获得了源代码,我尝试对其进行修改,以便它可以检索存储在 sd 卡中的文件夹之一中的所有图标并将其显示在 gridview 中。
同时,一旦用户单击该图标,它将链接到另一个活动。但我做不到。代码如下所示。
提前感谢那些可以提供帮助的人。

数据库处理程序.java

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHandler extends SQLiteOpenHelper {


private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "DISPLAYAPPS_MAIN";

// Contacts table name
 private static final String TABLE_MAINAPPS = "MAINAPPS";

 // Contacts Table Columns names
 private static final String KEY_ID = "id";
 private static final String KEY_NAME = "name";
 private static final String KEY_IMAGE = "image";

 public DataBaseHandler(Context context) {
  super(context, DATABASE_NAME, null, DATABASE_VERSION);
 }

 // Creating Tables
 @Override
 public void onCreate(SQLiteDatabase db) {
  String CREATE_APPS_TABLE = "CREATE TABLE " + TABLE_MAINAPPS + "("
    + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
    + KEY_IMAGE + " BLOB" + ")";
  db.execSQL(CREATE_APPS_TABLE);
 }

 // Upgrading database
 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  // Drop older table if existed
  db.execSQL("DROP TABLE IF EXISTS " + TABLE_MAINAPPS);

  // Create tables again
  onCreate(db);
 }


// Adding new apps
 public void addApps(APPS apps) {
  SQLiteDatabase db = this.getWritableDatabase();

  ContentValues values = new ContentValues();
  values.put(KEY_NAME, apps._name); // Apps Name
  values.put(KEY_IMAGE, apps._image); // Apps image

  // Inserting Row
  db.insert(TABLE_MAINAPPS, null, values);
  db.close(); // Closing database connection
 }

 // Getting single contact
 APPS getAPPS(int id) {
  SQLiteDatabase db = this.getReadableDatabase();

  Cursor cursor = db.query(TABLE_MAINAPPS, new String[] { KEY_ID,
    KEY_NAME, KEY_IMAGE }, KEY_ID + "=?",
    new String[] { String.valueOf(id) }, null, null, null, null);
  if (cursor != null)
   cursor.moveToFirst();

  APPS apps = new APPS(Integer.parseInt(cursor.getString(0)),
    cursor.getString(1), cursor.getBlob(1));

  // return contact
  return apps;

 }

 // Getting All Contacts
 public List<APPS> getAllAPPS() {
  List<APPS> appsList = new ArrayList<APPS>();
  // Select All Query
  String selectQuery = "SELECT  * FROM MAINAPPS ORDER BY name";

  SQLiteDatabase db = this.getWritableDatabase();
  Cursor cursor = db.rawQuery(selectQuery, null);
  // looping through all rows and adding to list
  if (cursor.moveToFirst()) {
   do {
    APPS apps = new APPS();
    apps.setID(Integer.parseInt(cursor.getString(0)));
    apps.setName(cursor.getString(1));
    apps.setImage(cursor.getBlob(2));

   // String name = cursor.getString(1) +"\n"+ cursor.getBlob(2); 
    // Adding contact to list
    appsList.add(apps);
   } while (cursor.moveToNext());
   }
  // close inserting data from database
  db.close();
  // return contact list
  return appsList;

 }

 // Updating single contact
 public int updateAPPS(APPS apps) {
  SQLiteDatabase db = this.getWritableDatabase();

  ContentValues values = new ContentValues();
  values.put(KEY_NAME, apps.getName());
  values.put(KEY_IMAGE, apps.getImage());

  // updating row
  return db.update(TABLE_MAINAPPS, values, KEY_ID + " = ?",
    new String[] { String.valueOf(apps.getID()) });

 }

 // Deleting single contact
 public void deleteContact(APPS contact) {
  SQLiteDatabase db = this.getWritableDatabase();
  db.delete(TABLE_MAINAPPS, KEY_ID + " = ?",
    new String[] { String.valueOf(contact.getID()) });
  db.close();
 }

 // Getting contacts Count
 public int getContactsCount() {
  String countQuery = "SELECT  * FROM " + TABLE_MAINAPPS;
  SQLiteDatabase db = this.getReadableDatabase();
  Cursor cursor = db.rawQuery(countQuery, null);
  cursor.close();

  // return count
  return cursor.getCount();
}

GridviewAvtivity.java

import com.mkyong.android.adapter.ImageAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;

public class GridViewActivity extends Activity {

GridView gridView;

static final String[] MOBILE_OS = new String[] { "Android", "Ios",
        "Windows", "Blackberry" };

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    gridView = (GridView) findViewById(R.id.gridView1);

    gridView.setAdapter(new ImageAdapter(this, MOBILE_OS));

    gridView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            String label = ((TextView) arg1.findViewById(R.id.grid_item_label)).getText().toString();

            if (label.equals("Android")) {
                Intent AndroidActivity = new Intent(GridViewActivity.this,
                        AndroidActivity.class);
                startActivity(AndroidActivity);
            } else if (label.equals("Ios")) {
                Intent IosActivity = new Intent(GridViewActivity.this,
                        IosActivity.class);
startActivity(IosActivity);
            } else if (label.equals("Windows")) {
                Intent WindowsActivity = new Intent(GridViewActivity.this,
                        WindowsActivity.class);
startActivity(WindowsActivity);
            } else if (label.equals("Blackberry")) {
                Intent BlackberryActivity = new Intent(GridViewActivity.this,
                        BlackberryActivity.class);
startActivity(BlackberryActivity);

            }




        }
    });

}

}

ImageAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;



 public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;

public ImageAdapter(Context context, String[] mobileValues) {
    this.context = context;
    this.mobileValues = mobileValues;
}

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    if (convertView == null) {

        gridView = new View(context);

        // get layout from mobile.xml
        gridView = inflater.inflate(R.layout.mobile, null);

        // set value into textview
        TextView textView = (TextView) gridView
                .findViewById(R.id.grid_item_label);
        textView.setText(mobileValues[position]);

        // set image based on selected text
        ImageView imageView = (ImageView) gridView
                .findViewById(R.id.grid_item_image);

        String mobile = mobileValues[position];

        if (mobile.equals("Windows")) {
            imageView.setImageResource(R.drawable.windows_logo);
        } else if (mobile.equals("iOS")) {
            imageView.setImageResource(R.drawable.ios_logo);
        } else if (mobile.equals("Blackberry")) {
            imageView.setImageResource(R.drawable.blackberry_logo);
        } else {
            imageView.setImageResource(R.drawable.android_logo);
        }

    } else {
        gridView = (View) convertView;
    }

    return gridView;
}

@Override
public int getCount() {
    return mobileValues.length;
}

@Override
public Object getItem(int position) {
    return null;
}

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

}
4

1 回答 1

0

尝试替换:

public class GridViewActivity extends Activity 

为了

public class GridViewActivity extends Activity implements AdapterView.OnItemClickListener 
于 2013-07-11T15:30:07.183 回答