我是一名机器人初学者。 我正在尝试将 onClick事件提供给listview中的数据。数据来自mysql数据库并存储在sqlite中,它显示在屏幕上。我不知道如何为该数据提供onClick事件。请给我一些建议。
提前致谢。
我正在尝试开发一个时间表应用程序。当用户点击按钮时,他当天必须参加的课程必须显示在列表视图中。当他点击课程时,他必须得到该课程的详细信息。
public class tuesday扩展了ListActivity {
private ArrayList<String> results = new ArrayList<String>();
private String DATABASE_TABLE = SQLiteDB.DATABASE_TABLE;
private SQLiteDatabase newDB;
private static final String KEY_PERIODNO = "periodno";
private static final String KEY_PERIODNAME = "periodname";
private String id;
TextView periodno,periodname;
SQLiteDB sqlite_obj;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tuesday);
sqlite_obj = new SQLiteDB(tuesday.this);
Intent previous = getIntent();
id = previous.getStringExtra("id");
Log.i("id: ", " " + id);
select_seqlite();
}
private void select_seqlite() {
sqlite_obj.open();
Cursor c = sqlite_obj.gettuesdayData();
if (c.moveToFirst())
{
do {
DisplayContact(c);
} while (c.moveToNext());
}
sqlite_obj.close();
}
private void DisplayContact(Cursor c) {
Toast.makeText(getBaseContext(),
"\n" +"periodno: " + c.getString(2)+ "\n"+"periodname: " + c.getString(3)+ "\n", Toast.LENGTH_LONG).show();
TextView tview = new TextView(this);
tview.setText(" " + c.getString(2) +
" "+ c.getString(3));
getListView().addHeaderView(tview);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.activity_list_item,results));
getListView().setTextFilterEnabled(true);}}
答案 0 :(得分:0)
在您的活动中,您定义了列表视图
在下面写下代码
listview.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?>adapter,View v, int position){
ItemClicked item = adapter.getItemAtPosition(position);
Intent intent = new Intent(Activity.this,destinationActivity.class);
//based on item add info to intent
startActivity(intent);
}
});
在你的适配器的getItem中你写了
public ItemClicked getItem(int position){
return items.get(position);
}
我希望它可以帮到你!