2017-05-25 70 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

没关系。感谢你付出的努力 :) –

相关问题