package org.techtown.challenge09;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
ListView listView;
SingerAdapter adapter;
EditText Name;
EditText Start;
EditText End;
Button button;
int[] result = new int[24];
int[] Time;
int click=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Name = findViewById(R.id.editText1);
Start = findViewById(R.id.editText2);
End = findViewById(R.id.editText3);
adapter = new SingerAdapter();
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = Name.getText().toString();
String start = Start.getText().toString() + ":00";
String end = End.getText().toString() + ":00";
adapter.addItem(new SingerItem(name, start, end, R.drawable.human));
int First = Integer.parseInt(Start.getText().toString());
int Last = Integer.parseInt(End.getText().toString());
for(int i=First; i<Last; i++) {
result[i] = 1;
}
click=1;
}
});
listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SingerItem item = (SingerItem) adapter.getItem(position);
Toast.makeText(getApplicationContext(), "선택 : " + item.getName(),
Toast.LENGTH_LONG).show();
}
});
if(click == 1) {
int num=0, num2=0;
for(int i=0; i<=23; i++) {
if(result[i] == 1) {
num++;
}
}
Time = new int[num-1];
for(int i=0; i<=23; i++) {
if(result[i] == 1) {
Time[num2]=i;
num2++;
}
}
button = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), Arrays.toString(Time), Toast.LENGTH_LONG).show();
}
});
}
button = findViewById(R.id.exit);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
public class SingerAdapter extends BaseAdapter {
ArrayList<SingerItem> items = new ArrayList<SingerItem>();
@Override
public int getCount() {
return items.size();
}
public void addItem(SingerItem item) {
items.add(item);
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
SingerItemView view = new SingerItemView(getApplicationContext());
SingerItem item = items.get(position);
view.setName(item.getName());
view.setStart(item.getStart());
view.setEnd(item.getEnd());
view.setImage(item.getResId());
return view;
}
}
}
当我单击“ button2”(单击“ button”之后)时,不会弹出Toast消息。有什么问题吗?
我想问题是在button.setOnClickListener()中'click'变量的更改不适用于整个更改。 如何才能更改button.setOnClickListener()中'click'变量中的更改应用于全部?