在我的项目中,我从一个活动到另一个ListActivity.now得到一个字符串我想把这个字符串带到我的适配器class.how我可以这样做.. 下面是我的代码,在这里我想将fromMobileno字符串转换为我的适配器类
public class MainActivity extends ListActivity {
ListView lv;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_contact);
cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
startManagingCursor(cursor);
String[] from={ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone._ID};
int[] to={R.id.name_entry,R.id.number_entry};
adapterlistadapter=new adapter(this,R.layout.single_row1,cursor,from,to);
setListAdapter(listadapter);
lv=getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Bundle extras = getIntent().getExtras();
String fromMobileno = extras.getString("KEY");
}
@Override()
public long getSelectedItemId() {
// TODO Auto-generated method stub
return super.getSelectedItemId();
}
@Override
public int getSelectedItemPosition() {
// TODO Auto-generated method stub
return super.getSelectedItemPosition();
}
class adapter extends SimpleCursorAdapter implements Filterable {
private Context context;
private int layout;
public adapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.context=context;
this.layout=layout;
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View v, Context context, Cursor c) {
....
}
}
答案 0 :(得分:0)
我想把这个字符串带到我的适配器类。我可以这样做吗
再向adapter
添加一个String参数,以便从类中获取所需的值:
String strfromMobileno="";
public adapter(Context context, int layout, Cursor c,
String[] from, int[] to,String fromMobileno) {
super(context, layout, c, from, to);
///... your code here
strfromMobileno=fromMobileno;
}
和onCreate
方法在创建adapter
类对象之前从intent获取值。
另一种方法是因为adapter
是MainActivity
类的内部类,所以你也可以在内部class.declare fromMobileno
中访问外部类变量作为类级别变量而不是内部{{ 1}}方法然后使用onCreate
访问MainActivity.this.fromMobileno
类
fromMobileno