0

In my project there three java classes they are: MainActivity.java,Activity2.java and DataBaseHelper.java. with two xml layouts. now i want to pass datas from MainAcitivity to Activity2 when i click on ListItem of activity_main layout and which is going to display on TextViews of act2 layout.Here i have used a sqliteDatabase with ID, NAME and DESC as a column name, my problem is i was not able to display NAME and DESC on TextView once i Clicked on each listview, i was only able to display ID. I have searched for few days for solving this problem but i didn't get anything. i know its a silly and easy question but it would be very great of you if you are able to share any information and help. Thank you! i have used below code:

MainActivity.java:

public final static String EXTRA_NAME="com.example.verena.NAME";
   public final static String EXTRA_DESC="com.example.verena.DESC";


     private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
            private AdapterView listView;

            public AdapterView getListView() {
                return listView;
            }

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

                /* TextView viewName = (TextView)view.findViewById(R.id.passed);
                 TextView viewDes = (TextView)view.findViewById(R.id.passeddes);

                 String sName = viewName.getText().toString();
                String sDesc = viewDes.getText().toString();
                i.putExtra(EXTRA_NAME,sName);
                i.putExtra(EXTRA_DESC, sDesc);*/
                //2detail


                Intent i = new Intent(MainActivity.this, Activity2.class);
                i.putExtra(EXTRA_NAME, String.valueOf(id));


                //
                Toast.makeText(getApplicationContext(), "Saved Data", Toast.LENGTH_LONG).show();

                int pos = this.getListView().getSelectedItemPosition();
                Cursor c = (Cursor) this.getListView().getAdapter().getItem(pos);

                Bundle b = new Bundle();
                b.putString(EXTRA_DESC, c.getString(0));
                b.putString(EXTRA_NAME, c.getString(1));
                i.setClass(MainActivity.this, Activity2.class);
                i.putExtras(b);

                /* TextView desv= (TextView)findViewById(R.id.passeddes);
                String  desS = desv.getText().toString();
                i.putExtra(EXTRA_DESC, desS);*/

               /* TextView viewDes = (TextView)view.findViewById(R.id.passeddes);
                String sDesc = viewDes.getText().toString();
                i.putExtra(EXTRA_DESC, sDesc);*/
       /*TextView viewDes = (TextView)view.findViewById(R.id.passeddes);
                String sDesc = viewDes.getText().toString();
                i.putExtra(EXTRA_DESC, sDesc);*/
                startActivity(i);
            }

Activity2.java

String passedVar=null;
private TextView passedNameView = null;
String passedVardes = null;
private TextView passedViewDescView = null;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act2);

    passedVar = getIntent().getStringExtra(MainActivity.EXTRA_NAME);
    passedVardes= getIntent().getStringExtra(MainActivity.EXTRA_DESC);
    passedView=(TextView)findViewById(R.id.passed);
    passedViewDesc=(TextView)findViewById(R.id.passeddes);
    passedNameView.setText("you have click on:" + passedVar);
    passedViewDescView.setText("you hv click on:"+passedVardes);
}

DatabaseHelper.java

    public class DatabaseHelper extends SQLiteOpenHelper {

    public static String DB_PATH = "/data/data/com.example.verena/databases/";
    public static String DB_NAME = "dic.sqlite";
    public static final int DB_VERSION = 1;

    public static final String TB_USER = "Users";

    private SQLiteDatabase myDB;
    private Context context;

    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);  
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

    }

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

    }

    @Override
    public synchronized void close(){
        if(myDB!=null){
            myDB.close();
        }
        super.close();
    }

    public List<String> getAllUsers(){
        List<String> listUsers = new ArrayList<String>();
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor c;


        try {
            c = db.rawQuery("SELECT * FROM " + TB_USER , null);
            if(c == null) return null;

            String name;
            c.moveToFirst();
            do {            
                name = c.getString(1);          
                listUsers.add(name);
            } while (c.moveToNext()); 
            c.close();
        } catch (Exception e) {
            Log.e("tle99", e.getMessage());
        }


        db.close();

        return listUsers;
    }


    /***
     * Open database
     * @throws SQLException
     */
    public void openDataBase() throws SQLException{
        String myPath = DB_PATH + DB_NAME;
        //myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
        myDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
    }
    public void copyDataBase() throws IOException{
        try {
            InputStream myInput = context.getAssets().open(DB_NAME);
            String outputFileName = DB_PATH + DB_NAME;
            OutputStream myOutput = new FileOutputStream(outputFileName);

            byte[] buffer = new byte[1024];
            int length;

            while((length = myInput.read(buffer))>0){
                myOutput.write(buffer, 0, length);
            }

            myOutput.flush();
            myOutput.close();
            myInput.close();
        } catch (Exception e) {
            Log.e("tle99 - copyDatabase", e.getMessage());
        }

    }

    /***
     * Check if the database doesn't exist on device, create new one
     * @throws IOException
     */
    public void createDataBase() throws IOException {
        boolean dbExist = checkDataBase();      

        if (dbExist) {

        } else {
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                Log.e("tle99 - create", e.getMessage());
            }
        }
    }

    // ---------------------------------------------
    // PRIVATE METHODS
    // ---------------------------------------------

    /***
     * Check if the database is exist on device or not
     * @return
     */
    private boolean checkDataBase() {
        SQLiteDatabase tempDB = null;
        try {
            String myPath = DB_PATH + DB_NAME;
            tempDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READWRITE);
        } catch (SQLiteException e) {
            Log.e("tle99 - check", e.getMessage());
        }
        if (tempDB != null)
            tempDB.close();
        return tempDB != null ? true : false;
    }
}
4

1 回答 1

0

您将 id 作为字符串、名称和描述传递到包中。因此,您必须从意图中获取捆绑包,并从该捆绑包中获取您的姓名和描述。另一种方法是您可以将 id、name 和 desc 作为字符串传递,如下所示:

在你的MainActivity.java

// New Intent
Intent i = new Intent(MainActivity.this, Activity2.class);

// Put Extras
// Put your id
i.putExtra("id", String.valueOf(id));

int pos = this.getListView().getSelectedItemPosition();
Cursor c = (Cursor) this.getListView().getAdapter().getItem(pos);

// Put Name
i.putExtra(EXTRA_NAME, c.getString(0));
// Put Description
i.putExtra(EXTRA_DESC, c.getString(1));

i.setClass(MainActivity.this, Activity2.class);

startActivity(i);

要进入id您的Activity2.java,请在您的方法中添加以下行onCreate

String passedVarid = getIntent().getStringExtra("id");
于 2015-10-07T05:36:00.553 回答