0

私は3つのレイアウトがあります。私はユニークな識別子に基づいてアダプタビューで表示する必要があります。私はswitch case.Butを使用して3つの異なるビューを分類しました。私はコードを実行すると、1つだけのレイアウトビューを選択し、エミュレータで表示していました。ユニークなID。androidの一意のIDに基づいてビューを変更する方法

public class PromotionsFeedActivity extends AppCompatActivity implements View.OnClickListener{ 


    JSONObject jsonObject=new JSONObject(); 
    public static ArrayList<String> Id=new ArrayList<String>(); 

    public static ArrayList<String> Titles = new ArrayList<String>(); 
    public static ArrayList<String> Title2 = new ArrayList<String>(); 
    public static ArrayList<String> Categorie = new ArrayList<String>(); 
    public static ArrayList<String> videoURL=new ArrayList<String>(); 
    public static ArrayList<String> Pictures=new ArrayList <String>(); 

    public static ArrayList<String> PartnerId=new ArrayList<String>(); 
    public static ArrayList<String> BuyUrl; 



    public static ArrayAdapter<String> pfAdapter; 
    ListView show; 
    VideoView videoView; 
    ImageView btnWatchVideo; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_promotions_feed); 

//  Id=new ArrayList<String>(); 
//  Titles=new ArrayList<String>(); 
//  Title2=new ArrayList<String>(); 
//  Categorie=new ArrayList<String>(); 
//  videoURL=new ArrayList<String>(); 
//  Pictures=new ArrayList<String>(); 
//  PartnerId=new ArrayList<String>(); 
//  BuyUrl=new ArrayList<String>(); 

     btnWatchVideo = (ImageView) findViewById(R.id.btnWatchVideo); 
     btnWatchVideo.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(PromotionsFeedActivity.this,MediaActivity.class)); 
      } 
     }); 

     for(int i=0; i < PartnerId.size(); i++){ 
      Log.i("PartnerIds",(PromotionsFeedActivity.PartnerId.get(i))); 
      Log.i("PartnerSize",String.valueOf(PartnerId.size())); 
      String id = PromotionsFeedActivity.PartnerId.get(i); 

      switch(id){ 

       case "5": Log.i("Partner:",id); 
        Log.i("PartnerSize",String.valueOf(PartnerId.size())); 
        show = (ListView) findViewById(R.id.lv_pf); 
        pfAdapter = new PromotionFeedAdapter(PromotionsFeedActivity.this, R.layout.livefeed_adapter, Title2); 
        pfAdapter = new PromotionFeedAdapter(PromotionsFeedActivity.this, R.layout.livefeed_adapter, Titles); 
        showData(pfAdapter); 
        break; 
       case "3": Log.i("Partner:",id); 
        Log.i("PartnerSize",String.valueOf(PartnerId.size())); 
        show = (ListView) findViewById(R.id.lv_pf); 
        pfAdapter = new PromotionFeedAdapter(PromotionsFeedActivity.this, R.layout.livefeed_adapter_groupon, Title2); 
        pfAdapter = new PromotionFeedAdapter(PromotionsFeedActivity.this, R.layout.livefeed_adapter_groupon, Titles); 
        showData(pfAdapter); 
        break; 
       case "null":Log.i("Partner:",id); 
        Log.i("PartnerSize",String.valueOf(PartnerId.size())); 
        show = (ListView) findViewById(R.id.lv_pf); 
        pfAdapter = new PromotionFeedAdapter(PromotionsFeedActivity.this, R.layout.livefeed_adapter_news, Title2); 
        pfAdapter = new PromotionFeedAdapter(PromotionsFeedActivity.this, R.layout.livefeed_adapter_news, Titles); 
        showData(pfAdapter); 
        break; 
      } 
      Log.i("Last",id); 
     } 
    } 
    public void showData(ArrayAdapter<String> adapter){ 
     adapter.notifyDataSetChanged(); 
     show.setAdapter(adapter); 
    } 

PromotionFeedAdapter.java

public class PromotionFeedAdapter extends ArrayAdapter<String> { 
    public int layout; 
    Button getCal; 
    public static String MY_PREFS_NAME="my_prefs"; 

    public PromotionFeedAdapter(Context context, int resource, List<String> objects) { 
     super(context, resource, objects); 
     layout=resource; 
    } 
    Context mContext; 

