2012-02-08 66 views
0

我陷入了奇怪的問題,我無法點擊我的列表視圖...我已經以同樣的方式執行它,我以前的方式,但事情是它不工作。啓用ListView選擇 - Android

listTags.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       listTags.setSelection(position); 
       Toast.makeText(getParent(), "hello", Toast.LENGTH_LONG).show(); 
      } 
     }); 

我擴展Activity類

和聽到的是我如何申報的ListView

listTags = (ListView) viewToLoad.findViewById(R.id.listPack); 

聽到的是什麼,我的確在XML

<ListView 
     android:id="@+id/listPack" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_marginBottom="5dip" 
     android:layout_marginLeft="5dip" 
     android:layout_marginRight="5dip" 
     android:layout_weight="1" > 
    </ListView> 

這僅僅是正常的到處都是,我無法弄清楚你的錯誤,請幫助我。

謝謝

聽到代碼適配器

adapter = new KeywordAdapter(getApplicationContext(), id, 
       getLNApplication().getKeyworddetail()); 
listTags.setAdapter(adapter); 

我KeywordAdapter類

public class KeywordAdapter extends BaseAdapter { 

    public KeywordAdapter(Context context, int id, ArrayList<ArrayList<Keyword>> keywordList) { 
     this.context = context; 
     if (id >= keywordList.size()) { 
      keyWordList = new ArrayList<Keyword>(); 
     } else 
      keyWordList = keywordList.get(id); 
    } 

    // Implemented methods for BaseAdpter 

    public class ViewHolder { 
     TextView tagName; 
     //.... more code 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     View view = convertView; 

     if (view == null) { 
      LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = inflater.inflate(R.layout.package_tag_details, null, true); 
      holder = new ViewHolder(); 
      holder.tagName = (TextView) view.findViewById(R.id.tagName); 
      //.... more code 
      holder.layout = (LinearLayout) view 
        .findViewById(R.id.linearLayout1); 
      view.setTag(holder); 
     } else { 
      holder = (ViewHolder) view.getTag(); 
     } 

     holder.tagName.setText(keyWordList.get(position).getName()); 
     ArrayList<Integer> rank = keyWordList.get(position).getRank(); 


     @SuppressWarnings("unused") 

     holder.tagRank1.setText(rank.get(position)); 


     //.... more code 

     return view; 
    } 

    public void forceReload() { 
     notifyDataSetChanged(); 

    } 
} 
+0

我認爲您的列表項選擇了Toast的上下文中的問題 – 2012-02-08 10:55:35

回答

1

什麼是你放置在ListView中的項目,使所有項目爲android:focusable="false"

+0

您的意思是在定製的項目查看 – 2012-02-08 11:44:59

+0

yeh在custum視圖中的項目..假設你使用TextView使它成爲android:focusable =「false」 – AndroidDev 2012-02-08 11:46:03

+0

好吧,讓我試試這個... – 2012-02-08 11:46:40

1

只要改變你的代碼如下。

listTags = (ListView)findViewById(R.id.listPack); 

你確定android:layout_width支持「match_parent」嗎?請看看控制檯。 這可以幫助你。

+0

列表顯示完美,但出錯了是我沒有點擊事件不叫 – 2012-02-08 11:38:54

0

我沒有看到你設置適配器,即listTags.setAdapter(?);

Try this code (works for me): 

    // MainActivity 

    public class MainActivity extends Activity { 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      final ListView listTags = (ListView) findViewById(R.id.listPack); 
      listTags.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, mStrings)); 
      listTags.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 
        listTags.setSelection(position); 
    //    Toast.makeText(getParent(), "hello", Toast.LENGTH_LONG).show(); 
       } 
      }); 



     } 


     private String[] mStrings = { 

      "Comte", "Coolea", "Cooleney", "Coquetdale", "Corleggy", "Cornish Pepper", 
      "Cotherstone", "Cotija", "Cottage Cheese", "Cottage Cheese (Australian)", 
      "Cougar Gold", "Coulommiers", "Coverdale", "Crayeux de Roncq", "Cream Cheese", 
      "Cream Havarti", "Crema Agria", "Crema Mexicana", "Creme Fraiche", "Crescenza", 
      "Croghan", "Crottin de Chavignol", "Crottin du Chavignol", "Crowdie", "Crowley", 
      "Cuajada", "Curd", "Cure Nantais", "Curworthy", "Cwmtawe Pecorino", 
      }; 

    // Main xml 

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello" /> 

     <ListView 
      android:id="@+id/listPack" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_marginBottom="5dip" 
      android:layout_marginLeft="5dip" 
      android:layout_marginRight="5dip" 
      android:layout_weight="1" > 
     </ListView> 

      </LinearLayout> 

    // list_item.xml // layout 

    <?xml version="1.0" encoding="utf-8"?> 
    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="10dp" 
     android:textSize="16sp" > 
    </TextView> 
0

有一個問題列表視圖 的自定義視圖我用我刪除該視圖中水平滾動視圖現在問題得到解決

+0

的scrollView內,但如果你從其他視圖中刪除了android:focusable =「false」我沒有認爲它將起作用 – AndroidDev 2012-02-08 11:55:12

+0

不,它正在工作......在我使用過的任何列表視圖中,我還沒有使用android:focusable =「false」。 – 2012-02-08 12:01:48

+0

你在你的視圖中有什麼樣的視圖 – 2012-02-08 12:03:19