我有一个列表视图,显示来自Sqlite数据库的数据,但是当点击它时它不起作用。有两个不同的用户"管理员"和#34;普通用户"。 setOnItemClickListner可以正常使用"普通用户"但它没有与" Admin"用户。
这是我的代码:
public class ShowUserList extends AppCompatActivity {
SimpleCursorAdapter sca;
BaseCustomArrayListAdapter bcal;
DataBaseHelper DBHelp = new DataBaseHelper(this);
ArrayList<String> col;
String uname, pass;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
Intent i = getIntent();
uname = i.getStringExtra("Username");
pass = i.getStringExtra("Password");
String userrole = "";
lv = (ListView) findViewById(R.id.listView);
DBHelp.open();
Cursor c = DBHelp.CheckUserRole(uname, pass);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
userrole = c.getString(c.getColumnIndex(DataBaseHelper.Key_USER_ROLE));
}
if(userrole.equals("Admin")){
GetData();
}
else if(userrole.equals("User")){
getdatauser();
}
DBHelp.close();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("List Item","Clicked");
Log.d("Position", "" + position);
int pos = position+1;
Toast.makeText(ShowUserList.this,"List Item Clicked",Toast.LENGTH_SHORT).show();
Intent details_intent = new Intent(ShowUserList.this,ShowListExtended.class);
details_intent.putExtra("POSITION", ""+pos);
startActivity(details_intent);
}
});
}
public void GetData(){
DBHelp.open();
Cursor c = DBHelp.getData();
col = new ArrayList<String>();
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
col.add(c.getString(c.getColumnIndex(DataBaseHelper.Key_FULL_NAME)) +
"\n" +
c.getString(c.getColumnIndex(DataBaseHelper.Key_GENDER)) +
"\n" +
c.getString(c.getColumnIndex(DataBaseHelper.Key_HOBBIES)) + "\n");
}
lv = (ListView) findViewById(R.id.listView);
bcal = new BaseCustomArrayListAdapter(this,col);
lv.setAdapter(bcal);
bcal.notifyDataSetChanged();
lv.invalidateViews();
DBHelp.close();
}
public void getdatauser(){
DBHelp.open();
Cursor c = DBHelp.getData();
String[] columns = {
DataBaseHelper.Key_FULL_NAME,
DataBaseHelper.Key_GENDER,
DataBaseHelper.Key_HOBBIES
};
int[] to = {
R.id.userFullName,
R.id.userGender1,
R.id.userHobbies1
};
lv = (ListView) findViewById(R.id.listView);
sca = new SimpleCursorAdapter(ShowUserList.this,R.layout.listview_user,c,columns,to,0);
lv.setAdapter(sca);
sca.notifyDataSetChanged();
lv.invalidateViews();
DBHelp.close();
}
如果有人可以提供帮助,那就太感激了......谢谢..
答案 0 :(得分:1)
如果您使用自定义布局显示列表项 - 请检查this answer。 其他想法是改变将click侦听器添加到列表的方式。另一种方式是:
this
传递给ListView的setOnItemClickListener
方法ShowUserList
类实现所需的接口方法