-1

我面临的问题是我的代码正在执行“else”块,即使“if”条件为真。

public class MsgNewPackage extends Activity {


    private DatabaseHandler dbhandler;
     private SimpleCursorAdapter dataAdapter;
     private  ListView listView;

     String PKG_NAME,PKG_DUR,PKG_PRICE,PKG_ITEMS;

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





         dbhandler = new DatabaseHandler(this);


          displayListView();
    }




private void displayListView() 


    {

    String check= "Mobilink";

     Intent intent = getIntent();
     String conn = intent.getStringExtra("NET_CONN");
    // Toast.makeText(getApplicationContext(), conn, Toast.LENGTH_SHORT).show(); 

     if(conn == check)
     {


        Cursor cursor = dbhandler.fetchAllMOBSMSPackages();

         String[] columns = new String[]{DatabaseHandler.KEY_PKG_NAME,DatabaseHandler.KEY_NO_OF_FREE_ITEMS,DatabaseHandler.KEY_DURATION,DatabaseHandler.KEY_CHARGES};

         // the XML defined views which the data will be bound to
          int[] to = new int[] { R.id.title,R.id.no_of_item,R.id.duration,R.id.pkgcharges};

        // create the adapter using the cursor pointing to the desired data 
          //as well as the layout information
          dataAdapter = new SimpleCursorAdapter(this, R.layout.list_row2, cursor,columns,to);

           listView = (ListView) findViewById(R.id.listView1);
          // Assign adapter to ListView
          listView.setAdapter(dataAdapter);

     } 


     else


     {  

            Cursor cursor = dbhandler.fetchAllSMSPackages();


             String[] columns = new String[]{DatabaseHandler.KEY_PKG_NAME,DatabaseHandler.KEY_NO_OF_FREE_ITEMS,DatabaseHandler.KEY_DURATION,DatabaseHandler.KEY_CHARGES};

             // the XML defined views which the data will be bound to
              int[] to = new int[] { R.id.title,R.id.no_of_item,R.id.duration,R.id.pkgcharges};

            // create the adapter using the cursor pointing to the desired data 
              //as well as the layout information
              dataAdapter = new SimpleCursorAdapter(this, R.layout.list_row2, cursor,columns,to);

               listView = (ListView) findViewById(R.id.listView1);
              // Assign adapter to ListView
              listView.setAdapter(dataAdapter);
     }

toast 显示 conn 具有“Mobilink”,但列表视图填充了 fetchAllSMSPackages() 的结果(即在 else 块中调用的函数),情况并非如此。请帮忙。

4

2 回答 2

4

Try using conn.equals(check) instead of conn == check.

Strings in Java are objects, and not primitives, so you cannot compare their value using ==. This will compare the object as a whole, and not the text.

于 2013-08-14T19:00:55.120 回答
2

You should use .equals() when comparing String values. Not ==.

Try using:

conn.equals(check);
于 2013-08-14T19:01:48.560 回答