2017-03-29 35 views
0

我想將oldTextView的功能交換到ID爲nameAgeLabel的TextView。TextView以外的CardView

「nameAgeLabel」位於View視圖之外。 它也應該像現在一樣動態地改變文本,具體取決於當前加載的視圖。

public View getView(final int position, View View, ViewGroup parent) { 


    Log.d("position:",String.valueOf(position)); 
    Log.d("laenge user id",String.valueOf(userIds.size())); 

    String currentUserId = userIds.get(position); 
    LayoutInflater inflater = (LayoutInflater)context.getSystemService 
      (Context.LAYOUT_INFLATER_SERVICE); 

View view = inflater.inflate(R.layout.content_single_mode_vc, parent, false); 

    ImageView imageView = (ImageView) view.findViewById(R.id.offer_image); 
    Bitmap image = userImages.get(currentUserId); 
    imageView.setImageBitmap(image); 


TextView oldTextView= (TextView) view.findViewById(R.id.textView2); 
oldTextView.setText(userNames.get(currentUserId)); 
    //works fine 

    return view; 
} 

我的onCreate看起來是這樣的:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_single_mode_vc); 
    TextView nameAge = (TextView) findViewById(R.id.nameAgeLabel); 

    updateImage(); 
    //(thats another method) 

} 

acvivity_single_mode_vc.xml:

<?xml version="1.0" encoding="utf-8"?> 
<com.daprlabs.aaron.swipedeck.layouts.SwipeFrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:swipedeck="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/swipeLayout" 
    android:orientation="vertical" 
    android:background="@color/white"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    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" 
    /> 
<com.daprlabs.aaron.swipedeck.SwipeDeck 
    android:id="@+id/swipe_deck" 
    android:layout_width="match_parent" 
    android:layout_height="600dp" 
    android:padding="20dp" 
    android:layout_marginTop="60dp" 
    swipedeck:max_visible="3" 
    swipedeck:card_spacing="15dp" 
    swipedeck:swipe_enabled="true" > 

</com.daprlabs.aaron.swipedeck.SwipeDeck> 
<Button 
    android:id="@+id/close" 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:layout_marginBottom="50dp" 
    android:layout_marginLeft="20dp" 
    android:layout_gravity="bottom" 
    android:elevation="1dp" 
    android:background="@drawable/close" 
    android:backgroundTint="@color/light" /> 

<Button 
    android:id="@+id/check" 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:layout_marginBottom="50dp" 
    android:layout_marginRight="20dp" 
    android:layout_gravity="bottom|right" 
    android:background="@drawable/check" 
    android:backgroundTint="@color/light" /> 

<TextView 

    android:layout_width="match_parent" 
    android:padding="20dp" 
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="350dp" 
    android:id="@+id/nameAgeLabel" 
    android:background="@color/separator" 
    /> 
</com.daprlabs.aaron.swipedeck.layouts.SwipeFrameLayout> 

content_single_mode_vc.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/card_view" 
    android:layout_gravity="center" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    card_view:cardCornerRadius="4dp" 
    card_view:cardElevation="4dp" 
    card_view:cardUseCompatPadding="true" 
    android:layout_margin="8dp" 
    android:gravity="center_horizontal" 
    android:padding="25dp" 
    android:clipChildren="true"> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
<ImageView 
     android:id="@+id/offer_image" 
     android:layout_width="match_parent" 
     android:scaleType="centerCrop" 
     android:adjustViewBounds="true" 
     android:layout_height="200dp"/> 
<TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginLeft="137dp" 
     android:layout_marginStart="137dp" 
     android:layout_marginTop="222dp" 
     android:id="@+id/textView2"/> 

</RelativeLayout> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:gravity="center"> 
    <ImageView 
     android:layout_weight="1" 
     android:id="@+id/left_image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/offer_image" 
     android:layout_centerHorizontal="true" /> 
    <ImageView 
     android:layout_weight="1" 
     android:id="@+id/right_image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/offer_image" 
     android:layout_centerHorizontal="true" /> 
    </LinearLayout> 
</LinearLayout> 
</android.support.v7.widget.CardView> 

所以,主要的問題是在哪裏和我如何必須改變textview「nameAge」才能得到與我現在一樣的結果?在我的getView方法不工作,因爲

TextView nameAge= (TextView) view.findViewById(R.id.nameAgeLabel) 

不包括ID爲「nameAgeLabel」的textView。在onCreate中做這件事並沒有多大意義。 在我的getView中創建一個新的視圖也不起作用,因爲我不能返回2個視圖,另外我得到一個「android-requestlayout-incorrectly ..:」消息。

非常感謝。

編輯:類名+構造

public class SwipeDeckAdapter extends BaseAdapter { 
[...] 
public SwipeDeckAdapter(LinkedList<String> data, Context context, LinkedList<String> userIds, Hashtable<String,String> userNames, Hashtable<String,String> userHashtags, Hashtable<String,Bitmap> userImages, Hashtable<String,ArrayList<String>> userAccepted) { 
    this.data = data; 
    this.context = context; 
    this.userIds = userIds; 
    this.userNames = userNames; 
    this.userHashtags = userHashtags; 
    this.userImages = userImages; 
    this.userAccepted = userAccepted; 
} 
} 

回答

0

由於你的描述是不完整的,我嘗試猜測。

SwipeFrameLayout創建一個適配器,您可以從Activity中通過TextView。無論是在構造函數還是單獨的方法。

喜歡的東西

public class CustomSwipeDeckAdapter extends SwipeDeckAdapter { 

    private AppCompatTextView mNameAgeLabel; 

    public SwipeDeckAdapter(List<String> data, Context context, AppCompatTextView textview) { 
     mNameAgeLabel = textView; 
     […] 
    } 

    public View getView(final int position, View View, ViewGroup parent) { 
     […] 
     mNameAgeLabel.setText(); 
    } 
+0

謝謝您的快速響應。是的,我忘了添加我的SwipeDeckAdapter構造函數。我在第一篇文章中編輯它。你想念別的嗎?我嘗試了構造函數,但在修復nullpointerexception之後,因爲mNameAgeLabel爲null,所以用戶名仍然不會顯示在我的mNameAgeLabel中。它可能不是AppCompatTextView中的正確上下文嗎? – matt

+0

我明白了,謝謝!這是正確的提示。 – matt