1

I'm currently working on developing an Android app, in which I have an Activity where the user can see all of their current streaks (which were created programmatically with data accessed from Android's SQLite database).

I'm trying to then pass this data when clicked into another activity which displays an individual streak from the list of all streaks. I would need to pass all 4 pieces of information (streak name, streak category, date started, and days kept), and I'm honestly not quite sure where to start. Below is what I have so far, and while I'm not getting errors - this is currently passing either null, 0 or simply nothing to EnlargedActivity.java.

I currently have most of my code in EnlargedActivity's onResume() method, since whenever a new button is clicked, new data has to be passed in - although I'm not sure if that's the best way to do this, I'm still pretty new to Android programming. Thanks!

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();
}

}
Carter Klein
  • 103
  • 6
  • Why not make a single Java class with 5 fields and storing that in a single list rather than separately maintaining 5 lists? – OneCricketeer Aug 06 '16 at 01:40
  • Looks like you are trying to implement a Master/Detail flow. A "master" listview that you click, which brings you to the full "detail" page.... https://guides.codepath.com/android/Flexible-User-Interfaces#creating-a-master-detail-flexible-interface – OneCricketeer Aug 06 '16 at 01:44
  • @cricket_007 that's a good idea, but even if I were to make a class - say streak - that held those five fields, how would I be able to access that from both AllStreaks and EnlargedActivity? Wouldn't I have to make a new list in each class either way? – Carter Klein Aug 06 '16 at 02:00
  • Isn't the "enlarged" class designed to only show one "Steak"? – OneCricketeer Aug 06 '16 at 03:06
  • Yes it is, I must just be a bit confused as to what exactly you're recommending I do. I just don't know how I'd get the information from a Streak I created in AllStreaks into EnlargedActivity via button click – Carter Klein Aug 06 '16 at 03:10
  • Assuming you have a `class Steak implements Serializable`, you can use `intent.putSerializable("streak", steakObject)`. Though, just know `Parcelable` is more performant over `Serializable`, but more work is required to implement it. – OneCricketeer Aug 06 '16 at 03:12
  • Oh, and I should have seen that earlier, but you are comparing the strings incorrectly in that code anyways. `streakList.get(counter) == getIntent().getStringExtra...` . See here http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – OneCricketeer Aug 06 '16 at 03:15
  • My issue right now is that when I pass data, for some reason i is always passing the value of the last button I created (in this case 7 since I have 7 buttons) instead of the current value. I'm not sure why, since I'm not incrementing i until after I set the onclicklistener – Carter Klein Aug 06 '16 at 03:24
  • Try to change the i-1 in, intent.putExtra("enlargeName", streakList.get(i-1));, to view.getId(). – i_A_mok Aug 06 '16 at 03:41

0 Answers0