آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

کمک در مورد gridview با دیتا بیس

ayhan  11 سال پیش  11 سال پیش
0 0

با سلام دوستان من سورسها رو میزارم میتونین کمکم کنین تو دو قسمت به ارورر logcatخوردم

activity_main.xml

 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="3" >
        
        
    </GridView>
	
</TableLayout>

showimage.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >
 
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="50px"
        android:layout_height="50px"
        android:layout_marginRight="10px" >
    </ImageView>
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5px"
        android:textSize="15px" >
    </TextView>
 
</LinearLayout>


myDBClass.java

 package com.myapp;


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

public class myDBClass extends SQLiteOpenHelper {
	

    // Database Version
    private static final int DATABASE_VERSION = 1;
 
    // Database Name
    private static final String DATABASE_NAME = "mydatabase";
 
    // Table Name
    private static final String TABLE_GALLERY = "gallery";

	public myDBClass(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		// Create Table Name
	    db.execSQL("CREATE TABLE " + TABLE_GALLERY + 
		          "(GalleryID INTEGER PRIMARY KEY AUTOINCREMENT," +
		          " Name TEXT(100)," +
		          " Path TEXT(100));");
	   
	    Log.d("CREATE TABLE","Create Table Successfully.");
	}	
	
	
	// Insert Data
	public long InsertData(String strGalleryID, String strName, String strPath) {
		// TODO Auto-generated method stub
		
		 try {
			SQLiteDatabase db;
     		db = this.getWritableDatabase(); // Write Data
     		
     		/**
     		 *  for API 11 and above
			SQLiteStatement insertCmd;
			String strSQL = "INSERT INTO " + TABLE_GALLERY
					+ "(GalleryID,Name,Path) VALUES (?,?,?)";
			
			insertCmd = db.compileStatement(strSQL);
			insertCmd.bindString(1, strGalleryID);
			insertCmd.bindString(2, strName);
			insertCmd.bindString(3, strPath);
			return insertCmd.executeInsert();
			*/
			
     	   ContentValues Val = new ContentValues();
     	   Val.put("GalleryID", strGalleryID); 
     	   Val.put("Name", strName);
     	   Val.put("Path", strPath);
    
			long rows = db.insert(TABLE_GALLERY, null, Val);

			db.close();
			return rows; // return rows inserted.
           
		 } catch (Exception e) {
		    return -1;
		 }

	}
	
	// Select All Data
	public String[][] SelectAllData() {
		// TODO Auto-generated method stub
		
		 try {
			String arrData[][] = null;	
			 SQLiteDatabase db;
			 db = this.getReadableDatabase(); // Read Data
				
			 String strSQL = "SELECT  * FROM " + TABLE_GALLERY;
			 Cursor cursor = db.rawQuery(strSQL, null);
			 
			 	if(cursor != null)
			 	{
					if (cursor.moveToFirst()) {
						arrData = new String[cursor.getCount()][cursor.getColumnCount()];
						/***
						 *  [x][0] = GalleryID
						 *  [x][1] = Name
						 *  [x][2] = Path
						 */
						int i= 0;
						do {				
							arrData[i][0] = cursor.getString(0);
							arrData[i][1] = cursor.getString(1);
							arrData[i][2] = cursor.getString(2);
							i++;
						} while (cursor.moveToNext());						

					}
			 	}
			 	cursor.close();
				
				return arrData;
				
		 } catch (Exception e) {
		    return null;
		 }

	}
	
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_GALLERY);
        
        // Re Create on method  onCreate
        onCreate(db);
	}

}

 

MainActivity.java

 package com.myapp;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class MainActivity extends Activity {
	  
    //List <String> ImageList;
    String arrData[][];
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
		// get Data from SQLite
		final myDBClass myDb = new myDBClass(this);
		/*
		 * for insert statement
		myDb.InsertData("1","Picture 1", "pic_a.png");
		myDb.InsertData("2","Picture 2", "pic_b.png");
		myDb.InsertData("3","Picture 3", "pic_c.png");
		myDb.InsertData("4","Picture 4", "pic_d.png");
		myDb.InsertData("5","Picture 5", "pic_e.png");
		myDb.InsertData("6","Picture 6", "pic_f.png");
		myDb.InsertData("7","Picture 7", "pic_g.png");
		myDb.InsertData("8","Picture 8", "pic_h.png");
		myDb.InsertData("9","Picture 9", "pic_i.png");
		*/
		arrData = myDb.SelectAllData();  
		/***
		 *  [x][0] = GalleryID
		 *  [x][1] = Name
		 *  [x][2] = Path
		 */
		
        // gridView1
        final GridView gView1 = (GridView)findViewById(R.id.gridView1); 
        	
        gView1.setAdapter(new ImageAdapter(this,arrData));
        
        // OnClick
        gView1.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
				int position, long id) {
				
				   Toast.makeText(getApplicationContext(),
					"Your selected : " + arrData[position][1].toString(), Toast.LENGTH_SHORT).show();
		    	
			}
		});
      
    }

    public class ImageAdapter extends BaseAdapter 
    {
        private Context context;
        private String[][] lis;
        
        public ImageAdapter(Context c, String[][] li) 
        {
        	// TODO Auto-generated method stub
            context = c;
            lis = li;
        }
 
        public int getCount() {
        	// TODO Auto-generated method stub
            return lis.length;
        }
 
        public Object getItem(int position) {
        	// TODO Auto-generated method stub
            return position;
        }
 
        public long getItemId(int position) {
        	// TODO Auto-generated method stub
            return position;
        }
 
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		 
				if (convertView == null) {
					convertView = inflater.inflate(R.layout.showimage, null);
				}
		 
				TextView textView = (TextView) convertView.findViewById(R.id.textView1);
				String strPath = "/mnt/sdcard/picture/"+lis[position][2].toString();
				
				textView.setText(lis[position][1].toString());
				
				// Image Resource
				ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
				Bitmap bm = BitmapFactory.decodeFile(strPath);
				imageView.setImageBitmap(bm);

		 
				return convertView;
				
		}
    } 
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
0 0
ارورهای logcat: یکی به mainactivity: gView1.setAdapter(new ImageAdapter(this,arrData)); یکی هم به خط: return lis.length; (11 سال پیش)
0 0
لااقل یکی امتحان کنه یه کپی پیست هست.نمیدونم من تو این سایت هر سوالی میپرسم اصلا جواب داده نمیشه.کاری هم نکردم که مدیرای سایت ناراحت بشن. :( (11 سال پیش)
 برای این سوال پاسخی وجود ندارد.

پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .