我熟悉vb.net,除了Java之外的所有内容对我来说都是一个全新的游戏。所以请通过这个问题与我一起......
我从我找到的教程中设置了一个活动类。活动类只是打开一个新屏幕,并显示mainActivity上文本框中的消息。 APP在服务器上调用一个php文件,返回JSON数据。当我测试它时,那一面在浏览器中运行良好。
现在为android问题。我试图让这些数据显示在messageActivity类中。我试图简单地使用下面的代码来做,但eclipse标记意图行说“构造函数Intent(Connect,Class)未定义”
我的问题是,触发活动的正确方法是什么,以便显示JSON数据;其次,如果查看quaryDB方法,如何从响应中获取MessageActivity的JSON数据?
主要活动:
public class MainActivity extends Activity {
RadioButton radioButton1, radioButton2, radioButton3;
public final static String EXTRA_MESSAGE = "com.example.xxxxxxx.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioButton1 = (RadioButton)findViewById(R.id.radioButton1);
radioButton2 = (RadioButton)findViewById(R.id.radioButton2);
radioButton3 = (RadioButton)findViewById(R.id.radioButton3);
radioButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (radioButton1.isChecked()){
radioButton2.setChecked(false);
radioButton3.setChecked(false);
}
}
});
radioButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (radioButton2.isChecked()){
radioButton3.setChecked(false);
radioButton1.setChecked(false);
}
}
});
radioButton3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (radioButton3.isChecked()){
radioButton2.setChecked(false);
radioButton1.setChecked(false);
}
}
});
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.PracticeTypes, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
Connect c = new Connect();
c.quaryDB(this);
}
public class MyOnItemSelectedListener implements OnItemSelectedListener{
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id){
Toast.makeText(parent.getContext(), "The Selected Practice is " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent){
}
}
public void sendMessage(View view){
Intent intent= new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
};
DisplayMessageActivity:
public class DisplayMessageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView= new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
}
然后获取JSON数据响应的连接类:
public class Connect{
void Connect(){
}
public void quaryDB(Context context){
Connection conn=null;
try{
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.0.6/DevServer/getAllByZip.php");
HttpResponse response = client.execute(httppost);
HttpEntity entity = response.getEntity();
String succMsg = "Successful Execute.\n";
Toast.makeText(context, succMsg, Toast.LENGTH_LONG).show();
}
catch (Exception e){
String errMsg ="Cannot connect to database server.\n"+e.toString();
Toast.makeText(context, errMsg, Toast.LENGTH_LONG).show();
}
finally{
if (conn !=null){
try{
conn.close();
}
catch (Exception e){
}
}
}
}
public void sendMessage(View view){
Intent intent= new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
sendMessage方法在Connect类中感觉不对,但我对如何正确地将JSON数据发送到DisplayMessageActivity感到迷茫。在我想出那部分之后,我将能够开始解析数据并完成需要完成的工作。非常感谢任何和所有帮助。
答案 0 :(得分:1)
Fisrt我会解释这个问题:
Intent
的构造函数期望将活动上下文作为第一个参数。在扩展Activity
的类中使用它时,可以使用this
作为第一个参数,即活动本身。
当您在课程Connect
中使用它时,Activity
不会延伸this
,使用Connect
作为第一个参数将恢复为没有sendMessage
的课程背景。
<强>解决方案:强>
如果您从活动中呼叫sendMessage(this, myview);
,则可以执行以下操作:
Connect
课程public void sendMessage(Context context, View view){
Intent intent= new Intent(context, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
中的
public class Connect{
Context context;
void Connect(Context context){
this.context = context;
}
如果您没有从活动中调用它,请将上下文传递给类constructer:
sendMessage()
现在您可以使用{{1}}
中的上下文 祝你好运