当我点击活动中的项目时,我发现了一个错误,他们都显示了与他的名字相同的字符串:“name_0”
点击时打开的活动:
public void handle() {
TextView var3 = (TextView) this.findViewById(R.id.name);
TextView var2 = (TextView) this.findViewById(R.id.meaning);
TextView var5 = (TextView) this.findViewById(R.id.proof);
Intent var4 = this.getIntent();
int var1 = var4.getIntExtra("name", 0);
switch (var1) {
case 0:
var3.setText(R.string.name_0);
var2.setText(R.string.meaning_0);
var5.setText(R.string.proof_0);
break;
case 1:
var3.setText(R.string.name_1);
var2.setText(R.string.meaning_1);
var5.setText(R.string.proof_1);
break;
case 2:
var3.setText(R.string.name_2);
var2.setText(R.string.meaning_2);
var5.setText(R.string.proof_2);
break;
}
}
以及打开项目onclick的活动:
String[] values = { "test", "list", "ok", "bon" };
protected void onCreate(Bundle var1) {
super.onCreate(var1);
this.setContentView(R.layout.activity_main);
this.listView = (ListView) this.findViewById(R.id.list);
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.listview_item,
R.id.textView_list, values));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
Intent Start = new Intent(MainActivity.this, Asma2Ma3anihaActivity.class);
startActivity(Start);
}
});
答案 0 :(得分:0)
问题是您没有发送所点击项目的位置
Intent start = new Intent(MainActivity.this, Asma2Ma3anihaActivity.class);
start.putExtra("name", position);
startActivity(start);
这应该有效。
你总是得0,因为你在这里设置0(var4.getIntExtra("name", 0);
)作为缺少“name”键时的默认值。
答案 1 :(得分:0)
在setOnItemClickListener中创建Intent时,永远不会在Intent上设置'name',因此当您尝试在'handle()'方法中检索int时,您将始终得到0。
在执行'startActivity'之前,您必须在意图上设置额外内容。
Intent startIntent = new Intent(MainActivity.this, Asma2Ma3anihaActivity.class);
startIntent.putExtra("name", position);
startActivity(startIntent);
答案 2 :(得分:0)
为了从项目中获取文本,您需要从itemclicklistener获取视图中的值
即
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
String text = ((TextView)view.findViewById(R.id.name)).getText();
Intent Start = new Intent(MainActivity.this, Asma2Ma3anihaActivity.class).putExtra("name",text);
startActivity(Start);
}
});