2011-04-09 4 views
0

SQLite DB에서 다음 활동으로 데이터를 전달하는 데 문제가 있습니다. 여기 새로운 활동에 SQLite를 전달하지 않음

여기

public class TimsFavs extends ListActivity implements OnItemClickListener { 

    /** Called when the activity is first created. */ 
    ArrayList<String> results = new ArrayList<String>(); 

    String col0,col1,col2,col3,col4,col5,col6,col7; 

    // Database results 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.timsfavs); 
     DBAdapter db = new DBAdapter(this); 

     //---get all Locations--- 
     db.open(); 

     Cursor c = db.getAllLocation(); 

     if (c.moveToFirst()) 
     { 
      do { 

       col0 = c.getString(c.getColumnIndex(DBAdapter.KEY_ROWID)); 
       col1 = c.getString(1); 
       col2 = c.getString(2); 
       col3 = c.getString(3); 
       col4 = c.getString(4); 
       col5 = c.getString(5); 
       col6 = c.getString(6); 
       col7 = c.getString(7); 

       results.add(col2); 

      } while (c.moveToNext()); 
     } 
     db.close(); 

     setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results)); 
     ListView lv; 
     lv = getListView(); 
     lv.setTextFilterEnabled(true); 
     lv.setBackgroundColor(Color.rgb(83, 05, 14)); 
     lv.setOnItemClickListener((OnItemClickListener) this); 

    } 

    public void onItemClick(AdapterView<?> TimsFavs, View view, int position, long id) { 

     Intent i = new Intent(getApplicationContext(), TimsFavsMore.class); 

     i.putExtra("ct_id_pass", col0); 
     i.putExtra("ct_storeid_pass", col1); 
     i.putExtra("ct_address_pass", results.get(position)); 
     i.putExtra("ct_city_pass", col3); 
     i.putExtra("ct_province_pass", col4); 
     i.putExtra("ct_country_pass", col5); 
     i.putExtra("ct_pcode_pass", col6); 
     i.putExtra("ct_phnum_pass", col7); 

     startActivity(i); 
     finish(); 

    } 
} 

public class TimsFavsMore extends Activity { 
    DBAdapter db = new DBAdapter(this); 

    String rowId; 

    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.timsfavsmore); 

      Intent i = getIntent(); 
      Bundle b = i.getExtras(); 

      final String rowId = b.getString("ct_id_pass"); 
      final String phnum = b.getString("ct_phnum_pass"); 
      final String storeid = b.getString("ct_storeid_pass"); 
      final String address = b.getString("ct_address_pass"); 
      final String city = b.getString("ct_city_pass"); 
      final String province = b.getString("ct_province_pass"); 
      final String country = b.getString("ct_country_pass"); 
      final String pcode = b.getString("ct_pcode_pass"); 

      TextView title = (TextView) findViewById(R.id.textview); 
      title.setText(Html.fromHtml("<br>Row ID: " + rowId + "<br><b><u>Location Address:</u></b><br><br>Store #" + storeid + "<br><br>" + address + "<br>" + city + ", " + province + "<br>" + country +"<br>" + pcode +"<br><br><b><u>Contact Info:</b></u><br><br>" + phnum +"<br>")); 

    // open to Nav 

    final Button button1 = (Button) findViewById(R.id.gohere); 
    button1.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      String uri = "google.navigation:q=Tim%20Hortons,%20" + address + ",%20" + city + ",%20" + province + ",%20" + pcode + ""; 
      Intent i2 = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); 
      startActivity(i2); 

     } 
    }); 

    // open to maps 

    final Button button2 = (Button) findViewById(R.id.showmap); 
    button2.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      String uri2 = "geo:0,0?q=Tim%20Hortons,%20" + address + ",%20" + city + ",%20" + province + ",%20" + pcode + ""; 
      Intent i3 = new Intent(Intent.ACTION_VIEW, Uri.parse(uri2)); 
      startActivity(i3); 

     } 
    }); 

    // Add to My Timmies List 

    final Button button3 = (Button) findViewById(R.id.removefav); 
    button3.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      //---add 2 SQLite--- 

      db.open(); 

      if (db.deleteLocation(rowId)) 
       Toast.makeText(getApplicationContext(), " Delete successful.", 
        Toast.LENGTH_LONG).show(); 
      else 
       Toast.makeText(getApplicationContext(), "Delete failed.", 
        Toast.LENGTH_LONG).show();    
      db.close(); 

      Intent i = new Intent(getApplicationContext(), MyTimmies.class); 
      startActivity(i); 
      finish(); 

     } 
    }); 


    final Button button4 = (Button) findViewById(R.id.favsdone); 
    button4.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

     Intent i4 = new Intent(getApplicationContext(), MyTimmies.class); 
      startActivity(i4); 
      finish(); 

     } 
    }); 
    } 
} 
+0

col2가 onItemClick 메서드에서 범위를 벗어났습니다. onCreate의 do 루프 안에 String으로 선언했습니다. –

답변

0

편집 TimsFavsMore.java 내 다른 2 개 파일

DBAdapter.java

public class DBAdapter 
{ 
    public static final String KEY_ROWID = "_id"; 
    public static final String KEY_PHONE = "phone"; 
    public static final String KEY_STNUMBER = "stnumber"; 
    public static final String KEY_ADDRESS = "address"; 
    public static final String KEY_CITY = "city"; 
    public static final String KEY_PROVINCE = "province"; 
    public static final String KEY_COUNTRY = "country"; 
    public static final String KEY_PCODE = "pcode";  
    private static final String TAG = "DBAdapter"; 

