I am developing an application, that sends a string array with putStringArrayListExtra. In the second activity I am receiving. The problem, if I send 2 item, 2 activity will open. The second element is on the listview(shows just one), when i click back, there is now the first item. I want the 2 item in the SAME listview. What am I doing wrong?
First activity
final int len = thumbnailsselection.length;
int cnt = 0;
String selectImages = "";
// String result = "";
String ar="";
for (int i =0; i<len; i++)
{
if (thumbnailsselection[i])
{
cnt++;
ar = nev[i];
selectImages = nev[i] + "|";
// List<String> myList = new ArrayList<String>(Arrays.asList(selectImages));
ArrayList<String> arr = new ArrayList<String>();
arr.add(selectImages);
Intent intent = new Intent(Gallery.this,Upload.class);
intent.putStringArrayListExtra("array_list", arr);
startActivity(intent);
// Log.i("adat","galleri:" + arr);
}
}
if (cnt == 0){
Toast.makeText(getApplicationContext(),
"Please select at least one image",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"You've selected Total " + cnt + " image(s).",
Toast.LENGTH_LONG).show();
}
}
});
Second activity
ListView listView;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listView = (ListView) findViewById(R.id.list);
Bundle b = getIntent().getExtras();
// ArrayList<String> arr = (ArrayList<String>)b.getStringArrayList("array_list");
ArrayList<String> arr = b.getStringArrayList("array_list");
Log.i("adat","UPLOAD:"+arr);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, arr);
listView.setAdapter(adapter);
}
Do like below while you are creating new arraylist everytime when you loop thats why you are not getting correct values.
And also take you intent outside the loop too.
ArrayList<String> arr = new ArrayList<String>();
for (int i =0; i<len; i++){
if (thumbnailsselection[i]){
cnt++;
ar = nev[i];
selectImages = nev[i] + "|";
arr.add(selectImages);
}
}
Intent intent = new Intent(Gallery.this,Upload.class);
intent.putStringArrayListExtra("array_list", arr);
startActivity(intent);
Related
I'm searching to get a little useful option to my app, the basics are simple, I have a list of questions (in listView) when we click in the question, we have another activity with details of questions and the answer that is setting ( true or false), if the answer is right, I want to color the question in the listview in green, red if false:
My code to generate questions:
public void showQuest(View view){
ModelHelper md = new ModelHelper(this);
db = openHelper.getWritableDatabase();
Spinner s = (Spinner) findViewById(R.id.spn_ques);
Integer para = 2;
para = Integer.parseInt( s.getSelectedItem().toString()) ;
ArrayList<String> lstQ = md.getOnlyQuestions(para);
ArrayAdapter lstad = new ArrayAdapter(this, android.R.layout.simple_list_item_1,lstQ);
lst.setAdapter(lstad);
The listView OnclickListener:
String quest = (String) parent.getAdapter().getItem(i);
int pos = lst.getCheckedItemPosition();
Intent inte = new Intent(view.getContext(),Reponse.class);
cursor = db.rawQuery("SELECT * FROM "+ ModelHelper.TABLE_QUESTION + " WHERE " + ModelHelper.KEY_QUESTION + " = ? ",new String[] {quest});
if (cursor != null)
if (cursor.getCount() > 0) {
cursor.moveToFirst();
final Intent intent = getIntent();
String matr= intent.getStringExtra("id");
String repA = cursor.getString(cursor.getColumnIndex(ModelHelper.KEY_PROFIL_WAITEDANSWER));
String cible = cursor.getString(cursor.getColumnIndex(ModelHelper.KEY_PROFILE));
String plan = cursor.getString(cursor.getColumnIndex(ModelHelper.KEY_PLAN));
int ID = cursor.getInt(cursor.getColumnIndex(ModelHelper.KEY_ID_QUESTION));
inte.putExtra("Question", quest);
inte.putExtra("answ", repA);
inte.putExtra("ID", ID);
inte.putExtra("pos", pos);
inte.putExtra("position", i);
inte.putExtra("matr", matr);
Toast.makeText(new.this, "" + ID, Toast.LENGTH_SHORT).show();
startActivity(inte);
}
}
});
The answer is selected in another activity, in the first one I have only the questions' list.
Thanks.
When you are rendering the item in the listview set the background color based on the logic you described.
Hello fellow stackoverflowers,
i just wrote this code to populate a tablelayout inside my activity_customer layout. For some reason in runs through without throwing the slightest error, but at the end of "populateView" the application just stops/crashes.
I tried some many things I found via google/stackoverflow, but none seems to work. I hope someone can help me find why the app stops.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer);
populateView(DataClass.getReturnData());
}
private void populateView(String[] Array){
int len = Array.length;
TableLayout tab = (TableLayout) findViewById(R.id.table);
if (len != 0){
for (int i = 0; i <= len - 1; i++) {
TableRow row = new TableRow(this);
TextView tvName = new TextView(this);
tvName.setText("" + Array[i]);
System.out.println(Array[i]);
tab.addView(row);
}
}
}
Thanks in advance,
Soulrox
Here is a complete example:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String[] row = { "ROW1", "ROW2", "Row3", "Row4", "Row 5", "Row 6",
"Row 7"
};
String[] column = { "COLUMN1", "COLUMN2", "COLUMN3", "COLUMN4",
"COLUMN5", "COLUMN6"
};
int rl=row.length;
int cl=column.length;
ScrollView sv = new ScrollView(this);
TableLayout tableLayout = createTableLayout(row, column,rl, cl);
HorizontalScrollView hsv = new HorizontalScrollView(this);
hsv.addView(tableLayout);
sv.addView(hsv);
setContentView(sv);
}
private TableLayout createTableLayout(String [] rv, String [] cv,int rowCount, int columnCount)
{
// 1) Create a tableLayout and its params
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
TableLayout tableLayout = new TableLayout(this);
tableLayout.setBackgroundColor(Color.BLACK);
// 2) create tableRow params
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
tableRowParams.setMargins(1, 1, 1, 1);
tableRowParams.weight = 1;
for (int i = 0; i < rowCount; i++)
{
// 3) create tableRow
TableRow tableRow = new TableRow(this);
tableRow.setBackgroundColor(Color.BLACK);
for (int j= 0; j < columnCount; j++)
{
// 4) create textView
TextView textView = new TextView(this);
// textView.setText(String.valueOf(j));
textView.setBackgroundColor(Color.WHITE);
textView.setGravity(Gravity.CENTER);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
Log.d("TAG", "-___>"+id);
if (i ==0 && j==0)
{
textView.setText("0==0");
}
else if(i==0)
{
Log.d("TAAG", "set Column Headers");
textView.setText(cv[j-1]);
}
else if( j==0)
{
Log.d("TAAG", "Set Row Headers");
textView.setText(rv[i-1]);
}
else
{
textView.setText(""+id);
// check id=23
if(id==23)
{
textView.setText("ID=23");
}
}
// 5) add textView to tableRow
tableRow.addView(textView, tableRowParams);
}
// 6) add tableRow to tableLayout
tableLayout.addView(tableRow, tableLayoutParams);
}
return tableLayout;
}
}
Output:
I have problem with ArrayList. I have the question files structure like:
Y aaaaaa.
Y bbbbbb.
N ccccccc.
N ddddddd.
and I want to put into textview3 the first letter like Y or N
public void value (){
Scanner answerScanner = new Scanner(getResources().openRawResource(R.raw.answer));
Scanner questionScanner = new Scanner(getResources().openRawResource(R.raw.question));
ArrayList<String> answerList = new ArrayList<String>();
ArrayList<String> questionList = new ArrayList<String>();
try {
while (answerScanner.hasNextLine() ) {
answerList.add(answerScanner.nextLine());
questionList.add(questionScanner.nextLine());
}
} finally {
answerScanner.close();
questionScanner.close();
}
int nextInt = random.nextInt(questionList.size());
String answerString = answerList.get(nextInt);
String questionString = questionList.get(nextInt);
yourAnswerString = answerString.substring(0);
yourQuestionString = questionString.substring(2);
shortform = questionString.substring(0,1);
TextView textGenerateNumber = (TextView)findViewById(R.id.textView1);
TextView textGenerateNumber2 = (TextView)findViewById(R.id.textView2);
TextView textGenerateNumber3 = (TextView)findViewById(R.id.textView3);
textGenerateNumber.setText(yourQuestionString);
textGenerateNumber2.setText(yourAnswerString);
textGenerateNumber3.setText(shortform);
}
then I add:
shortform = questionString.substring(0,1);
TextView textGenerateNumber3 = (TextView)findViewById(R.id.textView3);
textGenerateNumber3.setText(shortform);
I get good answers I all cases without first .... when radom get first line from text file I get empty value ( my textView3 are empty).
I have created dynamically edittext on my android application like below:
protected void onCreate(Bundle savedInstanceState) {
RelativeLayout layout = (RelativeLayout) findViewById(R.id.linearLayout1);
final EditText[] Et = new EditText[10];
int prevTextViewId = 0;
int add = 0;
int add1 = 0;
for (int a = 0; a < Arr.length; a++) {
Et[a] = new EditText(this);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
(int) LayoutParams.WRAP_CONTENT,
(int) LayoutParams.WRAP_CONTENT);
param.leftMargin = 25;
param.topMargin = (a + 1) * (100 + add1);
Et[a].setPadding(5, 23, 5, 5);
Et[a].setLayoutParams(param);
Et[a].setSingleLine(false);
Et[a].setHorizontalScrollBarEnabled(false);
Et[a].setWidth(280);
Et[a].getText().toString();
layout.addView(Et[a]);
add = add + 25;
add1 = add1 + 15;
}
}
And I want to get text from dynamically created edittext into a button like below:
btn_doneAnswer = (Button) findViewById(R.id.btn_doneAnswer);
btn_doneAnswer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int a = 0; a < Arr.length; a++) {
Et[a] = new EditText(this);
String text += Et[a].getText().toString();
}
Bundle bundle = new Bundle();
Intent i_backToAddItem = new Intent(
AnswerActivityMultiText.this,
QuestionActivityDynamic.class);
bundle.putString("text", text);
bundle.putString("MultiQues", MultiQues);
i_backToAddItem.putExtras(bundle);
startActivity(i_backToAddItem);
}
});
But I am getting error below in the button onclick method at this line Et[a] = new EditText(this);:
The constructor EditText(new View.OnClickListener(){}) is undefined
another below error at this line += in the button onclick method:
Syntax error on token "+=", = expected
Kindly suggest me how I am getting text from dynamically created edittext into a button.
Waiting for reply.
Thanks
you dont have to initialize the EditText again in the onClick. You should directly use the EditText object to get the text. So the line
Et[a] = new EditText(this);
is not required at all!
And for the error with '+=' operator, you are declaring the string and using the operator(+=) in one line. you have to split up the the declaration part out of the for loop and then use '+='. Here's the complete code :
btn_doneAnswer = (Button) findViewById(R.id.btn_doneAnswer);
btn_doneAnswer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String text;
for (int a = 0; a < Arr.length; a++) {
text += Et[a].getText().toString();
}
Bundle bundle = new Bundle();
Intent i_backToAddItem = new Intent(
AnswerActivityMultiText.this,
QuestionActivityDynamic.class);
bundle.putString("text", text);
bundle.putString("MultiQues", MultiQues);
i_backToAddItem.putExtras(bundle);
startActivity(i_backToAddItem);
}
});
Your problem is in this lines:
for (int a = 0; a < Arr.length; a++) {
Et[a] = new EditText(this);
String text += Et[a].getText().toString();
}
In this line:
Et[a] = new EditText(this);
this is the onClickListener object, you must have class.this (where class is the class name of file that this code is created).
In this other line:
String text += Et[a].getText().toString();
You always create a new String, and instead of created, you make an add. It is a Java concept fail.
You must do this:
String text = "";
for (int a = 0; a < Arr.length; a++) {
Et[a] = new EditText(class_name.this);
text += Et[a].getText().toString();
}
Is it possible to transfer values from SimpleAdapter to array[]. I am getting values in SimpleAdapter from ArrayList<HashMap<String, String>> and want to pass these values from SimpleAdapter to array[].
static ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
SimpleAdapter adapter = new SimpleAdapter(this, contactList,R.layout.list_item,new String[] {android_H_NAME}, new int[] {R.id.name});
String[] hospFields = //want to get values here from adapter
Modify and use this:
ArrayList<String> listItems = new ArrayList<String>();
for (int i = 0; i < result.length(); i++) {
try {
listItems
.add(result.getJSONObject(i)
.getString(BsharpConstant.NAME_CONSTANT)
.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
Inside oncreate method:
ListView list = (ListView) findViewById(R.id.idOfUrView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
ActivityName.this,
android.R.layout.simple_list_item_1, listItems);
list.setAdapter(adapter);
list.setOnItemClickListener(this);//if u need this until not required
Use this want to stores the value in item click:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String clickedItemName = (String) listOfProductsCategory
.getItemAtPosition(position);
int clickedItemId = (int) listOfProductsCategory
.getItemIdAtPosition(position);
// implement your login to store the value
//by using for loop listItems[i] ,i++
Toast.makeText(this.getActivity(),
"Custom text : " + clickedItemName,
Toast.LENGTH_SHORT).show();
//call your method if u want
}
There is no possibility to access the data directly, but you can use (in general):
Object[] o = new Object[adapter.getCount()];
for (int i = 0; i < adapter.getCount(); i++) {
o[i] = adapter.getItem(i);
}
to create a copy of your Adapter data. If you want just to extract specific values use:
String[] hospFields = new String[adapter.getCount()];
for (int i = 0; i < adapter.getCount(); i++) {
HashMap<String, String> rowData = adapter.getItem(i); //gets HashMap of a specific row
String name = rowData.get("name"); //gets the field name of that row
hospFields[i] = name; // copies field into your array
}