I want to show the for loop numbers on the listview, but it doesn't work.
App's Layout
public class MainActivity extends AppCompatActivity {
private ListView marksixlist;
private Button mRandombtn, mCleanbtn;
private TextView mText;
private ArrayList<Integer> marksixnum = new ArrayList<Integer>();
private ArrayAdapter arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
marksixlist = (ListView) findViewById(R.id.listview1);
mRandombtn = (Button) findViewById(R.id.randombtn);
mCleanbtn = (Button) findViewById(R.id.cleanbtn);
mText = (TextView) findViewById(R.id.items);
mRandombtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int j = 1; j <= 6; j++) {
int random = (int) (Math.random()* 49+1);
marksixnum.add(random);
Collections.shuffle(marksixnum);
}
}
});
arrayAdapter = new ArrayAdapter<Integer>(this, R.layout.listitempage, marksixnum);
marksixlist.setAdapter(arrayAdapter);
Log.d("aaa", "The markssix is - " + marksixnum);
}
}
Once you generate the random numbers and shuffle them then at the end of for loop add the arrayAdapter to the listview marksixlist again and it will work as a charm.
I have additionally cleared the ArrayList marksixnum before generating random numbers so that your list doesn't keep on growing.
Its optional, if you need last click elements then delete marksixnum.clear()
mRandombtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do this if you do not want
// numbers from last click
marksixnum.clear();
for (int j = 1; j <= 6; j++) {
int random = (int) (Math.random()* 49+1);
marksixnum.add(random);
Collections.shuffle(marksixnum);
}
// add arrayAdapter to listview
marksixlist.setAdapter(arrayAdapter);
}
});
Related
I have created dynamic layouts, but I don't know how to access their information,search on the interent and I got to this code, but it does not enter the first if
Button add, send,delete;
EditText size;
LinearLayout list, list2,list3;
ArrayList<String> text = new ArrayList<>();
Context context;
#SuppressLint("MissingInflatedId")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = findViewById(R.id.add);
size = findViewById(R.id.size);
send = findViewById(R.id.send);
list =findViewById(R.id.list);
list2 =findViewById(R.id.list2);
list3 =findViewById(R.id.list3);
delete = findViewById(R.id.delete);
context = this;
//Toast.makeText(context,""+et.getText().toString(),Toast.LENGTH_SHORT).show();
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("tag","4444444");
for (int i = 0; i<4-list.getChildCount(); i++){
Log.e("tag","555555");
if (list.getChildAt(i) instanceof LinearLayoutCompat) {
Log.d("tag","333333");
LinearLayoutCompat ll = (LinearLayoutCompat) list.getChildAt(i);
for (int j = 0; j < ll.getChildCount() ; j++) {
Log.d("tag","77777");
if(ll.getChildAt(j) instanceof EditText)
{
Log.d("tag","22222222");
EditText et = (EditText) ll.getChildAt(j);
if(et.getId() == R.id.size){
Log.d("tag","1111111");
Toast.makeText(context,""+et.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
}
}
}
}
});
add.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
addView();
}
});
}
what I expected was that in the for it would print the values that I enter in my textView through the toast
I'm trying create an array then generate a random image on ImageView, but my code has a problem...
setBackgroundResource generates an error and the message android studio is Cannot resolve method 'setBackgroundResource(int)' My code is:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button)findViewById(R.id.btn);
final RelativeLayout background = (RelativeLayout) findViewById(R.id.back);
Resources res = getResources();
final TypedArray myImages = res.obtainTypedArray(R.array.myImages);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Random random = new Random();
int randomInt = random.nextInt(myImages.length());
int drawableID = myImages.getResourceId(randomInt, -1);
background.setBackgroundResource(drawableID);
}
});
}
Since you're accessing the array in a different context, you should pull the data out of typed array into a list (or an array) and store it as a member variable.
private List<Integer> myImages;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button)findViewById(R.id.btn);
final RelativeLayout background = (RelativeLayout) findViewById(R.id.back);
myImages = getResourceList(R.array.myImages);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Random random = new Random();
int randomInt = random.nextInt(myImages.size());
int drawableID = myImages.get(randomInt);
background.setBackgroundResource(drawableID);
}
});
}
public List<Integer> getResourceList(int arrayId){
TypedArray ta = getResources().obtainTypedArray(arrayId);
int n = ta.length();
List<Integer> resourceList = new ArrayList<>();
for (int i = 0; i < n; i++) {
int id = ta.getResourceId(i, 0);
resourceList.add(id);
}
ta.recycle();
return resourceList;
}
Basically what I am trying to accomplish is storing editText view Strings in an array and returning the value of a randomly selected array element. The editText views can be dynamically added and removed so I had to take that into account. I've gone over my method and cannot find what I'm getting wrong.
the problem is is getAnswer(). Currently this is crashing the app
Java:
public class MainActivity extends AppCompatActivity {
private LinearLayout mEditTextContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mEditTextContainer = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
setContentView(activity_main);
//Button to choose random editText contents
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getAnswer();
}
});
//Button to add editText Field
final Button add_button = (Button) findViewById(R.id.add_button);
add_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Add_Line();
}
});
//Button to remove editText Field
final Button remove_button = (Button) findViewById(R.id.remove_button);
remove_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Remove_Line();
}
});
}
public void Add_Line() {
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText(null);
et.setHint("Enter Answer #" + (mEditTextContainer.getChildCount()+1));
et.setGravity(Gravity.CENTER);
mEditTextContainer.addView(et);
}
public void Remove_Line() {
int count = mEditTextContainer.getChildCount();
if(count == 2)
return;
else
mEditTextContainer.removeViewAt(mEditTextContainer.getChildCount()-1);
}
public void getAnswer() {
//get number of possible answers
int count = mEditTextContainer.getChildCount();
//create array to hold answers
String[] options = new String[count--];
//create temporary view to store editText view contents
View tempView;
//Loop to collect and store editText contents
for(int i = 1; i < count-1; i++) {
tempView = mEditTextContainer.getChildAt(i);
if(tempView instanceof EditText){
String tempText = ((EditText) tempView).getText().toString();
if(tempText != null){
options[i] = tempText;
}
else
return;
}
}
int number = (int)(Math.random() * count-1);
String answer = options[number];
TextView answerBox = (TextView)findViewById(R.id.textView7);
answerBox.setText(answer);
}
}
For anyone curious, I found the answer. The variable 'count' should not have been decremented by 1 in my method. Changing this fixed the whole thing.
I'm searching for a code that it doesn't need a long .xml and I can easily change number of buttons too 200 or everything.
public class buttons extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buttons);
}
}
in activity file add this:
// create buttons in a loop
for (int i = 0; i < 200; i++) {
Button button = new Button(this);
button.setText("Button " + i);
}
Something like that
public class Buttons extends Activity {
{
Button button;
List<Button> buttonList = new ArrayList<Button>();
LinearLayout.LayoutParams params;
LinearLayout list;
OnClickListener listener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.long);
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
list = (LinearLayout) findViewById(R.id.list);
listener = new OnClickListener() {
#Override
public void onClick(View v) {
int id = v.getId();
Button b = buttonList.get(id);
///
}
};
for (int i = 0; i < 200; i++)
addButton();
}
public void addButton()
{
button = new CheckBox(this);
button.setLayoutParams(params);
button.setText("TEXT");
button.setId(buttonList.size());
button.setOnClickListener(listener);
buttonList.add(button);
list.addView(button);
}
}
I am trying to do something simple: display my ArrayList to a TextView. I have tried to use methods that would do this without success. Or am I supposed to use a ListView instead of a TextView?
Anyway here is the code. I hope someone can help.
public class MainActivity extends Activity {
Button aButton; // Global Scope
TextView text2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_layout);
aButton = (Button) this.findViewById(R.id.button1);
aButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<String> list = new ArrayList<String>();
list.add("Books");
list.add("Newspapers");
list.add("Magazines");
for (int i = 0; i < list.size(); i++) {
//System.out.println(list.get(i));
Log.i("Results", list.get(i));
text2.setText(text2.getText());
}
}
});
}
You are not changing text of TexView:
text2.setText(text2.getText());
You rather thought about:
text2.setText(list.get(i));
eventually:
text2.setText((text2.getText() != null ? text2.getText() : "") + list.get(i));
try this :
aButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<String> list = new ArrayList<String>();
list.add("Books");
list.add("Newspapers");
list.add("Magazines");
String listString = "";
for (String s : list) {
listString += s + " ";
}
text2.setText(listString);
}
});
Your prolem is here
text2.setText(text2.getText())
The working code will be:
public class MainActivity extends Activity {
Button aButton; // Global Scope
TextView text2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_layout);
aButton = (Button) this.findViewById(R.id.button1);
aButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<String> list = new ArrayList<String>();
list.add("Books");
list.add("Newspapers");
list.add("Magazines");
for (int i = 0; i < list.size(); i++) {
//System.out.println(list.get(i));
Log.i("Results", list.get(i));
text2.setText(list.get(i));
}
}
});
}