listview.setOnItemClickListener无法使用CursorAdapter工作

时间:2017-07-02 02:58:58

标签: java android listview

当我点击列表视图的项目列表时,我正在尝试显示Toast。

我的列表视图效果很好,但问题是单击列表视图的项目后不会执行任何操作。

这是我的代码。

MainActivity.java

lv.setAdapter(adapter);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(MainActivity.this, "Test", Toast.LENGTH_SHORT).show();
    }
});

ClientCursorAdapter .java

class ClientCursorAdapter extends CursorAdapter {
    Context context;
    public ClientCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
        this.context = context;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.row_layout,viewGroup,false);
        return v;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        TextView title = view.findViewById(R.id.Title);
        TextView artist = view.findViewById(R.id.Artist);
        ToggleButton music = view.findViewById(R.id.minus_one);
        ToggleButton favorite = view.findViewById(R.id.favorite);

        title.setText(cursor.getString(cursor.getColumnIndex("TITLE")));
        artist.setText(cursor.getString(cursor.getColumnIndex("ARTIST")));

    }
}

2 个答案:

答案 0 :(得分:1)

尝试设置按钮(或您要处理的任何其他视图在列表项内单击),如下所示:

android:focusable="false"
android:focusableInTouchMode="false"

参考:https://stackoverflow.com/a/6703671/1084174

答案 1 :(得分:0)

如果您的列表视图行包含可点击或可聚焦的视图项,则不会调用status = error name = PropertiesConfig property.filename = logs appenders = console, rolling appender.console.type = Console appender.console.name = STDOUT appender.console.layout.type = PatternLayout appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n appender.rolling.type = RollingFile appender.rolling.name = RollingFile appender.rolling.fileName = ${filename}/AndrewDebug-${date:yyyy-MM-dd}.log appender.rolling.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz appender.rolling.layout.type = PatternLayout appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n appender.rolling.policies.type = Policies appender.rolling.policies.time.type = TimeBasedTriggeringPolicy appender.rolling.policies.time.interval = 1 appender.rolling.policies.time.modulate = true appender.rolling.policies.size.type = SizeBasedTriggeringPolicy appender.rolling.policies.size.size=10MB appender.rolling.strategy.type = DefaultRolloverStrategy appender.rolling.strategy.max = 20 logger.rolling.name = web.languages logger.rolling.level = debug logger.rolling.appenderRef.rolling.ref = RollingFile rootLogger.level = debug rootLogger.appenderRefs = stdout rootLogger.appenderRef.stdout.ref = STDOUT 。您可以将onItemClick设置为您的布局。如,

android:descendantFocusability="blocksDescendants"

或将<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:descendantFocusability="blocksDescendants"> </LinearLayout> 设置为您的商品,例如ImageButton。