我正在開發一個應用程序,其中,列表視圖中的每一行都有5個文本視圖(t1..t5)和其中的值。我有20行,每行在textviews中有不同的值。現在,當我點擊一個項目(在單個行上)時,另一個頁面打開並顯示五個文本視圖。我的問題是,如何獲取每行文本視圖的值到我導航到的頁面並將值放入文本視圖中。值也應該相應地變化,這取決於我點擊的位置。在android中的列表視圖中獲取項目
請你幫我解決這個問題。
我正在開發一個應用程序,其中,列表視圖中的每一行都有5個文本視圖(t1..t5)和其中的值。我有20行,每行在textviews中有不同的值。現在,當我點擊一個項目(在單個行上)時,另一個頁面打開並顯示五個文本視圖。我的問題是,如何獲取每行文本視圖的值到我導航到的頁面並將值放入文本視圖中。值也應該相應地變化,這取決於我點擊的位置。在android中的列表視圖中獲取項目
請你幫我解決這個問題。
試試這個
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String str = ((TextView) view.findViewById(R.id.someid)).getText().toString();
//Now pass this to intent
Intent intent = new Intent(getApplicationContext(),
SomeActivity.class);
intent.putExtra("string", str);
startActivity(intent);
}
});
嘗試使用onItemClickListener
contests_listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
//Here you can write code
}
});
獲取從ListView
的項目,在Intent
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView txt1 = (TextView) view.findViewById(R.id.txt1);
TextView txt2 = (TextView) view.findViewById(R.id.txt2);
TextView txt3 = (TextView) view.findViewById(R.id.txt3);
TextView txt4 = (TextView) view.findViewById(R.id.txt4);
TextView txt5 = (TextView) view.findViewById(R.id.txt5);
String strTxt1 = txt1.getText().toString();
String strTxt2 = txt2.getText().toString();
String strTxt3 = txt3.getText().toString();
String strTxt4 = txt4.getText().toString();
String strTxt5 = txt5.getText().toString();
}
});
給他們添加strTxt1,strTxt2,strTxt3,strTxt4和strTxt5到Intent
內,您的MainActivity的onCreate()方法,請執行下列操作
ListView listView = (ListView)findViewById(R.id.listView);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String str1 = ((TextView) view.findViewById(R.id.txt1)).getText().toString();
String str2 = ((TextView) view.findViewById(R.id.txt2)).getText().toString();
String str3 = ((TextView) view.findViewById(R.id.txt3)).getText().toString();
String str4 = ((TextView) view.findViewById(R.id.txt4)).getText().toString();
String str5 = ((TextView) view.findViewById(R.id.txt5)).getText().toString();
//Start a new intent and pass all these string values to your new Activity
Intent intent = new Intent(getApplicationContext(),
NewActivity.class);
intent.putExtra("t1", str1);
intent.putExtra("t2", str2);
intent.putExtra("t3", str3);
intent.putExtra("t4", str4);
intent.putExtra("t5", str5);
startActivity(intent);
}
});
現在,您的新活動的onCreate()方法中做這個
Intent intent = getIntent();
String str1 = intent.getStringExtra("t1");
String str2 = intent.getStringExtra("t2");
String str3 = intent.getStringExtra("t3");
String str4 = intent.getStringExtra("t4");
String str5 = intent.getStringExtra("t5");
TextView t1 = (TextView)findViewById(R.id.textView1);
t1.setText(str1);
.....
所以再次感謝你。 。:) –
沒問題。我希望它對你有用。 –
是的,它工作完美。 :) –