Parth Vora
Parth Vora

Reputation: 25

I have a listview that displays data from Sqlite Database but it does not work when click is fired on it?

I have a listview that displays data from Sqlite Database but it does not work when click is fired on it. There are two different users "Admin" and "Normal User". The setOnItemClickListner works correctly with "Normal User" but it is not working with "Admin" user.

Here is my code:

 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();
}

If Someone can help would be appreciable...thanks..

Upvotes: 0

Views: 74

Answers (1)

mdzeko
mdzeko

Reputation: 932

If you are using custom layout to display list item - check this answer. Other idea is to change the way you are adding click listener to your list. Another way would be:

  1. Pass this to setOnItemClickListener method of your ListView
  2. Have your ShowUserList class implement needed interface method
  3. Write click event code in implemented method.

Upvotes: 1

Related Questions