2016-03-29 6 views
-4

毎回別のカードをクリックするたびに、EvsActivityのようなアクティビティが呼び出されます。有効なリストビューへの入れ方

ただし、カードが100であれば、実行可能な解決策ではない100のアクティビティを作成する必要があります。

ので、より良いソリューションを持つ誰PLZウルヘルプがあるcardviewコード

package com.studyleague.app; 
import android.os.Bundle; 
import android.support.v7.widget.GridLayoutManager; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import java.util.ArrayList; 
import java.util.List; 

import butterknife.Bind; 
import butterknife.ButterKnife; 

public class AvailableContent extends TemplateFragment { 
    @Bind(R.id.available_recycler_view) 
    RecyclerView recyclerView; 

    public AvailableContent() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     // Inflate the layout for this fragment 
     View v = inflater.inflate(R.layout.available_content, container, false); 
     ButterKnife.bind(this, v); 
     recyclerView.setHasFixedSize(true); 
     GridLayoutManager glm = new GridLayoutManager(getActivity(), 2, LinearLayoutManager.VERTICAL, false); 
     recyclerView.setLayoutManager(glm); 

     List<AvailableContentModel> content = new ArrayList<>(); 
     for (int i = 0; i < AvailableContentData.acName.length; i++) { 
      content.add(new AvailableContentModel(AvailableContentData.acIcon[i], AvailableContentData.acName[i])); 
     } 

     final AvailableContentAdapter contentAdapter = new AvailableContentAdapter(getActivity(), content); 
     recyclerView.setAdapter(contentAdapter); 



     return v; 

    } 


} 

//アダプタ//

package com.studyleague.app; 

import android.content.Context; 
import android.content.Intent; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.List; 

import butterknife.Bind; 
import butterknife.ButterKnife; 

public class AvailableContentAdapter extends RecyclerView.Adapter<AvailableContentAdapter.ContentViewHolder> { 

    private final List<AvailableContentModel> dataSet; 
    private Context context; 
    public ArrayList<AvailableContentModel> content; 

    public AvailableContentAdapter(Context context, List<AvailableContentModel> content) { 
     this.context = context; 
     this.dataSet = content; 
    } 

    @Override 
    public ContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.available_content_card, parent, false); 
     return new ContentViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(final ContentViewHolder hold, final int listPosition) { 
     hold.acImg.setImageResource(dataSet.get(listPosition).getContentImg()); 
     hold.acText.setText(dataSet.get(listPosition).getContentName()); 
    } 

    @Override 
    public int getItemCount() { 
     return dataSet.size(); 
    } 

    public class ContentViewHolder extends RecyclerView.ViewHolder { 

     @Bind(R.id.available_card_image_view) 
     ImageView acImg; 
     @Bind(R.id.available_card_text_view) 
     TextView acText; 

     public ContentViewHolder(final View view) { 
      super(view); 
      ButterKnife.bind(this, view); 

      view.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        switch (getAdapterPosition()) { 
         case 0: 
          Toast.makeText(v.getContext(), "Mechanics", Toast.LENGTH_SHORT).show(); 
          break; 
         case 1: 
          Toast.makeText(v.getContext(), "B. E. E.", Toast.LENGTH_SHORT).show(); 
          break; 
         case 2: 
          Toast.makeText(v.getContext(), "Maths", Toast.LENGTH_SHORT).show(); 
          break; 
         case 3: 
          Toast.makeText(v.getContext(), "Chemistry", Toast.LENGTH_SHORT).show(); 
          break; 
         case 4: 
          Toast.makeText(v.getContext(), "Physics", Toast.LENGTH_SHORT).show(); 
          break; 
         case 5: 
          context.startActivity(new Intent(context, EvsActivity.class)); 
          break; 
         default: 
          Toast.makeText(v.getContext(), "YOLO", Toast.LENGTH_SHORT).show(); 
          break; 
        } 
       } 
      }); 


     } 
    } 


} 

//アクティビティと

// Recyclerviewを理解されるであろうカードの1つをクリックすると呼び出されます//

package com.studyleague.app; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.ExpandableListView; 
import android.widget.ExpandableListView.OnChildClickListener; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.Collection; 
import java.util.HashMap; 
import java.util.List; 

import butterknife.Bind; 
import butterknife.ButterKnife; 

public class EvsActivity extends AppCompatActivity { 
    @Bind(R.id.exp_list) 
    ExpandableListView expListView; 

