【问题标题】:ListView does not react to selectionListView 对选择没有反应
【发布时间】:2014-01-17 00:04:05
【问题描述】:

我在一个活动中有一个 ListView,它对被选中做出反应。这可能是一件小事,因为我对 Android 比较陌生,但我自己似乎无法弄清楚。

我的 Activity 中的 onLoad() 加载如下:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.article_list);

    Bundle extras = getIntent().getExtras();
    if(extras != null){
        if(extras.containsKey("categoryId")) categoryId = extras.getLong("categoryId");
    }

    ListView lv = (ListView) findViewById(R.id.article_list_activity);
    iDomsAndroidApp app = ((iDomsAndroidApp) getApplicationContext());

    try {
        objects = app.getDataManager().getArticlesForCategory(categoryId, 15);
        adapter = new ArticleListAdapter(this, R.layout.article_list_cell, objects, categoryId);
        lv.setOnScrollListener(adapter);
        lv.setAdapter(adapter);
    } catch (Exception e){
        Log.e(iDomsAndroidApp.TAG, "Error " + e.getMessage(), e);
    }

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // When clicked, show a toast with the TextView text
            Toast.makeText(getApplicationContext(), objects.get(position).getTitle(),
                    Toast.LENGTH_SHORT).show();


            Intent intent = new Intent(view.getContext(), ArticleActivity.class);
            intent.putExtra("articleId", objects.get(position).getId());
            startActivity(intent);


        }
    });

}

然后在我的适配器(ArrayAdapter)中

public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (view == null) {
            view = inflater.inflate(R.layout.article_list_cell, viewGroup, false);
        }

        Article article = getItem(i);
        TextView titleText = (TextView)view.findViewById(R.id.article_list_titleText);

        try{
        TextView siteText = (TextView)view.findViewById(R.id.article_list_siteText);
        TextView summaryText = (TextView)view.findViewById(R.id.article_list_summaryText);
            RadioButton readBttn = (RadioButton) view.findViewById(R.id.article_list_readButton);
            TextView updatedText = (TextView) view.findViewById(R.id.article_list_updatedText);
            TextView date = (TextView) view.findViewById(R.id.article_cell_date);

            titleText.setText(article.getTitle());
            siteText.setText(article.getSite().getName());
            summaryText.setText(article.getSummary());

            date.setText(DateFormat.getMediumDateFormat(this.getContext()).format(article.getPubDate()));
            if(article.getRead()){
                readBttn.setChecked(false);
            } else {
                readBttn.setChecked(true);
            }

            if(article.getUpdated()){
                updatedText.setVisibility(View.VISIBLE);
            } else {
                updatedText.setVisibility(View.INVISIBLE);
            }

        } catch (Exception e) {
            titleText.setText("Error loading article content!");
        }

        return view;
    }

article_list_cell.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:descendantFocusability="blocksDescendants" >

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Site"
                android:id="@+id/article_list_siteText" android:paddingLeft="10dp"/>

        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Today 12:34"
                android:id="@+id/article_cell_date"
                android:gravity="right"/>
    </LinearLayout>
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="80dp">

        <LinearLayout
                android:orientation="vertical"
                android:layout_width="50dp"
                android:layout_height="fill_parent"
                android:gravity="center">

            <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/article_list_readButton" android:checked="false"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"/>

            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="Updated"
                    android:id="@+id/article_list_updatedText" android:textColor="#1898ff"
                    android:layout_gravity="right"
                    android:visibility="visible"
                    android:textSize="12dp"
                    android:paddingBottom="5dp"
                    android:paddingTop="5dp"/>
        </LinearLayout>

        <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@color/list_background_overlay">
            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="Title"
                    android:id="@+id/article_list_titleText" android:maxLines="1"/>
            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="Summary"
                    android:id="@+id/article_list_summaryText" android:maxLines="3"/>
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 您可以尝试在 xml 中将列表的 id 更改为 android:id/list 并将其获取为 android.R.id.list .. 虽然不知道这是否是一个问题

标签: android listview android-listview


【解决方案1】:

我的猜测是RadioButton 在您单击列表项时获得焦点。

所以添加

android:descendantFocusability="blocksDescendants" 

article_list_cell.xml中的根元素

编辑:

【讨论】:

  • Focusable 和 focusableintouchmode cud 也设置为 false
  • 试过了,但没有什么不同。还禁用了单选按钮而不进行更改。我用 article_list_cell.xml 文件更新了问题
  • @LuukD.Jansen 将检查并恢复
  • 我试过并删除了单选按钮,但这也没有什么不同。
  • @LuukD.Jansen 我尝试了相同的布局,但没有您的代码并且选择有效。尝试评论lv.setOnScrollListener(adapter);
【解决方案2】:

尝试将@Override 添加到您的 onItemClick 方法中

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

【讨论】:

  • 实现接口方法时不允许这样做
【解决方案3】:
l1.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                 // TODO Auto-generated method stub
                 String name=(String)parent.getItemAtPosition(position);

                 Toast.makeText(getBaseContext(), name, Toast.LENGTH_LONG).show();
                  Intent i = new Intent(getBaseContext(),Webview.class);
                  //i.putExtra("USERNAME", name);
                  startActivity(i); 

                }
            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-29
    • 2018-06-09
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    相关资源
    最近更新 更多