    @Override 
    public View getView(final int position, View convertView, final ViewGroup parent){ 
     ViewHolder2 mainViewHolder=null; 
     mContext = parent.getContext(); 
     LayoutInflater inflater=LayoutInflater.from(getContext()); 
     convertView= inflater.inflate(layout,parent,false); 
     final ViewHolder2 viewHolder=new ViewHolder2(); 

     viewHolder.title=(TextView)convertView.findViewById(R.id.pf_title); 
     viewHolder.title2=(TextView)convertView.findViewById(R.id.pf_title2); 
     viewHolder.category=(TextView)convertView.findViewById(R.id.pf_category); 
     viewHolder.picture=(ImageView)convertView.findViewById(R.id.pf_main_pic); 
     viewHolder.title.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       getBusinessProfile(); 
      } 
     }); 
     convertView.setTag(viewHolder); 
     mainViewHolder=(ViewHolder2)convertView.getTag(); 
     mainViewHolder.title.setText(getItem(position)); 
     String title2,picUrl,category; 
     picUrl=PromotionsFeedActivity.Pictures.get(position); 
     category=PromotionsFeedActivity.Categorie.get(position); 
     convertView.setTag(viewHolder); 

     URL newurl = null; 
     try { 
      newurl = new URL(picUrl); 
      Picasso.with(getContext()).load(picUrl).into(viewHolder.picture); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     return convertView; 
    } 


    public class ViewHolder2 { 
     TextView id; 
     TextView title; 
     TextView title2; 
     ImageView picture; 
     TextView category; 
    } 

activity_promotion_feed.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 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.example.arunmannuru.arun.PromotionsFeedActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="1" 
     android:orientation="horizontal"> 

     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/farbinder_logo" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="40dp" 
     android:weightSum="1" 
     android:orientation="horizontal"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="30dp" 
     android:src="@drawable/pin" 
     android:layout_marginTop="10dp" 
     android:layout_marginLeft="20dp" /> 
    <TextView 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:text="My Location" 
     android:textSize="20dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginTop="12dp"/> 

     <ImageView 
      android:id="@+id/btnWatchVideo" 
      android:layout_width="145dp" 
      android:layout_height="38dp" 
      android:layout_marginTop="5dp" 
      android:src="@drawable/watch_video_dark" 
      android:layout_weight="0.16" /> 

    </LinearLayout> 

    <ListView 
     android:layout_marginTop="85dp" 
     android:layout_width="match_parent" 
     android:layout_height="415dp" 
     android:id="@+id/lv_pf"> 

    </ListView> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_gravity="bottom" 
     android:padding="@dimen/bottom_navigation_padding"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/specials" 
      android:drawableTop="@drawable/specials" 
      android:layout_weight="1" 
      android:onClick="ShowSpecials" 
      android:gravity="center_horizontal"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/calendar" 
      android:drawableTop="@drawable/calendar" 
      android:layout_weight="1" 
      android:onClick="ShowCalendar" 
      android:gravity="center_horizontal"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/search" 
      android:onClick="ShowSearch" 
      android:drawableTop="@drawable/search" 
      android:layout_weight="1" 
      android:gravity="center_horizontal"/> 

     <TextView 
      android:id="@+id/btnComunity" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/community" 
      android:drawableTop="@drawable/community" 
      android:layout_weight="1" 
      android:onClick="ShowCommunity" 
      android:gravity="center_horizontal"/> 


     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/profile" 
      android:drawableTop="@drawable/profile" 
      android:layout_weight="1" 
      android:onClick="ShowProfile" 
      android:gravity="center_horizontal"/> 

    </LinearLayout> 

</FrameLayout> 

