gpt4 book ai didi

java - 从选定的 ListViewItem 文本填充 TextView

转载 作者:行者123 更新时间:2023-12-01 12:29:47 27 4
gpt4 key购买 nike

每次单击 ListView 中的任何文本时,我都想在新 Activity 中的 TextView 上调用 setText。例如,如果我单击 listView 中包含“Android”的项目,我希望“Android”一词出现在新 Activity 中。

找到下面的代码:

public class ThirdMainActivity extends ActionBarActivity {

ListView lvCourses;
String[] courses = { "Android", "HTML 5", "J2SE", "JavaScript", "MySQL", "PHP", "CSS", "JQuery", "C#"};

String[] descr = { "Android Course", "HTML 5 Course", "J2SE Course", "JavaScript Course", "MySQL Course", "PHP Course", "CSS Course", "JQuery Course", "C# Course" };

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

lvCourses = (ListView) findViewById(R.id.lvCourses);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, Android.R.layout.simple_list_item_1, courses);

final TextView rr = (TextView)findViewById(R.id.setT);
lvCourses.setAdapter(adapter);
lvCourses.setOnItemClickListener(
new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> lv, View view, int position, long id) {

if(position == 0){
rr.setText(""+descr[0]);
Intent e = new Intent(getApplicationContext(),TeMainActivity.class);
startActivity(e);
}

if(position == 1){
rr.setText(""+descr[1]);
Intent e = new Intent(getApplicationContext(),TeMainActivity.class);
startActivity(e);
}
}
});
}
}

最佳答案

您无需检查所有仓位状态。只需在 onItemClick() 上执行此操作

rr.setText(descr[position]);
Intent e = new Intent(getApplicationContext(),TeMainActivity.class);
startActivity(e);

如果您想将此数据传递给新 Activity

rr.setText(descr[position]);
Intent e = new Intent(getApplicationContext(),TeMainActivity.class);
e.putExtra("desc", descr[position])
startActivity(e);

关于java - 从选定的 ListViewItem 文本填充 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26026503/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com