我有一个ListView,带有TextView和Button。我想从我的CustomAdapter发送数据到.class,但我不知道如何。那是我的CustomAdapter。我可以获得我想要的数据的Log或Toast,但我不知道如何在不使用意图的情况下发送。谢谢 ! :)
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
public MyCustomAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int pos) {
return list.get(pos);
}
@Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable. ->> return list.get(pos).getId();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.listapagos, null);
}
//Handle TextView and display string from your list
final TextView listItemText = (TextView) view.findViewById(R.id.txtPersona);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
Button BtnPago = (Button) view.findViewById(R.id.btn_pago);
final String textbutton = listItemText.getText().toString();
BtnPago.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("BUTTON PRESSED: ",textbutton);
notifyDataSetChanged();
}
});
return view;
}
}
答案 0 :(得分:0)
以下是使用interface作为回调的示例
public static interface OnClickCallBack {
public void onClickItem(int index);
}
public class A implements OnClickCallBack {
MyCustomAdapter myAdapter;
{
myAdapter = new MyCustomAdapter (list, context, this); //set this as the callback for myAdapter
}
@Override
public void onClickItem(int index){
String text = myAdapter.getItem(index);
}
}
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private OnClickCallBack callback;
public MyCustomAdapter(ArrayList<String> list, Context context, OnClickCallBack cb) {
callback = cb;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
....
BtnPago.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callback.onClickItem(position); //invoke callback
notifyDataSetChanged();
}
});
}
答案 1 :(得分:0)
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
public MyCustomAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int pos) {
return list.get(pos);
}
@Override
public long getItemId(int pos) {
return pos;
//just return 0 if your list items do not have an Id variable. ->> return list.get(pos).getId();
}
class ViewHolder{
TextView listItemText;
Button BtnPago;
ViewHolder(View view)
{
listItemText = (TextView) view.findViewById(R.id.txtPersona);
BtnPago = (Button) view.findViewById(R.id.btn_pago);
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if(row == null)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.listapagos, parent,false);
holder = new ViewHolder(row);
row.setTag(holder);
}
else
{
holder = (ViewHolder) row.getTag();
}
holder.listItemText.setText(list.get(position));
holder.BtnPago.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view=(View) v.getParent();
TextView info=(TextView)view.findViewById(R.id.txtPersona)
Intent i=new Intent(context,YourClassName.class);
i.putExtra("info",info.getText().toString());
context.startActivity(i);
}
});
return row;
}
}
public class YourClassName extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
String data=getIntent().getStringExtra("info");
Toast.makeText(this, data, Toast.LENGTH_LONG).show();
}
}
并且不要忘记在AndroidManifest.xml中添加活动
<activity
android:name=".YourClassName"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
答案 2 :(得分:0)
我刚刚解决了它。只需在我的类下面复制适配器,因为它是唯一使用适配器的类。所以,现在我有我班上的数据,我可以按照自己的意愿在数据库中完成所有操作。感谢任何试图帮助我的人。