我面临的问题是,即使“if”条件为真,我的代码也会执行“else”阻止。
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块中调用的函数),但不应该是这种情况。请求帮助。
答案 0 :(得分:4)
尝试使用conn.equals(check)
代替conn == check
。
Java中的字符串是对象,而不是基元,因此您无法使用==
来比较它们的值。这将比较整个对象,而不是文本。
答案 1 :(得分:2)
比较字符串值时应使用.equals()
。不是==。
尝试使用:
conn.equals(check);