处理listview中的点击

时间:2018-06-08 14:26:36

标签: android listview onitemclicklistener

我是Android的新手,需要我的ListView帮助,其中包含每个列表项中的文本视图和图像。我需要单独处理列表项和列表项中的图标(R.id.list_star)上的点击。我尝试了几种方法,但似乎我不能自己做。我试图将star.setOnItemClickListener放在lvData.setOnItemClickListener附近的开头 - 但是在这种情况下找不到视图,并在AdapterView.OnItemClickListener内添加开关 - 不起作用。我的代码只处理列表项单击但没有图标点击

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

}

0 个答案:

没有答案