AndroidのListView上の項目でのsetVisibilityについて質問させてください。
やりたいことは、「XML上でvisibilityをgoneにしている項目を、特定の条件のときだけvisibleにしたい」ということです。
以下、サンプルコードです。
MainActivity.java
public class MainActivity extends AppCompatActivity { // 名前 String[] mName = new String[] { "osomatsu", "karamatsu", "choromatsu", "itimatsu", "jushimatsu", "todomatsu" }; // 得点 String[] mScore = new String[] { "60", "40", "30", "100", "20", "80" }; private SimpleAdapter sa; private ListView lv; ArrayList<HashMap<String, String>> data; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // ArrayList data = new ArrayList<HashMap<String, String>>(); // データ格納 for(int i = 0; i< 6; i++){ HashMap<String, String> map = new HashMap<String, String>(); map.put("name", mName[i]); map.put("score", mScore[i]); if (mScore[i].equals("100")) { map.put("result", "GREAT!"); } else { map.put("result", "SO-SO"); } data.add(map); } /* * 作成したdataとカスタマイズしたレイアウトrow.xmlを * 紐付けたSimpleAdapterを作成する */ sa = new SimpleAdapter(this, data, R.layout.row, new String[]{"name", "score", "result"}, new int[]{R.id.name, R.id.score, R.id.result} ); // main.xmlのListViewにsaをセットします。 lv = (ListView)findViewById(R.id.listview); lv.setAdapter(sa); setVisibility(); } public void setVisibility() { for(int i=0; i<data.size(); i++) { View v = lv.getAdapter().getView(i, null, lv); TextView tvScore = (TextView) v.findViewById(R.id.score); TextView tvResult = (TextView) v.findViewById(R.id.result); String tempScore = tvScore.getText().toString(); if (tempScore.equals("100")) { Log.v("MYTEST", "pass here"); tvResult.setVisibility(View.VISIBLE); } } } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="60dip" android:orientation="horizontal" > <TextView android:id="@+id/name" android:layout_width="100dip" android:layout_height="wrap_content" /> <TextView android:id="@+id/score" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/result" android:visibility="gone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" /> </LinearLayout>
自分が試しているコードは上記のような感じで、setVisibility()の中で、visibilityをいじりたいTextViewをfindViewByIdで取ってきて、setVisibilityでVISIBLEにしたら表示されると思ったのですが、if文の中は通っているのですが表示されませんでした。。
何か根本的な理解が間違っているのでしょうか。。
表示/非表示の切り替え方をご存知の方、何卒ご教授のほどお願いいたします。
ベストアンサーのyonaさんに教えて頂き、解決できました。
同様の問題の解決策を探している方のために、私が修正したコードを載せておこうと思います。
MainActivity.java
Java
1public class MainActivity extends AppCompatActivity { 2 // 名前 3 String[] mName = new String[] { 4 "osomatsu", "karamatsu", "choromatsu", "itimatsu", "jushimatsu", "todomatsu" 5 }; 6 // 得点 7 String[] mScore = new String[] { 8 "60", "40", "30", "100", "20", "80" 9 }; 10 private CustomAdapter ca; 11 private ListView lv; 12 private ArrayList<HashMap<String, String>> data; 13 private Activity act; 14 @Override 15 public void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.main); 18 19 // ArrayList 20 data 21 = new ArrayList<HashMap<String, String>>(); 22 23 // データ格納 24 for(int i = 0; i< 6; i++){ 25 HashMap<String, String> map 26 = new HashMap<String, String>(); 27 28 map.put("name", mName[i]); 29 map.put("score", mScore[i]); 30 if (mScore[i].equals("100")) { 31 map.put("result", "GREAT!"); 32 } else { 33 map.put("result", "SO-SO"); 34 } 35 data.add(map); 36 } 37 /* 38 * 作成したdataとカスタマイズしたレイアウトrow.xmlを 39 * 紐付けたSimpleAdapterを作成する 40 */ 41 ca 42 = new CustomAdapter(this 43 , data, R.layout.row, 44 new String[]{"name", "score", "result"}, 45 new int[]{R.id.name, R.id.score, R.id.result} 46 ); 47 // main.xmlのListViewにcaをセットします。 48 lv = (ListView)findViewById(R.id.listview); 49 lv.setAdapter(ca); 50 changeData(); // 後から値を変えてみるテスト 51 } 52 53 // 後から値を変えてみるテスト 54 public void changeData() { 55 HashMap<String, String> mMap 56 = new HashMap<String, String>(); 57 mMap = data.get(2); 58 mMap.put("score", "100"); 59 mMap.put("result", "GREAT!!"); 60 data.set(2, mMap); 61 ca.notifyDataSetChanged(); 62 } 63} 64 65class CustomAdapter extends SimpleAdapter { 66 LayoutInflater mLayoutInflater; 67 Context mContext; 68 69 public CustomAdapter(Context context, 70 List<? extends Map<String, ?>> data, int resource, 71 String[] from, int[] to) { 72 super(context, data, resource, from, to); 73 mContext = context; 74 } 75 76 @Override 77 public View getView(int position, View convertView, ViewGroup parent) { 78 mLayoutInflater = LayoutInflater.from(mContext); 79 convertView = mLayoutInflater.inflate(R.layout.row, parent, false); 80 ListView listView = (ListView)parent; 81 // get data 82 Map<String, Object> data = (Map<String, Object>)listView.getItemAtPosition(position); 83 // set items 84 TextView name = (TextView)convertView.findViewById(R.id.name); 85 name.setText((String)data.get("name")); 86 TextView score = (TextView)convertView.findViewById(R.id.score); 87 score.setText((String)data.get("score")); 88 String myScore = score.getText().toString(); 89 TextView result = (TextView)convertView.findViewById(R.id.result); 90 result.setText((String)data.get("result")); 91 // edit visibility 92 if (myScore.equals("100")) { 93 result.setVisibility(View.VISIBLE); 94 } 95 return convertView; 96 } 97} 98
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2015/12/05 14:26