我正在製作一個Android應用程序,並且在我的活動中,我執行了一個對數據庫的查詢並取得結果。我將結果和TextView添加到Activity中。我希望當我點擊TextView,傳遞給下一個活動餐廳的名稱,我點擊。我的代碼的問題是,它爲所有的TextViews保存最後一個餐廳的名稱。有任何想法嗎?謝謝!生成帶有循環的TextViews併爲每個生成點擊
public class ViewRestaurants extends Activity{
String name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.row_restaurant);
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c = db.getSpRestaurants(getIntent().getStringExtra("city"), getIntent().getStringExtra("area"), getIntent().getStringExtra("cuisine"));
View layout = findViewById(R.id.items);
if(c.moveToFirst())
{
do{
name = c.getString(0);
TextView resname = new TextView(this);
TextView res = new TextView(this);
View line = new View(this);
resname.setText(c.getString(0));
resname.setTextColor(Color.RED);
resname.setTextSize(30);
resname.setTypeface(null,Typeface.BOLD);
res.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
res.setText(c.getString(1)+","+c.getString(2)+","+c.getString(3)+"\n"+c.getString(4));
res.setTextSize(20);
res.setTextColor(Color.WHITE);
res.setClickable(true);
res.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setClassName("com.mdl.cyrestaurants.guide", "com.mdl.cyrestaurants.guide.RestaurantDetails");
i.putExtra("name",name);
startActivity(i);
}
});
line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,2));
line.setBackgroundColor(Color.RED);
((LinearLayout) layout).addView(resname);
((LinearLayout) layout).addView(res);
((LinearLayout) layout).addView(line);
}while (c.moveToNext());
}
db.close();
}
}
謝謝,問題解決了:) – nestorasg