0

I'm new in Android and need help with my ListView, which contains text view and image in every list item. I need handle clicks on list item and on icon (R.id.list_star) in list item separately. I tried several ways but it seems I can't do it by myself. I tried to put star.setOnItemClickListener in the beginning, near lvData.setOnItemClickListener - but in this case view isn't found, and add swith inside AdapterView.OnItemClickListener - doesn't work. My code handles only list item click but no icon clicks

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class ListViewActivity extends AppCompatActivity  implements LoaderManager.LoaderCallbacks<Cursor> {

ListView lvData;
DB db;
SimpleCursorAdapter scAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view);

    db = new DB(this);
    db.open();

    String[] from = new String[]{DB.COLUMN_IMG, DB.COLUMN_TXT, DB.COLUMN_IMG2};
    int[] to = new int[]{R.id.list_label, R.id.list_text, R.id.list_star};

    scAdapter = new SimpleCursorAdapter(this, R.layout.list_item, null, from, to, 0);
    lvData = (ListView) findViewById(R.id.list);
    lvData.setAdapter(scAdapter);

    getSupportLoaderManager().initLoader(0, null, this);

    lvData.setOnItemClickListener(mOnListItemClickListener);
}

final Context context = this;

protected void onDestroy() {
    super.onDestroy();
    db.close();
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle bndl) {
    return new MyCursorLoader(this, db);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    scAdapter.swapCursor(cursor);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
}

static class MyCursorLoader extends CursorLoader {

    DB db;

    public MyCursorLoader(Context context, DB db) {
        super(context);
        this.db = db;
    }

    @Override
    public Cursor loadInBackground() {
        Cursor cursor = db.getAllData();
        return cursor;
    }

}

private AdapterView.OnItemClickListener mOnListItemClickListener = new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {

        Cursor cursor = (Cursor) scAdapter.getItem(position);

        switch (v.getId()) {
            case R.id.list_star:
                Toast.makeText(getApplicationContext(), "star", Toast.LENGTH_SHORT).show();

            case R.id.list_label:
                Toast.makeText(getApplicationContext(), "label", Toast.LENGTH_SHORT).show();

        }

        ImageView star = (ImageView) findViewById(R.id.list_star);
        star.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Вы выбрали стихи Агнии Барто", Toast.LENGTH_SHORT).show();
            }
        });

        Intent intent = new Intent(context, ViewPagerActivity.class);
        String pos = Long.toString(position);
        intent.putExtra("pos", pos);
        startActivity(intent);
    }
};

}
plase
  • 361
  • 1
  • 3
  • 9
O К
  • 15
  • 6
  • 2
    If you're new to Android, I wouldn't even bother learning ListView. A few years ago they introduced RecyclerView which is more flexible, easier to understand etc. This example includes a click listener too: https://stackoverflow.com/questions/40584424/simple-android-recyclerview-example – MSpeed Jun 08 '18 at 14:30
  • You can try creating new adapter by extending `SimpleCursorAdapter ` where you can fetch your views by id and add click listener for each of them independently inside `bindView` – Jimmy Jun 08 '18 at 14:37
  • @MikeSpeed thank you, but i need images in the list, and cannot have it in RecyclerView as I understand. – O К Jun 08 '18 at 14:56
  • 1
    I don't know where you got that from, but you can display anything you want in RV – Tim Jun 08 '18 at 15:15

0 Answers0