    private static final String DATABASE_NAME = "timsfavs"; 
    private static final String DATABASE_TABLE = "location"; 
    private static final int DATABASE_VERSION = 1; 

    private static final String DATABASE_CREATE = 
     "create table location (_id integer primary key autoincrement, " 
     + "phone text not null, stnumber text not null, " 
     + "address text not null, city text not null, " 
     + "province text not null, country text not null, " 
     + "pcode text not null);"; 

    private final Context context; 

    private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 

    public DBAdapter(Context ctx) 
    { 
     this.context = ctx; 
     DBHelper = new DatabaseHelper(context); 
    } 

    private static class DatabaseHelper extends SQLiteOpenHelper 
    { 
     DatabaseHelper(Context context) 
     { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) 
     { 
      db.execSQL(DATABASE_CREATE); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, 
     int newVersion) 
     { 
      Log.w(TAG, "Upgrading database from version " + oldVersion 
        + " to " 
        + newVersion + ", which will destroy all old data"); 
      db.execSQL("DROP TABLE IF EXISTS location"); 
      onCreate(db); 
     } 
    }  

    //---opens the database--- 
    public DBAdapter open() throws SQLException 
    { 
     db = DBHelper.getWritableDatabase(); 
     return this; 
    } 

    //---closes the database---  
    public void close() 
    { 
     DBHelper.close(); 
    } 

    //---insert a title into the database--- 
    public long insertLocation(String phone, String stnumber, String address, String city, String province, String country, String pcode) 
    { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_PHONE, phone); 
     initialValues.put(KEY_STNUMBER, stnumber); 
     initialValues.put(KEY_ADDRESS, address); 
     initialValues.put(KEY_CITY, city); 
     initialValues.put(KEY_PROVINCE, province); 
     initialValues.put(KEY_COUNTRY, country); 
     initialValues.put(KEY_PCODE, pcode); 
     return db.insert(DATABASE_TABLE, null, initialValues); 
    } 

    //---deletes a particular title--- 
    public boolean deleteLocation(String rowId) 
    { 
     return db.delete(DATABASE_TABLE, KEY_ROWID + 
       "=" + rowId, null) > 0; 
    } 

    //---retrieves all the titles--- 
    public Cursor getAllLocation() 
    { 

     return db.query(DATABASE_TABLE, new String[] { 
       KEY_ROWID, 
       KEY_PHONE, 
       KEY_STNUMBER, 
       KEY_ADDRESS, 
       KEY_CITY, 
       KEY_PROVINCE, 
       KEY_COUNTRY, 
       KEY_PCODE}, 
       null, 
       null, 
       null, 
       null, 
       null); 

    } 

    //---retrieves a particular title--- 
    public Cursor getLocation(long rowId) throws SQLException 
    { 
     Cursor mCursor = 
       db.query(true, DATABASE_TABLE, new String[] { 
         KEY_ROWID, 
         KEY_PHONE, 
         KEY_STNUMBER, 
         KEY_ADDRESS, 
         KEY_CITY, 
         KEY_PROVINCE, 
         KEY_COUNTRY, 
         KEY_PCODE 
         }, 
         KEY_ROWID + "=" + rowId, 
         null, 
         null, 
         null, 
         null, 
         null); 
     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
     return mCursor; 
    } 

    //---updates a title--- 
    public boolean updateLocation(long rowId, String phone, 
    String stnumber, String address, String city, 
    String province, String country, String pcode) 
    { 
     ContentValues args = new ContentValues(); 
     args.put(KEY_PHONE, phone); 
     args.put(KEY_STNUMBER, stnumber); 
     args.put(KEY_ADDRESS, address); 
     args.put(KEY_CITY, city); 
     args.put(KEY_PROVINCE, province); 
     args.put(KEY_COUNTRY, country); 
     args.put(KEY_PCODE, pcode); 
     return db.update(DATABASE_TABLE, args, 
         KEY_ROWID + "=" + rowId, null) > 0; 
    } 
} 

있습니다 내 된 .java입니다 : 내가 오해 귀하의 코드, 잘하면 당신 didn 아직 읽어 보지 마!

ListView의 데이터를 커서에서 가져 오면 SimpleCursorAdapter을 사용하여이 모든 것을 처리하는 더 쉬운 방법이 있습니다.이 방법을 사용하면 커서에서 ListView가 채워지고 항목을 클릭하면 어떤 행이 표시되는지 알 수 있습니다. 클릭 된 항목이있는 커서.

+0

내가 문자열 줄을 추가하면 여전히 i.putExtra ("ct_address_pass", col2 [position])에서 java 오류가 발생합니다. i.putExtra ("ct_address_pass", col2)로 변경하면; 나는 더 이상 자바 오류를 얻지 만 null이 없으므로 데이터가 전달되지 않습니다. – PsychoDogg

+0

그래서 문자열을 추가하고 변경된 putextra를 변경 한 후 \t public void onItemClick (AdapterView TimsFavs, 뷰 뷰, int 위치, 긴 ID) onItemClick (AdapterView 상위 뷰 뷰, int 위치, 긴 ID) {그리고 그것은 효과가 있었다. – PsychoDogg

+0

@PsychoDogg Cursor를 통해 작업 할 때 찾은 모든 문자열을 결과에 포함하기 때문에 @PsychoDogg는'results.get (position)'을 사용하여 해당 행에서 문자열을 가져 오십시오. 일반적으로 이것은 활동에서 커서를 사용하려는 경우 CursorAdapter로 처리하는 것이 좋습니다. – Dan

관련 문제