これは私にとって完全に機能しています
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.graphics.Color;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView listview;
List<Map<String,Object>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prepareData();
listview =new ListView(this);
MySimpleAdapter adapter=new MySimpleAdapter(this,data,R.layout.activity_item,new String[] {"uu"},new int[]{R.id.showTv});
listview.setAdapter(adapter);
setContentView(listview);
}
private void prepareData(){
data=new ArrayList<Map<String,Object>>();
Map<String,Object> item;
item=new HashMap<String,Object>();
item.put("din", "hello");
data.add(item);
item=new HashMap<String,Object>();
item.put("bin", "world");
data.add(item);
item=new HashMap<String,Object>();
item.put("tin", "rock");
data.add(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
アダプタ クラス
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MySimpleAdapter extends SimpleAdapter {
private final Context context;
private List<Map<String, Object>> data;
private int resource;
private String[] from;
private int[] to;
public MySimpleAdapter(Context context,
List<? extends Map<String, ?>> data, int resource, String[] from,int[] to) {
super(context, data, resource, from, to);
this.context=context;
this.data=(List<Map<String, Object>>) data;
this.resource=resource;
this.from=from;
this.to=to;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View rowView = inflater.inflate(resource, null, true);
Map<String,Object> medMap = data.get(position);
final TextView[] showTv = new TextView[from.length];
Button btn1 = (Button)rowView.findViewById(R.id.gointoBt);
Button.OnClickListener mOkOnClickListener = new Button.OnClickListener()
{
public void onClick(View v) {
Toast.makeText(context, "my pos " + "button1 "+ position, Toast.LENGTH_LONG).show();
}
};
btn1.setOnClickListener(mOkOnClickListener);
Button btn2=(Button)rowView.findViewById(R.id.chooseBt);
Button.OnClickListener mOkOnClickListener2 = new Button.OnClickListener()
{
public void onClick(View v) {
Toast.makeText(context, "my pos " + "button2 "+ position, Toast.LENGTH_LONG).show();
}
};
btn2.setOnClickListener(mOkOnClickListener2);
return rowView;
}
}
楽しむ..