设置新活动的视图数据

发布于 2024-12-25 21:57:53 字数 1577 浏览 1 评论 0原文

因此,我有一个 ListView,单击它即可启动名为 MerchantView 的新 Activity。 在活动之间,我传递 uid,它是商家的唯一标识符。 然后我从数据库中提取商家数据并希望在此视图中查看此数据。 一切正常(在调试时我可以看到数据是从数据库获取并正确传递给 setText 方法)但数据没有显示,我这样做对吗?

public class MerchantView extends Activity {
public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.merchant);

     String desc = "";
     String name = "";


     Bundle extras = getIntent().getExtras();
     String uid = "0";
     if(extras !=null) {
         uid = extras.getString("uid");     
     }

     // get merchant from database
     if(Integer.valueOf(uid) > 0){
         Cursor c = Utilities.db.query(mydb.TABLE_MERCHANT,
                null,
                "uid=?", new String[] {uid}, null, null, null);

         if(c.moveToFirst() != false){
             name = c.getString(c.getColumnIndex(MerchantsColumns.COLname));
             desc = c.getString(c.getColumnIndex(MerchantsColumns.COLdesc));
         }
         // set values to UI
         TextView descUI = (TextView) findViewById(R.id.merchantDescription);
         descUI.setText(desc);
         TextView nameUI = (TextView) findViewById(R.id.merchantName);
         nameUI.setText(name);           
     }
     else{

     }

     Button buttonMerchants = (Button) findViewById(R.id.buttonMerchants);
     buttonMerchants.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
     });



}

}

So I have a ListView on which I click to start new Activity called MerchantView.
Between the activities I am passing the uid which is a unique identifier of a merchant.
Im then extracting merchant data from DB and want to view this data in this view.
Everything works (while debugging i can see that data is taken from DB and passed properly to setText methods) but the data does not show, am I doing this right?

public class MerchantView extends Activity {
public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.merchant);

     String desc = "";
     String name = "";


     Bundle extras = getIntent().getExtras();
     String uid = "0";
     if(extras !=null) {
         uid = extras.getString("uid");     
     }

     // get merchant from database
     if(Integer.valueOf(uid) > 0){
         Cursor c = Utilities.db.query(mydb.TABLE_MERCHANT,
                null,
                "uid=?", new String[] {uid}, null, null, null);

         if(c.moveToFirst() != false){
             name = c.getString(c.getColumnIndex(MerchantsColumns.COLname));
             desc = c.getString(c.getColumnIndex(MerchantsColumns.COLdesc));
         }
         // set values to UI
         TextView descUI = (TextView) findViewById(R.id.merchantDescription);
         descUI.setText(desc);
         TextView nameUI = (TextView) findViewById(R.id.merchantName);
         nameUI.setText(name);           
     }
     else{

     }

     Button buttonMerchants = (Button) findViewById(R.id.buttonMerchants);
     buttonMerchants.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
     });



}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桜花祭 2025-01-01 21:57:53

数据设置正确。问题出在布局上。顶部的标题(LinearLayout)有两个按钮,它被设置为 android:layout_height="fill_parent" ,它占据了整个空间。修复后,数据可以正确显示。

Data was set properly. The problem was in layout. The header (LinearLayout) at the top had two buttons and it was set to android:layout_height="fill_parent" which was taking whole space. After fixing that, data is showing properly.

や莫失莫忘 2025-01-01 21:57:53

尝试下面的代码希望有帮助

SQLiteDatabase myDB = this.openOrCreateDatabase("databasename.db", SQLiteDatabase.OPEN_READWRITE, null);
     try{
            Cursor c = myDB.rawQuery("select name, desc from abctable where uid="+uid, null); 
            int Column1 = c.getColumnIndex("name");
            int Column2 = c.getColumnIndex("desc");

        // Check if our result was valid.
        c.moveToFirst();
        if (c != null) {
            int i = 0;
            // Loop through all Results
            do {
                i++;
             String name = c.getString(Column1);
             String desc = c.getString(Column2);
             TextView descUI = (TextView) findViewById(R.id.merchantDescription);
                  descUI.setText(desc);
                  TextView nameUI = (TextView) findViewById(R.id.merchantName);
         nameUI.setText(name);
            } while (c.moveToNext()); 
                 } 


        } catch (SQLiteException e) { 
            e.printStackTrace();
        } finally { 
             if (myDB != null) 
                  myDB.close(); 
        }

Try Below code hope it helps

SQLiteDatabase myDB = this.openOrCreateDatabase("databasename.db", SQLiteDatabase.OPEN_READWRITE, null);
     try{
            Cursor c = myDB.rawQuery("select name, desc from abctable where uid="+uid, null); 
            int Column1 = c.getColumnIndex("name");
            int Column2 = c.getColumnIndex("desc");

        // Check if our result was valid.
        c.moveToFirst();
        if (c != null) {
            int i = 0;
            // Loop through all Results
            do {
                i++;
             String name = c.getString(Column1);
             String desc = c.getString(Column2);
             TextView descUI = (TextView) findViewById(R.id.merchantDescription);
                  descUI.setText(desc);
                  TextView nameUI = (TextView) findViewById(R.id.merchantName);
         nameUI.setText(name);
            } while (c.moveToNext()); 
                 } 


        } catch (SQLiteException e) { 
            e.printStackTrace();
        } finally { 
             if (myDB != null) 
                  myDB.close(); 
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文