2017-05-25 68 views
-1

當前我正在使用庫來顯示類似卡的Tinder滑動功能(here),但我對卡沒有顯示有點問題。我一定要檢查我的getCount和初始化,但仍然不顯示。我不確定我做錯了什麼,我想我需要一雙新的眼睛來看這個。在這裏呆了好幾天。謝謝。getView()未在ArrayAdapter中調用

app_bar_main.xml

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.jobforhire.mobile.CardSliderActivity"> 

<android.support.design.widget.AppBarLayout 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" > 

     <TextView 
      android:id="@+id/toolbar_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="JobForHire" 
      android:textColor="@android:color/white" 
      android:textSize="24sp" 
      android:textStyle="bold" /> 

     </android.support.v7.widget.Toolbar> 
</android.support.design.widget.AppBarLayout> 

<include layout="@layout/activity_job_list_container"/> 

</android.support.design.widget.CoordinatorLayout> 

activity_job_list_container.xml

<LinearLayout android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" 
tools:context="com.jobforhire.mobile.CardSliderActivity" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

    <link.fls.swipestack.SwipeStack 
    android:id="@+id/swipeStack" 
    android:layout_width="320dp" 
    android:layout_height="240dp" 
    android:padding="32dp"/> 

</LinearLayout> 

activity_job_list_card_view.xml

<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_margin="8dp" 
    android:gravity="center_horizontal" 
    android:padding="25dp" 
    card_view:cardBackgroundColor="@color/cardview_light_background" 
    card_view:cardCornerRadius="4dp" 
    card_view:cardElevation="4dp" 
    card_view:cardUseCompatPadding="true"> 

     <TextView 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:card_view="http://schemas.android.com/apk/res-auto" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:id="@+id/title" 
      android:text="Hello"/> 

</android.support.v7.widget.CardView> 

CardSliderActivity.java

private int cardCount = 2; 
private SwipeDeck cardStack; 
private String token; 
private ArrayList<String> testData; 
private ArrayList<DataJobCards> testJsonData = new ArrayList<>(); 
private ArrayList<DataPreference> dataPreferenceArrayList = new ArrayList<>(); 
private ArrayList<DataPreference> dataPreferenceArrayListContainer = new ArrayList<>(); 
private SwipeDeckAdapter adapter; 

/** 
* Inflate a card deck and insert card into the deck 
*/ 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    new RetrieveCards(this,1).execute(); 

    setContentView(R.layout.activity_main); 

    Intent intent = getIntent(); 
    token = intent.getStringExtra("access_token"); 

    new PrefUserAsync(this,this,token).execute(); 

    Fabric.with(this, new Answers()); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    //toolbar.setTitle(R.string.card_title); 
    setSupportActionBar(toolbar); 

    //getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    //getSupportActionBar().setCustomView(R.layout.custom_toolbar); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 

    getSupportActionBar().setDisplayShowTitleEnabled(false); 

    cardStack = (SwipeDeck) findViewById(R.id.swipe_deck); 
} 

@Override 
public void PassCards(ArrayList<DataJobCards> arrayList) { 

    this.testJsonData = arrayList; 
    adapter = new SwipeDeckAdapter(testData,testJsonData,cardStack,this); 
    if(cardStack != null) { 
     cardStack.setAdapter(adapter); 
     Log.d("Check adapter ", "is !null"); 
    } 
    else{ 
     Log.d("Check adapter ", "is null"); 
    } 
} 

CardDeckAdapter.java

public class CardDeckAdapter extends ArrayAdapter<DataJobCards> { 

private Context context; 
private ArrayList<Integer> randomImage = new ArrayList<>(); 
private ArrayList<DataJobCards> cardList; 
private LayoutInflater inflater; 

public CardDeckAdapter(Context context, ArrayList<DataJobCards> apps) { 
    super(context,0,apps); 
    this.cardList = apps; 
    this.context = context; 

    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    Log.d("Adapter ", "Executed"); 
    for(int i = 0; i < cardList.size(); i++){ 
     Log.d("Check ", cardList.get(i).getJobTitle()); 
    } 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder holder; 
    Log.d("getView ", "Executed"); 

    if (convertView == null) { 
     convertView = inflater.inflate(R.layout.activity_job_list_card_view, parent,false); 
     holder = new ViewHolder(); 
     holder.name = (TextView) convertView.findViewById(R.id.title); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    DataJobCards dataJobCard = getItem(position); 

    holder.name.setText(dataJobCard.getJobTitle()); 

    return convertView; 
} 

@Override 
public int getCount() { 
    return cardList.size(); 
} 

@Override 
public DataJobCards getItem(int position) { 
    return cardList.get(position); 

} 

@Override 
public long getItemId(int position) { 
    return 0; 
} 

public static class ViewHolder { 
    TextView name; 
} 
} 

編輯 仍與此沒有任何進展,所以我寄予了更多的一點點代碼從我的主類,這是CardSliderActivity.java以防萬一我的主課有什麼與它有關。

+0

你確定你使用的圖書館是你鏈接的圖書館嗎? 它沒有任何關係,它似乎是一個,而不是:https://github.com/flschweiger/SwipeStack 您能否更新您的問題,並讓您提供準確的數據,以幫助我們? – Eselfar

+0

我很抱歉。昨天晚上,當我發佈這個消息時,我感到非常疲倦,可能會感到困惑。我已經更新了它。 –

+0

修剪你的代碼到最低限度。如果到那時還沒有解決問題,請發佈較小的代碼。這是太多的代碼。 –

回答

0

我只能猜測,因爲我看不到在你的xml中定義的SwipeStack視圖。您的swipeStack對象可能爲空,因此未設置適配器。你能證實嗎?

+0

對不起,這是一個複製粘貼錯誤。 SwipeStack在容器下。我已經更新了它。 –

+0

我試着運行你的應用程序,它似乎對我有用。我只能再次推測,因爲我看不到你的整個活動代碼。你可以確保arrayList在你的活動中沒有被清除嗎?由於您的活動和適配器共享列表的同一個實例,因此在「活動」中清除它也會將其清除到適配器中。 – tompee

0

您在寫給我們代碼時寫了activity_job_list_view.xml作爲文件名。但稍後,在getView方法中,您將膨脹R.layout.activity_job_list_card_view。它可能會讓你對這些名字感到困惑嗎?或者它只是你在stackoverflow上製作的一個錯字?

+0

對不起。這是一個錯字。這裏真的很晚,我很累。我已經更新了它。 –

+0

好吧,那麼......現在我找不到更多的錯誤。對不起。 – Claff

+0

沒關係。感謝你付出的努力 :) –

相關問題