jeudi 17 décembre 2015

i want to get specfic row data from sqlite database, against when i enter any name or id in edit text and then it match in dB & show data in txt viw

enter code here

// Guiedeenter code here me step by step make my code easy to write //MY DATABASE CLASS {DBControler}

public class DBController extends SQLiteOpenHelper {
    private static final String tablename = "info";

    private static final String id = "ID";  // auto generated ID column
    private static final String home = "home";
    private static final String name = "name";
    private static final String nic = "nic"; //column name
    private static final String fatherName = "fatherName"; // column name
    private static final String cast = "cast";
    private static final String address = "address";
    private static final String blockCode = "blockCode";
    private static final String databasename = "recordInfo"; // Dtabasename
    private static final int versioncode = 1; //versioncode of the database
    public DBController(Context context) {
        super(context, databasename, null, versioncode);
    }
    @Override
    public void onCreate(SQLiteDatabase database) {
        String query;
        query = "CREATE TABLE IF NOT EXISTS " + tablename +
                "(" + id + " integer primary key, " + home + " text, " + nic + " integer, " + name + " text, "
                + fatherName + " text," + cast + " text, " + address + " text," + blockCode + " text)";

        database.execSQL(query);
    }
    @Override
    public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
        String query;
        query = "DROP TABLE IF EXISTS " + tablename;
        database.execSQL(query);
        onCreate(database);
    }

   // public ArrayList<HashMap<String, String>> getAllrecord() {ArrayList<HashMap<String, String>> wordList;

   public ArrayList<HashMap<String, String>>getAllrecord()
          {ArrayList<HashMap<String, String>> wordList;
        //wordList = new ArrayList<HashMap <String, String>>();
        wordList = new ArrayList<>();
        String selectQuery = "SELECT  * FROM " + tablename;
        SQLiteDatabase database = this.getWritableDatabase();
        Cursor cursor = database.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
          //HashMap<String, String> map = new HashMap<String, String>();
                HashMap<String, String> map = new HashMap<>();
                map.put("id", cursor.getString(0));
                map.put("home",cursor.getString(1));
                map.put("nic", cursor.getString(2));
                map.put("name", cursor.getString(3));
                map.put("fatherName", cursor.getString(4));
                map.put("cast", cursor.getString(5));
                map.put("address", cursor.getString(6));
                map.put("blockCode", cursor.getString(7));

                wordList.add(map);
            } while (cursor.moveToNext());
        }
        // return contact list
        return wordList;
    }

}

// ANOTHER CLASS {RECORD LIST}

public class RecordList extends Activity {
    DBController controller = new DBController(this);
    ListView lv;
    TextView infotext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recordlist);

        lv = (ListView) findViewById(R.id.recordlist);
        infotext = (TextView) findViewById(R.id.txtresulttext);
        try {
            List<HashMap<String , String>> data = controller.getAllrecord();
            if (data.size() != 0) {
                // Srno, RMCode, Fileno, Loc, FileDesc, TAGNos
    SimpleAdapter adapter = new SimpleAdapter(RecordList.this, 
                       data, R.layout.rows,
      new String[]{"id","home","nic", "name", "fatherName","cast","address","blockCode"}, new int[]{
                        R.id.txt_id,
                        R.id.txt_homeid,
                        R.id.txtnic,
                        R.id.txt_name,
                        R.id.txt_fatherName,
                        R.id.txt_cast,
                        R.id.txt_address,
                        R.id.txt_blockCode,
                        });

                lv.setAdapter(adapter);
                String length = String.valueOf(data.size());
                infotext.setText(length + " record ");
            } else {
                infotext.setText("No data in database");
            }
        } catch (Exception ex) {
            infotext.setText(ex.getMessage().toString());
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


THIS IS MY SPINER CLASS WITH NAME {FIRST ACTIVITY}

public class FirstActivity extends AppCompatActivity {

     EditText editText;
     Button getData;
     TextView TextV;
    private Spinner spinner;
    private static final String[]paths = {"search by NIC" , "Select by name", "search by house number","search by cast","search by block cod" };


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_activity);

        spinner = (Spinner)findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(FirstActivity.this,android.R.layout.simple_spinner_item,paths);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        //spinner.setOnItemSelectedListener(this);
        //spinner.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);

        TextV=(TextView)findViewById(R.id.detail_specific_data);
        getData=(Button)findViewById(R.id.get_data_on_click);
        editText  =(EditText)findViewById(R.id.edit_text_value);

        getData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.v("editText", editText.getText().toString());
                TextV.setText(editText.getText().toString());

            }
        });



    }

// first_activity.xml IN THIS XML VIEW I WANT TO SHOW SPECIFIC SEARCH DATA //FROM DATABASE ACORDING TO MY SEARCH

<LinearLayout
    xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Spinner

        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:drawable/btn_dropdown"
        android:spinnerMode="dropdown"
        />

    <EditText

        android:id="@+id/edit_text_value"
        android:hint="name"
        android:textColor="#0b0b0b"
        android:layout_width="match_parent"
        android:layout_height="38dp"
        android:background="#50fa06"
        android:layout_margin="4dp"
        android:gravity="center"


        />
    <Button
        android:id="@+id/get_data_on_click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:text=" GET DATA"/>


    <TextView

        android:gravity="center"
        android:background="#06a1fa"
        android:id="@+id/detail_specific_data"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_margin="5dp"/>

</LinearLayout>

Aucun commentaire:

Enregistrer un commentaire