onListItemClick()不工作的ListView在Android的 [英] onListItemClick() not working for ListView in Android

查看:91
本文介绍了onListItemClick()不工作的ListView在Android的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面将不会在点击注册吐司。事实上,它甚至不似乎检测点击。这CustomListTitle扩展父类是ListActivity,所以一切都应该进行设置,但它不工作。没有错误,只是没有注册的点击。

The following will not register the Toast upon clicking. In fact, it doesn't even seem to detect a click. The parent class that CustomListTitle extends is ListActivity, so everything should be set, but it's not working. No errors, just no registering a click.

package com.mtp;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class AvoidForeclosure extends CustomListTitle {

 // create variables
 private DbAdapter db;
 private SimpleCursorAdapter adapter;
 private ListView list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // set to main layout
        setContentView(R.layout.main);

        // set title
        this.title.setText("Avoid Foreclosure");

        // open new DB connection
        db = new DbAdapter(this);
        db.open();

        // Get all the clients and manage the cursor
        Cursor c = db.fetchAllClients();
        startManagingCursor(c);

        String[] from = new String[] { DbAdapter.KEY_NAME };

        int[] to = new int[] { R.id.text1 };

        // Create an array adapter and set it to display using our row
        adapter = new SimpleCursorAdapter(this, R.layout.clientsrow, c, from, to);

        // Set the adapter
        setListAdapter(adapter);

        // get the list and set some data
        list = getListView();

        list.setTextFilterEnabled(true);

        list.setClickable(true);

 }

    @Override
    protected void onListItemClick(ListView list, View v, int position, long id) {
     // TODO Auto-generated method stub

    // get the selection then attempt to make a Toast
     String selection = list.getItemAtPosition(position).toString();
     Toast.makeText(this, selection, Toast.LENGTH_LONG).show();

      // Comment these out for now until I get a working Toast.
      //Intent intent = new Intent();
  //intent.setClass(v.getContext(), CurrentMarketValue.class);
  //intent.putExtra("clientId", clientData.getInt(0));
  //startActivity(intent);

     super.onListItemClick(list, v, position, id);
    }


}

这是我的主要布局:

And here's my main layout:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/widget55"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:descendantFocusability="blocksDescendants"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Choose a current client:"
    android:textStyle="bold"
    android:layout_margin="3dip"
    />


   <ListView
   android:id="@android:id/list"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:descendantFocusability="blocksDescendants"
   />

   <TextView android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="There are no Clients yet."
        />


</LinearLayout>

和这里的ListView的使用来填充我的clientsrow布局:

And here's my clientsrow layout that the ListView uses to populate:

  <?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FF0081D3"
    android:padding="10dip"
    android:textStyle="bold"
    android:inputType="textCapWords"
    android:capitalize="words"
    android:focusable="false"
    />

任何想法?谢谢!我如此坚持,并在最后期限。

Any ideas? Thank you! I'm so stuck and on a deadline.

推荐答案

尝试摆脱的机器人:descendantFocusability =blocksDescendants(两次出现)和机器人:可调焦=假,这两者都不是可取的,你已经设置了结构。另外,我不知道你为什么叫 list.setClickable(真); ,因为这是不需要的。

Try getting rid of android:descendantFocusability="blocksDescendants" (both occurrences) and android:focusable="false", neither of which are desirable for the structure you have set up. Also, I do not know why you are calling list.setClickable(true);, as that is not needed.

这篇关于onListItemClick()不工作的ListView在Android的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