根据sqlite数据更改图像

时间:2013-10-29 08:20:59

标签: android arrays sqlite listview imageview

我想根据sqlite数据库的输出更改imageview,我有一个list_row布局,用于保存sqlite数据库中的数据。我想要的是:如果string = something,则更改该listview项的imageview。

的活动:

public class MainActivity extends FragmentActivity {

    Button btnStartService;
    Context context;
    Database mDB = new Database(this);
    Cursor c;
    SimpleCursorAdapter sCA;
    ListView lv;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ListView lv = (ListView)findViewById(R.id.listView1);
        SimpleCursorAdapter sCA;
        btnStartService = (Button) findViewById(R.id.button1);
        btnStartService.setText("Add test data");
        mDB.open();
        btnStartService.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                //startService(new Intent(MainActivity.this, CallService.class));
                //stopService(new Intent(MainActivity.this, CallService.class));
            }
        });

        ArrayList<String> list1  = new ArrayList<String>();

        ArrayList<String> list2 = new ArrayList<String>();

        c = mDB.getValues();
            c.moveToFirst();
            startManagingCursor(c);

            for (int i = 0; i < c.getCount(); i++) {

            String number = c.getString(c.getColumnIndex("cname"));

            String action = c.getString(c.getColumnIndex("caction"));

            String forwardnumber = c.getString(c.getColumnIndex("cnumber"));

            if (action.equals("Block Call")) {
                Log.d("LOG_TAG", "Block call");
                    //Change image to this item

            }else {
                Log.d("LOG_TAG", "Forward call");
                    //Change image
            }
            Log.d("LOG_TAG", number + " " + action + " " + forwardnumber + " ");
                list1.add(number);
                list2.add(forwardnumber);
                c.moveToNext();
            }
            //String []from = {"cname", "caction", "cnumber"};
            //int []to = {R.id.txtNumber,R.id.txtAction,R.id.txtFNumber};
            //sCA = new SimpleCursorAdapter(this, R.layout.list_row, c, from, to);
            //lv.setAdapter(sCA);
            //sCA.notifyDataSetChanged();
            //int []to = {R.id.txtNumber,R.id.txtAction,R.id.txtFNumber};
            //sCA = new SimpleCursorAdapter(this, R.layout.list_row, c, list, to);

        c = mDB.getEmp();
        String []from = {"cname","caction","cnumber"};
        int []to = {R.id.txtNumber,R.id.txtAction,R.id.txtFNumber};
        sCA = new SimpleCursorAdapter(this, R.layout.list_row, c, from, to);
        //ImageView iv = (ImageView)findViewById(R.id.imgAction);
        //iv.setImageResource(R.drawable.one);
        /*if (from[2] == "Block Call") {
            Log.d("LOG_TAG", "Blocked");
        }else {
            Log.d("LOG_TAG", "Forward");
        }*/
        lv.setAdapter(sCA);
        sCA.notifyDataSetChanged();
        c.requery();                           
    } 

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

    @Override
      protected void onPause() {
        super.onPause();
      }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_addnumber:
                Log.d("LOG_TAG", "Add number");
                FragmentManager fm = getSupportFragmentManager();
                CustomDialogFragment addDialog = new CustomDialogFragment();
                addDialog.show(fm, "dialog");
                return true;
        case R.id.action_settings:
                Log.d("LOG_TAG", "Open settings");
                Intent myIntent = new Intent(MainActivity.this, PrefrencesActivity.class);
                MainActivity.this.startActivity(myIntent);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("deprecation")
    public void doPositiveClick(String etNumber, String etSpinner, String etForwardTo){     
        ContentValues cv = new ContentValues();
        cv.put("cName", etNumber);
        cv.put("cAction", etSpinner);
        cv.put("cNumber", etForwardTo);
        mDB.empInsert(cv);
        c.requery(); 
    }

}

数据库类:

public class Database
{
        public static final String DB_NAME = "callContacts";
        public static final String DB_CONTACTS = "callContactList";
        MyHelper mh;
        Context myCon;
        SQLiteDatabase sdb;
        public Database(Context c) {
            myCon = c;
            mh = new MyHelper(myCon, DB_NAME, null, 1);
        }

        public void open() {
            sdb = mh.getWritableDatabase();
        }

        public class MyHelper extends SQLiteOpenHelper {
            public MyHelper(Context context, String name, CursorFactory factory, int version) {
                super(context, name, factory, version);
                // TODO Auto-generated constructor stub
            }

            @Override
            public void onCreate(SQLiteDatabase db){
                db.execSQL("create table callContactList(_id integer primary key, cname text,caction text,cnumber text);");
                Log.d("1", "Table Created");
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                // TODO Auto-generated method stub
            }                      
        }

        public void empInsert(ContentValues cv) {
            sdb.insert("callContactList", null, cv);
        }

        public Cursor getEmp() {
            Cursor c = sdb.query("callContactList", null, null, null, null, null, null);
             return c;
        }

        @SuppressWarnings({ "finally", "resource" })
        public boolean CheckNumber(String number) throws SQLException {
            int count = -1;
            Cursor c = null; 
            try {
               String query = "select * from callContactList where cname = " + number;
               c = sdb.rawQuery(query, new String[] {number});
               if (c.moveToFirst()) {
                  count = c.getInt(0);
               }
               return count > 0;
            }
            finally {
               if (c != null) {
                  Log.d("LOG_TAG", "True");
                  c.close();                  
                  return true;
               }else{
                   Log.d("LOG_TAG", "False");
                   return false;
               }
            }
        }

        public boolean verificationNumber(String _number) {
            Cursor c = sdb.rawQuery("SELECT 1 FROM callContactList WHERE cname=?", new String[] {_number});
            boolean exists = c.moveToFirst();
            Log.d("LOG_TAG", Boolean.toString(exists));
            c.close();
            return exists;

        }

        public boolean verificationAction(String _action, String _incomingNumber) {
            Cursor c = sdb.rawQuery("SELECT 1 FROM callContactList WHERE cname=? AND caction=?", new String[] {_incomingNumber, _action});
            boolean exists = c.moveToFirst();
            Log.d("LOG_TAG", Boolean.toString(exists));
            c.close();
            return exists;

        }

        public String getForwardNumber(String _number){
            String forwardNumber = "";
            String selectQuery = "SELECT * FROM callContactList WHERE cname="+_number; 

            //SQLiteDatabase sdb = this.getReadableDatabase();
            Cursor cursor = sdb.rawQuery(selectQuery, null);

            if (null != cursor && cursor.moveToFirst()) {
                forwardNumber = cursor.getString(cursor.getColumnIndex("cnumber"));
            } 

            return forwardNumber;

        }

        public Cursor getValues() throws SQLException{
            // TODO Auto-generated method stub


            Cursor mCursor = sdb.query("callContactList",
                              new String[] {"cname" , "caction", "cnumber"},
                                            null, null, null, null, null);
                    if (mCursor != null) {
                        mCursor.moveToFirst();
                    }

                    return mCursor;



        }
}

非常感谢任何帮助!感谢

0 个答案:

没有答案