0

I have a ListView in an activity which does note react to being selected. This could be a small thing as I am relatively new to Android, but don't seem to be able to figure it out myself.

My onLoad() in my Activity loads like this:

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


        }
    });

}

Then in my Adapter (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>
Luuk D. Jansen
  • 4,402
  • 8
  • 47
  • 90

3 Answers3

1

My guess is RadioButton takes focus when you click list item.

So add

android:descendantFocusability="blocksDescendants" 

to root element in article_list_cell.xml

Edit:

enter image description here

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Focusable and focusableintouchmode cud also be set to false – Rat-a-tat-a-tat Ratatouille Jan 16 '14 at 15:36
  • Tried that, but doesn't make a difference. Also disabled the radio button without change. I updated the question with the article_list_cell.xml file – Luuk D. Jansen Jan 16 '14 at 15:46
  • @LuukD.Jansen will check and revert back – Raghunandan Jan 16 '14 at 15:49
  • I tried and removed the radio button, but that doesn't make a difference either. – Luuk D. Jansen Jan 16 '14 at 15:52
  • @LuukD.Jansen i tried the same layout but without your code and the selection works. try commenting `lv.setOnScrollListener(adapter);` – Raghunandan Jan 16 '14 at 15:54
  • @LuukD.Jansen i tried i do get the test on click of listview row. tried it with my suggestion and it works – Raghunandan Jan 16 '14 at 16:04
  • @LuukD.Jansen also tried without `android:descendantFocusability="blocksDescendants"` does not work. so my suggestion stands. try commenting `lv.setOnScrollListener(adapter);` if it helps – Raghunandan Jan 16 '14 at 16:06
  • You are right. Re moving the scroll listener fixes it... any suggestions what that could be the reason for that? I am only getting a feeling for Android at the moment while porting an App. – Luuk D. Jansen Jan 16 '14 at 17:07
  • @LuukD.Jansen its a scrolllistener and you need to implement the scrolllistener interface. http://developer.android.com/reference/android/widget/AbsListView.OnScrollListener.html. but you had `lv.setOnScrollListener(adapter)`. Anyway by suggestion regarding the radio button taking focus still stands – Raghunandan Jan 16 '14 at 17:08
  • ok, I will have a look. I implemented the scroll listener in the adapter, to keep the code together. Tried moving it to the Activity, but doesn't make a difference. Will do a bit of research or post another question. – Luuk D. Jansen Jan 16 '14 at 17:12
  • @LuukD.Jansen http://stackoverflow.com/questions/14774121/load-more-data-when-scrollview-is-scrolled-to-bottom – Raghunandan Jan 16 '14 at 17:14
  • @LuukD.Jansen http://stackoverflow.com/questions/1080811/android-endless-list. this is the most voted answer of scroll listener on stackoverflow – Raghunandan Jan 16 '14 at 17:17
0

Try to add @Override to your onItemClick method

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {}
M. Bongini
  • 122
  • 6
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); 

                }
            });
learner
  • 3,092
  • 2
  • 21
  • 33