如果用户点击列表项,是否可以将其定向到包含多个TextViews的cardsViews的单独布局?

时间:2018-12-31 09:15:53

标签: android listview android-cardview

嘿,我已经在我的android报价应用程序中初始化了列表视图。现在,我想当用户将例如生活报价录入磁带时,生活的所有报价应分别显示在cardView上。我不知道如何在每张cardView上显示每个报价,请引导我进入正确的道路,我将非常感激。

这是我去所有其他活动的报价活动

public class QoutesActivity extends AppCompatActivity {

private ListView listView;

// Listview Adapter
ArrayAdapter<String> adapter;


String[] listItems = {

        "Happiness Quotes",
        "Attitude Quotes",
        "Anniversary Quotes",
        "Life Quotes",
        "Alone Quotes",
        "Strength Quotes",
        "Patriotism Quotes" ,
        "Hope Quotes" ,
        "Inspirational Quotes" ,
        "Independence Quotes" ,
        "Childhood Quotes" ,
        "Trust Quotes",
        "Selfie Quotes",
        "Birthday Quotes" ,
        "Knowledge Quotes" ,
        "Love Quotes" ,
        "Nature Quotes"
};

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_qoutes);

    listView =  findViewById(R.id.list_view);


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(QoutesActivity.this,
            android.R.layout.simple_list_item_1,listItems) {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Get the current item from ListView
            View view = super.getView(position, convertView, parent);

            // Get the Layout Parameters for ListView Current Item View
            ViewGroup.LayoutParams params = view.getLayoutParams();

            // Set the height of the Item View
            params.height = 180;
            view.setLayoutParams(params);

            return view;

        }

    };
    listView.setAdapter(adapter);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String itemValue = (String) listView.getItemAtPosition(position);

            if (position == 0) {
                //code specific to first list item
                Intent myIntent = new Intent(view.getContext(), HappinessActivity.class);

                startActivityForResult(myIntent, 0);
            }else if (position == 1){

                Intent myIntent = new Intent(view.getContext(), AttitudeActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 2){
                Intent myIntent = new Intent(view.getContext(), AnniversaryActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 3){
                Intent myIntent = new Intent(view.getContext(), LifeActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 4){
                Intent myIntent = new Intent(view.getContext(), AloneActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 5){
                Intent myIntent = new Intent(view.getContext(), StrengthActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 6){
                Intent myIntent = new Intent(view.getContext(), PatriotismActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 7){
                Intent myIntent = new Intent(view.getContext(), HopeActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 8){
                Intent myIntent = new Intent(view.getContext(), InspitationalActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 9){
                Intent myIntent = new Intent(view.getContext(), IndependenceActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 10){
                Intent myIntent = new Intent(view.getContext(), ChildhoodActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 11){
                Intent myIntent = new Intent(view.getContext(), TrustActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 12){
                Intent myIntent = new Intent(view.getContext(), SelfieActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 13){
                Intent myIntent = new Intent(view.getContext(), BirthdayActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 14){
                Intent myIntent = new Intent(view.getContext(), KnowledgeActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 15){
                Intent myIntent = new Intent(view.getContext(), LoveActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 16){
                Intent myIntent = new Intent(view.getContext(), NatureActivity.class);
                startActivityForResult(myIntent, 0);
            }
        }
    });

}

}

这是xml文件

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layercontainer"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg3">

<ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

1 个答案:

答案 0 :(得分:0)

以我的诚实观点,如果有太多if语句基本上可以验证立场,我会考虑替换。您会看到,我不会说仅仅因为它有效就能这样做是不对的,这是确保您的主要需求正常运行的重要部分。但是,如果您可以缩短用于它的代码行,那就更好了!这正是区分不同程序员的属性的原因。从不同角度看问题的人。我建议您替换更可扩展且可用于将来的功能的主要原因是,如果您想在该代码中进行更改,则只需要修改几行,而不是50多行(请相信我) ,随着您的应用程序变得越来越复杂,它会变得越来越困难)。

话虽如此,进行一个新的活动(命名任何东西),在这种情况下,我将其命名为 userChoiceActivity

您当前的活动:

 @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String itemValue = (String) listView.getItemAtPosition(position);
            Intent intent = new Intent(view.getContext(), userChoiceActivity.class);
            intent.putExtra("userChoice", itemValue);
            startActivity(intent);
}

下一步,在您的 userChoiceActivity 类中,您需要在 onCreate 方法中接收这些值。

String userChoice;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        userChoice = null;
    } else {
        userChoice = extras.getString("userChoice");
    }
} else {
    userChoice = (String) savedInstanceState.getSerializable("userChoice");
}

现在,您应该在第二个活动( userChoiceActivity )中的userChoice变量中拥有用户从listView中选取的内容。在这里,您可以使用多个 if 语句来确定将用于填充recyclerview的阵列列表。对于recyclerview,Internet上有无尽的教程,但是您可以尝试使用以下内容:-

Android RecyclerView and CardView Tutorial