【问题标题】:How to change view based on unique Id in android如何根据android中的唯一ID更改视图
【发布时间】:2017-05-10 18:15:51
【问题描述】:

我有三种不同的布局。我需要根据唯一标识符在适配器视图中显示。我使用 switch case 对三个不同的视图进行了分类。但是当我运行代码时,它只选择一个布局视图并显示在模拟器上。视图应该根据唯一 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>

【问题讨论】:

  • 请发布完整的代码,而不是未格式化的sn-ps。
  • 我刚刚更新了代码.. 你可以看到 PromotionFeedActivity.java ,PromotionFeedAdapter.java , live_feed_adapter.xml,live_feed_groupon.xml @Ben
  • 那你期待什么?在我看来,这段代码将导致列表视图根据最后一个 PartenerId 显示子视图。
  • 我明白了。您是否尝试同时显示混合子视图?
  • 是的。我想根据 PartnerId 显示三个不同的视图。同时.. @Ben

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


【解决方案1】:

PromotionFeedAdapter::PromotionFeedAdapter 中删除int resource

删除onCreateActivitysetContentView(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 );

,而不是在 Activity 中切换。

尝试使用Map 以获得更简洁的代码。我是手写的,d**n Java 太长了,无法使用Map

【讨论】:

  • 这是有效的.. 但是位置只返回 1 和 0。但是如果我们遍历 PartnerId 列表。它包含更多数量的 id。每个 id 可能是 3,5 或 null。如果我们采取那种情况。它将返回错误的显示格式。 @本
  • @user1918566 已更新
  • 如果我删除其余代码。并用 showData(pfAdapter) 替换它将在三个布局中选择哪个活动。还有另一个问题。 @本
  • @user1918566 我忘了指出你应该在适配器的构造函数中删除layout参数
  • 所以在 promotinfeedadapter 我只需要在构造函数中放置上下文。??
猜你喜欢
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-28
  • 2020-04-22
  • 2019-08-24
相关资源
最近更新 更多