    private List<String> chap; 
    private HashMap<String, List<String>> hashMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_evs); 
     ButterKnife.bind(this); 

     // preparing list data 
     prepareListData(); 

     // setting list adapter 
     ExpandableListAdapter listAdapter = new ExpandableListAdapter(this, chap, hashMap); 
     expListView.setAdapter(listAdapter); 

     // expListView on child click listener 
     expListView.setOnChildClickListener(new OnChildClickListener() { 

      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
             int groupPosition, int childPosition, long id) { 
       String groupName = chap.get(groupPosition); 
       String childName = hashMap.get(groupName).get(childPosition); 
       Toast.makeText(getApplication(), groupName + " : " + childName, Toast.LENGTH_SHORT) 
         .show(); 
       Intent i = new Intent(EvsActivity.this, Study.class); 
       startActivity(i); 
       return false; 
      } 
     }); 
    } 

    /* 
    * Preparing the list data 
    */ 
    private void prepareListData() { 

     // Hash map for both header and child 
     hashMap = new HashMap<>(); 

     // Array list for header 
     chap = new ArrayList<>(); 

     // Adding headers to list 
     chap.add("1. Multidisciplinary Nature of Environmental Studies"); 
     chap.add("2. Sustainable Development"); 
     chap.add("3. Environmental Pollution"); 
     chap.add("4. Environmental Legislation"); 
     chap.add("5. Renewable Sources of Energy"); 
     chap.add("6. Environment and Technology"); 
     chap.add("7. Another Chapter"); 
     chap.add("8. Almost Another Chapter"); 

     // Array list for child items 
     List<String> child1 = new ArrayList<>(fillChildData(1, 5)); 
     List<String> child2 = new ArrayList<>(fillChildData(2, 3)); 
     List<String> child3 = new ArrayList<>(fillChildData(3, 11)); 
     List<String> child4 = new ArrayList<>(fillChildData(4, 6)); 
     List<String> child5 = new ArrayList<>(fillChildData(5, 4)); 
     List<String> child6 = new ArrayList<>(fillChildData(6, 2)); 
     List<String> child7 = new ArrayList<>(fillChildData(7, 2)); 
     List<String> child8 = new ArrayList<>(fillChildData(8, 2)); 

     // Adding header and children to hash map 
     hashMap.put(chap.get(0), child1); 
     hashMap.put(chap.get(1), child2); 
     hashMap.put(chap.get(2), child3); 
     hashMap.put(chap.get(3), child4); 
     hashMap.put(chap.get(4), child5); 
     hashMap.put(chap.get(5), child6); 
     hashMap.put(chap.get(6), child7); 
     hashMap.put(chap.get(7), child8); 
    } 

    private Collection<String> fillChildData(int index, int n) { 
     List<String> list = new ArrayList<>(); 
     for (int i = 0; i < n; i++) { 
      list.add("Group " + index + " - Child : " + (i + 1)); 
     } 
     return list; 
    } 
} 
データの

//コードを包み

package com.studyleague.app; 

public class AvailableContentData { 

    static final Integer[] acIcon = { 
      R.mipmap.mec, 
      R.mipmap.elec, 
      R.mipmap.mat, 
      R.mipmap.chem, 
      R.mipmap.ic, 
      R.mipmap.env 
    }; 
    static final String[] acName = { 
      "Mechanics", 
      "B. E. E.", 
      "Maths", 
      "Chemistry", 
      "Physics", 
      "E. V. S." 
    }; 
} 
+1

この質問はかなり不明です。あなたのコードを表示し、あなたが期待している結果とそれがあなたのために起こっていない方法を含めて、あなたが持っている問題を明確に説明する方が良いでしょう。 – NoChinDeluxe

+0

あなたの目標は何ですか?あなたが間違ったアプローチをしている可能性がありますので、私たちが助けることができるようにあなたが望むものを明確にしてください。 – Sam

+0

@NoChinDeluxeとsam私は答えをplzでコードを掲載しています – Ace

答えて

0

が間違ったアカウントでこれを型付き//必要に応じて(カントコメント、申し訳ありません)、私は推測固執しませんでした。 1つのアクティビティを1つのListViewで作成し、CardViewインテントから使用するアダプタを渡します。

バタフライCardview - > BUTTERFLY - > ButterflyAdapter

野球バットCardview - > BASEBALL - > BaseballBatAdatper

あなたの項目が関連していない場合。それらが関連している場合は、同じアダプタを使用しますが、あなたが送信された内容に基づいて別のレイアウトに送信することができます

蝶 - 。> R.layout.butterfly_items

クジラ - > R.layout.whale_items

関連する問題