我正在开发一个Android应用程序,其中我有一个Activity,用户可以在其中查看所有当前条纹(使用从Android的SQLite数据库访问的数据以编程方式创建)。
我试图在点击另一个活动时传递此数据,该活动显示所有条纹列表中的单个条纹。我需要传递所有4条信息(条纹名称,连胜类别,开始日期和保留日期),而且老实说我不太清楚从哪里开始。下面是我到目前为止,虽然我没有收到错误 - 这当前传递的是null,0或者只是传递给EnlargedActivity.java
。
我目前在EnlargedActivity的onResume()方法中拥有大部分代码,因为无论何时点击一个新按钮,都必须传入新数据 - 虽然我不确定这是否是最好的方法,但是我对Android编程来说还是一个新手。谢谢!
AllStreaks.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_streaks);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
prefs = getSharedPreferences("carter.streakly", Context.MODE_PRIVATE);
editor = prefs.edit();
db = new DatabaseHelper(this);
mTableLayout = (TableLayout) findViewById(R.id.all_streak_table);
Cursor res = db.getAllData();
if(res.getCount() ==0) {
//show message
showMessage("Error", "Nothing found");
return;
}
idList = new ArrayList<>();
streakList = new ArrayList<>();
categoryList = new ArrayList<>();
dateStartedList = new ArrayList<>();
daysKeptList = new ArrayList<>();
buttonList = new ArrayList<>();
int counter = 0;
//StringBuffer buffer = new StringBuffer();
while (res.moveToNext()){
idList.add(res.getString(0));
//buffer.append("STREAKNAME :"+ res.getString(1)+"\n");
//buffer.append("STREAKCATEGORY :"+res.getString(2)+"\n");
//buffer.append("DATESTARTED :"+res.getString(3)+"\n");;
daysKeptList.add(res.getString(4));
streakList.add(res.getString(1));
counter++;
}
LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(200, 200);
btnParams.setMargins(200, 30, 80, 30);
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
tvParams.setMargins(100, 0, 0, 0);
i = 0;
while (i < counter){
if(i%2==0){
mTableRow = new TableRow(this);
mTableLayout.addView(mTableRow);
}
ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
mTableRow.addView(ll);
btn = new Button(this);
btn.setText(daysKeptList.get(i));
btn.setId(i);
btn.setBackground(getResources().getDrawable(R.drawable.round_button));
btn.setLayoutParams(btnParams);
buttonList.add(btn);
ll.addView(btn);
tv = new TextView(this);
tv.setText(streakList.get(i));
tv.setId(i);
tv.setGravity(Gravity.CENTER | Gravity.BOTTOM);
tv.setTextSize(20);
tv.setLayoutParams(tvParams);
ll.addView(tv);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(AllStreaks.this, EnlargedActivity.class);
intent.putExtra("enlargeName", streakList.get(i-1));
startActivity(intent);
}
});
i++;
}
}
public void showMessage(String title, String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}
EnlargedActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enlarged);
prefs = getSharedPreferences("carter.streakly", Context.MODE_PRIVATE);
editor = prefs.edit();
Toast.makeText(EnlargedActivity.this, streakIconName, Toast.LENGTH_LONG).show();
}
public void onResume(){
super.onResume();
db = new DatabaseHelper(this);
res = db.getAllData();
if(res.getCount() ==0) {
//show message
showMessage("Error", "Nothing found");
return;
}
idList = new ArrayList<>();
streakList = new ArrayList<>();
categoryList = new ArrayList<>();
dateStartedList = new ArrayList<>();
daysKeptList = new ArrayList<>();
streakIcon = (TextView)findViewById(R.id.activity_enlarge_icon);
streakIcon.setGravity(Gravity.CENTER);
streakIcon.setTextSize(30);
counter = 0;
while (res.moveToNext()){
idList.add(res.getString(0));
streakList.add(res.getString(1));
categoryList.add(res.getString(2));
dateStartedList.add(res.getString(3));
daysKeptList.add(res.getString(4));
if (streakList.get(counter) == getIntent().getStringExtra("enlargeName")){
streakIconName = streakList.get(counter);
daysKept = Integer.parseInt(daysKeptList.get(counter));
streakIcon.setText(streakIconName + "\n" + daysKept);
}
counter++;
}
}
public void onPause(){
super.onPause();
counter = 0;
idList.clear();
streakList.clear();
categoryList.clear();
dateStartedList.clear();
daysKeptList.clear();
}
public void showMessage(String title, String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}