我有一个包含多个片段的活动.Activity最初有片段,里面有两个按钮.单击此按钮后,我必须用新片段替换片段.每个片段都有各种小部件,并将当前片段替换为各种事件.
这是我的问题.我怎样才能做到这一点.
建议我的想法.
我试图获得textview的顶部和左边缘,我给了硬编码的边距.
为什么我这样做?因为我想循环遍历所有的文本视图,所以我放置在这个文本视图之下,这样我就可以在这些精确的边距上编程添加radiobutton组.
textView[0] = new TextView(context);
relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
textView[0].setId(1); // changed id from 0 to 1
textView[0].setText(naam[0].toUpperCase());
relativeLayoutParams.setMargins(24, 39, 0, 0);
int tyo = relativeLayoutParams.getTop(); //Trying to get top margins.
Run Code Online (Sandbox Code Playgroud) 我的完整代码在这里给出.但没有显示活动.添加clickListener()时显示空指针异常.
我如何访问使用片段膨胀的按钮
//无法添加完整代码.显示添加更多细节.//
public class Activity extends Activity{
private static final String KEY_SUCCESS="success";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
Fragment fragment=new PlaceholderFragment();
transaction.add(R.id.container,fragment);
transaction.addToBackStack("welcome");
transaction.commit();
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
Button loginButton;
private String userNameString;
private String passwordString;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
rootView = inflater.inflate(R.layout.fragment_welcome, container, false);
try{
loginButton= …Run Code Online (Sandbox Code Playgroud) 我试图从sqlite中的表中获取所有项目,并需要在列表视图中显示它.
这是我获取物品的代码
public List<MenuData> getMenuItem(){
SQLiteDatabase db;
Cursor cursor=null;
List<MenuData> menuList = new ArrayList<MenuData>();
db=getReadableDatabase();
String query ="SELECT * from "+TABLE_NAME_MENU;
try{
cursor = db.rawQuery (query, null );
}
catch (NullPointerException e){
Log.e("Error","Null Pointer Exception");
}
if ( cursor.moveToFirst()) {
do {
MenuData menuData= new MenuData();
menuData.setKEY_ITEM_NAME(cursor.getString(cursor.getColumnIndex(KEY_ITEM_NAME)));
menuData.setKEY_ITEM_CATEGORY(cursor.getString(cursor.getColumnIndex(KEY_ITEM_CATEGORY)));
menuData.setKEY_ITEM_CONTENTS(cursor.getString(cursor.getColumnIndex(KEY_ITEM_CONTENTS)));
menuData.setKEY_ITEM_TYPE(cursor.getString(cursor.getColumnIndex(KEY_ITEM_TYPE)));
menuData.setKEY_PRICE(cursor.getString(cursor.getColumnIndex(KEY_PRICE)));
menuList.add(menuData);
} while (cursor.moveToNext());
}
return menuList;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我得到了表格最后一行的结果menuList,它在表格中有相同的行数.因此列表视图具有所有相同的项目.
请帮我!首先请遵循此算法:
btn_set1单击1- ,其功能正在执行.
2- btn_set2被点击时,"文本1"是的setText编入"+"!
为什么???请帮帮我,我该怎样解决这个问题?谢谢.
public void onClick(View view) {
switch (view.getId()){
//time picker pannel
case R.id.btn_set1:
text1.setText(aTime);
intent1 = new Intent(this, alarmService.class);
intent1.putExtra("rq1", req_code);
intent1.putExtra("alarm_time", alarm_time1);
startService(intent1);
alinear.setAnimation(animLeft);
Linear.setVisibility(View.GONE);
text2.setVisibility(View.VISIBLE);
break;
case R.id.btn_set2:
text2.setText(aTime);
intent2 = new Intent(this, alarmService.class);
intent2.putExtra("rq2", req_code);
intent2.putExtra("alarm_time", alarm_time1);
startService(intent2);
LLinear.setAnimation(animLeft);
Linear.setVisibility(View.GONE);
case R.id.btn_cancle1:
Linear.setAnimation(animLeft);
Linear.setVisibility(View.GONE);
cancel_one(text1);
break;
case R.id.btn_cancle2:
Linear.setAnimation(animLeft);
Linear.setVisibility(View.GONE);
cancel_one(text2);
break;
}
}
private void cancel_one(TextView textView) {
if(intent1 != null)
stopService(intent1);
if(intent2 != null)
stopService(intent2);
textView.setText("+");
}
Run Code Online (Sandbox Code Playgroud)