2014-01-10 72 views
0

我有一個自定義列表適配器填充列表視圖。 我想讓列表視圖中的每個項目都是可點擊的。點擊後,我希望應用加載其他活動並使用適當的數據填充它。數據來自於listing.java的java列表。列表視圖單元格不可點擊

我似乎無法得到它的迴應點擊,這裏是我到目前爲止已經試過:

//this is in the onCreate method 
    final ListView listview = (ListView) findViewById(R.id.listview); 
    listingsAdapter = new ListingsAdapter(this, mylistings); 
    listview.setAdapter(listingsAdapter); 

這是我第一次嘗試(這只是爲了獲得敬酒的工作,但它沒有工作)

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, final View view,int position, long id) { 
     Toast.makeText(getApplicationContext(), 
     "Click ListItem Number " + position, Toast.LENGTH_LONG).show(); 
     }; 
    }); 

我也試過這樣:

listview.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
      Intent i = new Intent(HomeScreenActivity.this, DetailedViewActivity.class); 
      startActivity(i); 
     }; 
    }); 

幫助,將不勝感激。讓我知道如果我應該發佈我的適配器!

這裏是適配器:

public class ListingsAdapter extends BaseAdapter{ 

    List<Listing> listings; 
    Context context; 
    LayoutInflater inflater; 

    public ListingsAdapter(Context context, List<Listing> listings){ 
     this.context = context; 
     this.listings = listings; 
     inflater = (LayoutInflater) context.getSystemService 
       (Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public boolean isEnabled(int position) 
    { 
     return true; 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent){ 
     View _localView = convertView; 
     if (_localView == null){ 
      _localView = inflater.inflate(R.layout.main_cell, parent, false); 
     } 
     TextView text1 = (TextView) _localView.findViewById(R.id.firstline); 
     TextView text2 = (TextView) _localView.findViewById(R.id.secondLine);; 
     Listing listing = listings.get(position); 
     text1.setText(listing.getTitle()); 
     text2.setText(listing.getAddress()); 
     return _localView; 
    } 



    @Override 
    public int getCount(){ 
     // TODO Auto-generated method stub 
     return listings.size(); 
    } 

    @Override 
    public Object getItem(int arg0){ 
     return listings.get(arg0); 
    } 

    @Override 
    public long getItemId(int arg0){ 
     // TODO Auto-generated method stub 
     return arg0; 
    } 
} 

這是main_cell.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="?android:attr/listPreferredItemHeight" 
android:padding="6dp" 
android:background="#CC7C43" 
android:clickable="true" 
android:focusable="true" 
android:focusableInTouchMode="true"> 

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginRight="6dip" 
    android:contentDescription="TODO" 
    android:src="@drawable/ic_launcher" /> 

<TextView 
    android:id="@+id/secondLine" 
    android:layout_width="fill_parent" 
    android:layout_height="26dip" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_toRightOf="@id/icon" 
    android:ellipsize="marquee" 
    android:singleLine="true" 
    android:text="Description" 
    android:textSize="12sp" /> 

<TextView 
    android:id="@+id/firstline" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/secondLine" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:layout_alignWithParentIfMissing="true" 
    android:layout_toRightOf="@id/icon" 
    android:gravity="center_vertical" 
    android:text="Example application" 
    android:textSize="16sp"/> 

</RelativeLayout> 

回答

0

對,所以我患了嚴重的後遺症中恢復,我只是記得我答應闡述在我發佈的鏈接上 - 無視鏈接!

應該將下面的代碼添加到您的ListingsAdapter:

@Override 
public boolean isEnabled(int position) 
{ 
    return true; 
} 

而且你應該在你的main_cell.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="6dp" 
    android:background="#CC7C43" 
    android:longClickable="true"> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="6dp" 
    android:background="#CC7C43"> 

該作品編輯RelativeLayout ,我已經測試過了。

+0

非常感謝哈哈,讓我看看我能做些什麼 – user2886057

+0

sooo你想說明一下嗎? :p – user2886057

+0

是啊對不起,我完全忘了:-)我編輯了我的回答 –

0

嘗試初始化本地實例視野:

@Override 
public View getView(int position, View convertView, ViewGroup parent){ 
    View _localView = convertView; 
    if (_localView == null){ 
     _localView = inflater.inflate(R.layout.main_cell, parent, false); 
    } 
    TextView text1 = (TextView) _localView.findViewById(R.id.firstline); 
    TextView text2 = (TextView) _localView.findViewById(R.id.secondLine);; 
    Listing listing = listings.get(position); 
    text1.setText(listing.getTitle()); 
    text2.setText(listing.getAddress()); 
    return _localView; 
} 

UPD: 還可以修改您的適配器類等必要的方法,因爲你需要獲得一個項目的ID點擊在onItemClick方法使用它:

@Override 
public Object getItem(int arg0){ 
    // TODO Auto-generated method stub 
    listings.get(arg0); 
} 

@Override 
public long getItemId(int arg0){ 
    // TODO Auto-generated method stub 
    return arg0; 
} 

作爲一個例子。希望這會幫助你。

+0

我會在活動或創建方法中執行此操作嗎? – user2886057

+0

@ user2886057在您的「ListingsAdapter」類中。只需創建一個本地對象並處理它,而不是方法的參數。 – infm

+0

不幸的是,這並沒有解決它 – user2886057

相關問題