1行目のボタンを押した場合↓
package com.example.listviewtest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.widget.ListView; public class MainActivity extends Activity { private HashMap<String, String> data; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); List<Map<String, String>> dataList = new ArrayList<Map<String, String>>(); Map<String, String> data = new HashMap<String, String>(); data.put("title", "タイトル欄"); data.put("comment", "COMMENT欄"); dataList.add(data); data = new HashMap<String, String>(); data.put("title", "タイトル欄2"); data.put("comment", "COMMENT欄"); dataList.add(data); MySimpleAdapter adapter = new MySimpleAdapter( this, dataList, R.layout.row, new String[] { "title", "comment" }, new int[] { android.R.id.text1, android.R.id.text2 }); ListView listView = (ListView) findViewById(R.id.listView1); listView.setAdapter(adapter);// } }
package com.example.listviewtest; import java.util.HashMap; import java.util.List; import java.util.Map; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.SimpleAdapter; import android.widget.TextView; public class MySimpleAdapter extends SimpleAdapter { // private Context context; private LayoutInflater inflater; private List<? extends Map<String, ?>> listData; public class ViewHolder { TextView line1; TextView line2; } public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); // TODO 自動生成されたコンストラクター・スタブ // this.context = context; this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.listData = data; } public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; // ビューを受け取る View view = convertView; if (view == null) { view = inflater.inflate(R.layout.row, parent, false); // LayoutInflater inflater = (LayoutInflater) // context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); // view = inflater.inflate(R.layout.raw, null); holder = new ViewHolder(); holder.line1 = (TextView) view.findViewById(android.R.id.text1); holder.line2 = (TextView) view.findViewById(android.R.id.text2); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } String text1 = ((HashMap<?, ?>) listData.get(position)).get("title").toString(); String text2 = ((HashMap<?, ?>) listData.get(position)).get("comment").toString(); holder.line1.setText(text1); holder.line2.setText(text2); Button btn = (Button) view.findViewById(R.id.button1); btn.setTag(position); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO 自動生成されたメソッド・スタブ Log.v("buttonクリック", "ポジション: " + position); if(holder.line2.getVisibility() == android.view.View.GONE){ holder.line2.setVisibility(View.VISIBLE); }else holder.line2.setVisibility(View.GONE); } }); return view; } }main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>row.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@android:id/text1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="TextView" android:textSize="18dp" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout> <TextView android:id="@android:id/text2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" > </TextView> </LinearLayout>
0 件のコメント:
コメントを投稿