如何根据android中的唯一ID更改视图

时间:2017-05-10 18:15:52

标签: android android-layout listview android-fragments android-arrayadapter

我有三种不同的布局。我需要在基于唯一Identifier的适配器视图中显示。我使用switch case对三个不同的视图进行了分类。但是,当我运行代码时,它只选择一个布局视图并在模拟器上显示。视图应该基于以下内容进行更改独特的身份。

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>

1 个答案:

答案 0 :(得分:0)

int resource删除PromotionFeedAdapter::PromotionFeedAdapter

删除setContentView(R.layout.activity_promotions_feed); onCreate ActivitypfAdapter = 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

,而不是切换活动。

尝试使用Map来获得更干净的代码。我是手写的,而且Java使用for(var i=0; i<4; i++){ document.getElementsByClassName("moves")[0].getElementsByTagName("BUTTON")[i].onclick = alert(document.getElementsByClassName("moves")[0].document.getElementsByClassName("move")[i].textContent); } 太长了。