This question already has an answer here:
This function is to create buttons programmatically according to the number of goals. I created them successfully however i want to pass the goal_id assigned when a certain button is clicked to another activity.
FOR EXAMPLE: if the user created 3 goals
button 1 : goal 1
button 2 : goal 2
button 3 : goal 3
assuming the user clicked BUTTON 3, it will get the goal_id of Goal 3 and passed it to another activity. <-- This i don't know how.
VARIABLES:
private Button b;
private LinearLayout goals;
private ArrayList<Integer> goalidArray;
public TaskListActivity task;
public int tempHolder;
//database variables
MyDBAdapter dbhandler;
OnCreate Function
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_goals);
goals = (LinearLayout)findViewById(R.id.goalList);
task = new TaskListActivity();
goalidArray = new ArrayList<Integer>();
//create database
dbhandler = new MyDBAdapter(this);
back(); // to go back button
displayAllGoals();
}
public void displayAllGoals(){
//get a list of all goals
List<Goals> allGoals = dbhandler.getAllGoals();
for (Goals goal : allGoals){
b = new Button(this);
b.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
b.setText(goal.getGoalName());
b.setId(goal.getGoalId());
b.setTextColor(Color.BLACK);
//adding it on the array
goalidArray.add(goal.getGoalId());
goals.addView(b);
// when button is clicked
b.setOnClickListener(new View.OnClickListener() {
int clickedid = b.getId();
int i = 0;
int tempid;
//get total no of goals
long goal_counts = dbhandler.getGoalsCount();
@Override
public void onClick(View v) {
while( i < goal_counts-1){
if(goalidArray.get(i) == clickedid){
tempid = i;
Intent myIntent = new Intent(ViewGoalsActivity.this, TaskListActivity.class);
String temp = Integer.toString(goalidArray.get(tempid));
MessageTo.message(ViewGoalsActivity.this,temp); // its like a TOAST, to see if i was able to get the goal id
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString("goalid",temp);
//Add the bundle to the intent
myIntent.putExtras(bundle);
startActivity(myIntent);
}else
i++;
}
}
});
}}
It will go error when i clicked a button. The emulator lags and appears to be stuck.
TO THE ONE WHO POST THIS AS A DUPLICATE didn't even read the whole thing. This is different from the others.
Aucun commentaire:
Enregistrer un commentaire