2014-01-16 58 views
0

我有一個ListView在一個活動中,確實注意到被選中的反應。這可能是一件小事,因爲我對Android比較陌生,但似乎無法自己弄清楚。ListView對選擇沒有反應

我的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> 
+0

你可以嘗試更改列表的id到xml中的android:id/list,並將其作爲android.R.id.list ..雖然不知道是否該問題b問題 –

回答

1

我的猜測是當您點擊列表項目時,RadioButton需要關注。

所以加

android:descendantFocusability="blocksDescendants" 

中根元素article_list_cell.xml

編輯:

enter image description here

+0

焦點和focusableintouchmode cud也設置爲false –

+0

試過了,但沒有什麼區別。同時禁用單選按鈕而不更改。我用article_list_cell.xml文件更新了問題 –

+0

@ LuukD.Jansen將檢查並恢復 – Raghunandan

0

嘗試@Override添加到您的onItemClick方法

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

當實現接口方法時不允許 –

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

       } 
      });