livefeed_adapter.xml

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


    <LinearLayout 
     android:layout_width="380dp" 
     android:layout_height="50dp" 
     android:layout_marginLeft="2dp" 
     android:src="@drawable/img_frame_1" 
     android:orientation="horizontal" 
     android:background="@drawable/boarder"> 

     <ImageView 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:src="@drawable/special_icon" 
      android:layout_marginTop="5dp" 
      android:layout_marginLeft="5dp" /> 
     <TextView 
      android:layout_width="330dp" 
      android:layout_height="wrap_content" 
      android:text="$80 Toward Gourmet Stackhouse" 
      android:layout_marginTop="16dp" 
      android:id="@+id/pf_title" 
      android:textStyle="bold" 
      android:textSize="16sp" /> 

    </LinearLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="200dp"> 
     <ImageView 
      android:layout_width="370dp" 
      android:layout_height="220dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:id="@+id/pf_main_pic" 
      android:src="@color/colorPrimaryDark" /> 

     <ImageView 
      android:layout_width="65dp" 
      android:layout_height="65dp" 
      android:src="@drawable/special_tag" 
      android:layout_alignRight="@+id/pf_main_pic" 
      android:layout_alignEnd="@+id/pf_main_pic" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Artistic Expert Picture Framing" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp" 
      android:textStyle="bold" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Retail" 
      android:textStyle="normal" 
      android:layout_marginTop="20dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp"/> 

    </RelativeLayout> 


    </LinearLayout> 

livefeed_adapter_groupon.xml

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


    <LinearLayout 
     android:layout_width="380dp" 
     android:layout_height="50dp" 
     android:layout_marginLeft="2dp" 
     android:src="@drawable/img_frame_1" 
     android:orientation="horizontal" 
     android:background="@drawable/boarder"> 

     <ImageView 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:src="@drawable/special_icon" 
      android:layout_marginTop="5dp" 
      android:layout_marginLeft="5dp" /> 
     <TextView 
      android:layout_width="330dp" 
      android:layout_height="wrap_content" 
      android:text="$80 Toward Gourmet Stackhouse" 
      android:layout_marginTop="16dp" 
      android:id="@+id/pf_title" 
      android:textStyle="bold" 
      android:textSize="16sp" /> 

    </LinearLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="200dp"> 
     <ImageView 
      android:layout_width="370dp" 
      android:layout_height="220dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:id="@+id/pf_main_pic" 
      android:src="@color/colorPrimaryDark" /> 

     <ImageView 
      android:layout_width="65dp" 
      android:layout_height="65dp" 
      android:src="@drawable/special_tag" 
      android:layout_alignRight="@+id/pf_main_pic" 
      android:layout_alignEnd="@+id/pf_main_pic" /> 
     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/powered_by_groupon" 
      android:layout_marginTop="173dp" 
      android:layout_marginLeft="240dp"/> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Artistic Expert Picture Framing" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp" 
      android:textStyle="bold" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Retail" 
      android:textStyle="normal" 
      android:layout_marginTop="20dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp"/> 

    </RelativeLayout> 



</LinearLayout> 
+0

フォーマットされていないスニペットではなく完全なコードを投稿してください。 – Ben

+0

私はコードを更新しました。PromotionFeedActivity.java、PromotionFeedAdapter.java、live_feed_adapter.xml、live_feed_groupon.xml @Ben – user1918566

+0

あなたは何を期待していますか?私の考えでは、このコードは最後の 'PartenerId'に従ったサブビューを示すリストビューにつながります。 – Ben

答えて

0

int resourcePromotionFeedAdapter::PromotionFeedAdapterから削除してください。

はあなた ActivityonCreatesetContentView(R.layout.activity_promotions_feed);以下のすべてのコードを削除し、その後、あなたのアダプタを変更

pfAdapter = new PromotionFeedAdapter(PromotionsFeedActivity.this, Title2); 
showData(pfAdapter); 

に置き換えます。

getView

switch(PromotionsFeedActivity.PartnerId.get(position)){ 
    case "5":layout=R.layout.livefeed_adapter;break; 
    case "3":layout=R.layout.livefeed_adapter_groupon;break; 
    case "null":layout=R.layout.livefeed_adapter_news;break; 
} 
convertView= inflater.inflate(layout,parent,false); 

、代わりに活性の切り替え。

クリーナーコードにはMapをお試しください。私は手書きで、Javaは長すぎてMapを使用することはできません。

+0

これは機能していました。 1と0です。ただし、PartnerIdのリストを繰り返し処理すると、ID番号の数が増えます。それぞれのIDは3,5またはnullです。このシナリオを取ると、間違った表示形式が返されます。 @Ben – user1918566

+0

@ user1918566 – Ben

+0

を更新しました。残りのコードを削除してください。また、showData(pfAdapter)の代わりに3つのレイアウトの中から選択してください。別の問題があります。 @Ben – user1918566

関連する問題