ListView不会对选择做出反应

时间:2014-01-16 15:20:36

标签: android listview android-listview

我在一个活动中有一个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>

3 个答案:

答案 0 :(得分:1)

当您单击列表项时,我的猜测是RadioButton成为焦点。

所以添加

android:descendantFocusability="blocksDescendants" 

article_list_cell.xml

中的根元素

编辑:

enter image description here

答案 1 :(得分:0)

尝试将@Override添加到onItemClick方法

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

答案 2 :(得分:0)

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

                }
            });