Clickable listView ishlamayapti

Men Android uchun juda yangi va tugmachalar ro'yxatidagi ko'rinishda ishlayapman, men duch kelayotgan muammom, bu har bir narsa tovuq ko'rsatilmaguncha va terminal oynasida chiqqandan qat'iy nazar ishlaydigan har qanday ishlaydi. Mening ro'yxatimDo'lovda hech qanday rasm bolasi yo'q, uning faqatgina matn shaklidagi ro'yxati. Shuning uchun Fokusability muammosi yo'q. Mana mening kodim

public class FavoritePage extends AppCompatActivity implements OnItemClickListener {

private ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_favorite_page);
    DataBaseHandler db = new DataBaseHandler(this);
    db.add_activity("");
    lv = (ListView) findViewById(R.id.listView);
    ArrayAdapter arr2 = new ArrayAdapter(getApplicationContext(), R.layout.simple_colum, db.getalllist());
    lv.setAdapter(arr2);
    db.del_activity("");


}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    String data = (String) parent.getItemAtPosition(position);
    Toast.makeText(getApplicationContext(), "This is my Toast message!",
            Toast.LENGTH_LONG).show();
    System.out.println("***************************************************************" + data);
}}

Har qanday fikr nima uchun shunday qiladi .. ??

2
Xo'sh, avval tinglovchilar ro'yxatini ko'rishingiz kerak. Bir narsa: lv.setOnItemClickListener (this) . Buni findViewById() kodining ostiga onCreate() ga qo'shing
qo'shib qo'ydi muallif mt0s, manba
Rahmat, u joziba kabi ishladi, men uni to'g'ri belgilashim uchun quyidagi javobni yozing
qo'shib qo'ydi muallif Alpit Anand, manba

8 javoblar

Faoliyatdagi OnItemClickListenerni amalga oshirishning o'rniga, siz quydagi kabi biror narsani sinashingiz mumkin

public class FavoritePage extends AppCompatActivity {

private ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_favorite_page);
    DataBaseHandler db = new DataBaseHandler(this);
    db.add_activity("");
    lv = (ListView) findViewById(R.id.listView);
    ArrayAdapter arr2 = new ArrayAdapter(getApplicationContext(), R.layout.simple_colum, db.getalllist());
    lv.setAdapter(arr2);
    lv.setOnItemClickListener(clickListener);
    db.del_activity("");
}

private AdapterView.OnItemClickListener clickListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    }
};

}

1
qo'shib qo'ydi

Faoliyatdagi OnItemClickListenerni amalga oshirishning o'rniga, siz quydagi kabi biror narsani sinashingiz mumkin

public class FavoritePage extends AppCompatActivity {

private ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_favorite_page);
    DataBaseHandler db = new DataBaseHandler(this);
    db.add_activity("");
    lv = (ListView) findViewById(R.id.listView);
    ArrayAdapter arr2 = new ArrayAdapter(getApplicationContext(), R.layout.simple_colum, db.getalllist());
    lv.setAdapter(arr2);
    lv.setOnItemClickListener(clickListener);
    db.del_activity("");
}

private AdapterView.OnItemClickListener clickListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    }
};

}

1
qo'shib qo'ydi

Xo'sh, avval tinglovchilar ro'yxatini ko'rishingiz kerak. Bir narsa: lv.setOnItemClickListener (this) .

Buni findViewById() kodining to'liq qismida onCreate() ga qo'shing.

1
qo'shib qo'ydi

Xo'sh, avval tinglovchilar ro'yxatini ko'rishingiz kerak. Bir narsa: lv.setOnItemClickListener (this) .

Buni findViewById() kodining to'liq qismida onCreate() ga qo'shing.

1
qo'shib qo'ydi

Siz onItemClickListener xususiyatini sozlashda kamchiliksiz.

onCreate usulida lv = (ListView) findViewById (R.id.listView); bu qatordan so'ng quyidagi qatorni qo'shing lv.setOnItemClickListener (this) .

Bundan tashqari, Java dasturiyasini ishlab chikarishda camelCase uslubi dan foydalanishni tavsiya etaman.

1
qo'shib qo'ydi

Siz onItemClickListener xususiyatini sozlashda kamchiliksiz.

onCreate usulida lv = (ListView) findViewById (R.id.listView); bu qatordan so'ng quyidagi qatorni qo'shing lv.setOnItemClickListener (this) .

Bundan tashqari, Java dasturiyasini ishlab chikarishda camelCase uslubi dan foydalanishni tavsiya etaman.

1
qo'shib qo'ydi

Quyidagi kod sizga yordam berishi mumkin, deb o'ylayman: -

lv.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {

                  String data = (String) parent.getItemAtPosition(position);
    Toast.makeText(getApplicationContext(), "This is my Toast message!",
            Toast.LENGTH_LONG).show();
    System.out.println("***************************************************************" + data);
            });
1
qo'shib qo'ydi

Quyidagi kod sizga yordam berishi mumkin, deb o'ylayman: -

lv.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {

                  String data = (String) parent.getItemAtPosition(position);
    Toast.makeText(getApplicationContext(), "This is my Toast message!",
            Toast.LENGTH_LONG).show();
    System.out.println("***************************************************************" + data);
            });
1
qo'shib qo'ydi