我是新手,面临意图问题。 目标:生成一个列表,然后单击List中的项目,打开一个新的布局,其中一些参数在intent中传递。
问题:
请帮忙。
package com.example.testapp08;
import android.os.Bundle;
import android.view.View;
import android.app.ListActivity;
import android.content.Intent;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ListActivity {
String[]countries;
final String THE_ACTION2 = "com.example.testapp08.TEMPT2";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
countries=getResources().getStringArray(R.array.countries);
this.setListAdapter(new ArrayAdapter<String>(
this,
R.layout.list_layout,
R.id.countryName,
countries));
ListView listview = getListView();
listview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View clickView, int position,
long id) {
// TODO Auto-generated method stub
String country = countries[position];
//Toast.makeText(MainActivity.this, String.format("%s was chosen", country), Toast.LENGTH_SHORT).show();
//Intent intent = new Intent(MainActivity.this, Tempt1.class );
Intent intent = new Intent();
intent.setAction(THE_ACTION2);
intent.putExtra("MY TEXT", country);
startActivity(intent);
}
});
}
}
package com.example.testapp08;
import android.os.Bundle;
import android.widget.TextView;
public class Tempt1 extends MainActivity{
TextView textView1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.temp_layout);
textView1 = (TextView) findViewById(R.id.textView1);
Bundle bundle = getIntent().getExtras();
String var_from_prev_intent = bundle.getString("MY TEXT");
textView1.setText(var_from_prev_intent);
}
}
答案 0 :(得分:0)
我给了你一个非常简单的listview示例。
import android.os.Bundle;
import android.view.View;
import android.app.ListActivity;
import android.content.Intent;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ListActivity {
String[]countries;
final String THE_ACTION2 = "com.example.testapp08.TEMPT2";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
countries=getResources().getStringArray(R.array.country);
this.setListAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
countries));
ListView listview = getListView();
listview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View clickView, int position,
long id) {
// TODO Auto-generated method stub
String country = countries[position];
//Toast.makeText(MainActivity.this, String.format("%s was chosen", country), Toast.LENGTH_SHORT).show();
//Intent intent = new Intent(MainActivity.this, Tempt1.class );
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
//intent.setAction(THE_ACTION2);
intent.putExtra("MY TEXT", country);
startActivity(intent);
}
});
}
}
2)MainActivity2.java
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity2 extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
Intent i = getIntent();
Toast.makeText(this,i.getStringExtra("MY TEXT"), Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
现在,当您点击任何列表项时,您将看到以toast msg格式选择的内容。
希望这会对你有所帮助。