Making a simple program which will generate a multiple choice form. I have an sing_select.xml which acts as the template for making each question. Then, in code I wanted to populate my main.xml with a bunch of these templates customized. Though it works great for the first question, any subsequent questions do not get displayed. Not sure what I'm doing wrong. I know there isn't an overlap as I manually hid the first question.
Java File
public class FormFillerActivity extends Activity
{
private LinearLayout mQuestionList;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Must come before setContentView or program crashes
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Must set before accessing layout elements or program crashes
setContentView(R.layout.main);
mQuestionList = (LinearLayout) findViewById(R.id.Body_Layout);
initForm();
}
private void initForm()
{
int count = 1;
ArrayList<String> answers = new ArrayList<String>();
answers.add("Single");
answers.add("Married");
answers.add("Separated");
answers.add("Divorced");
mQuestionList.addView(addSingSelectQuestion(count++, "What is your marital status?", answers));
answers.clear();
answers.add("Male");
answers.add("Female");
mQuestionList.addView(addSingSelectQuestion(count++, "What is your gender?", answers));
}
private View addSingSelectQuestion(int count, String question, ArrayList<String> answers)
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View container = inflater.inflate(R.layout.sing_select, null);
((TextView) container.findViewById(R.id.Sing_Select_Num)).setText(count + ") ");
((TextView) container.findViewById(R.id.Sing_Select_Text)).setText(question);
RadioGroup rg = (RadioGroup) container.findViewById(R.id.Sing_Select_Answer);
//Generate radio group answers
Iterator<String> it = answers.iterator();
while (it.hasNext())
{
RadioButton rb = new RadioButton(rg.getContext());
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
String ans = it.next();
rb.setId(answers.indexOf(ans));
rb.setLayoutParams(params);
rb.setText(ans);
rb.setTextColor(getResources().getColor(R.color.black));
rb.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.txt_normal));
rg.addView(rb);
}
return container;
}
}
main.xml (stripped out unrelated UI elements)
<?xml version="1.0" encoding="utf-8"?>
...
<ScrollView
android:id="#+id/Body_Scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/Footer"
android:layout_below="#id/Title"
android:scrollbars="vertical" >
<LinearLayout
android:id="#+id/Body_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/marg_normal"
android:padding="#dimen/pad_large" >
</LinearLayout>
</ScrollView>
...
sing_select.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Sing_Select_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:padding="8dp" >
<TextView
android:id="#+id/Sing_Select_Num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#) "
android:textColor="#color/black"
android:textSize="#dimen/txt_normal" />
<TextView
android:id="#+id/Sing_Select_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/Sing_Select_Num"
android:layout_toRightOf="#+id/Sing_Select_Num"
android:text="The Question?"
android:textColor="#color/black"
android:textSize="#dimen/txt_normal" />
<RadioGroup
android:id="#+id/Sing_Select_Answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Sing_Select_Text"
android:layout_below="#+id/Sing_Select_Text"
android:layout_toLeftOf="#+id/Sing_Select_Trans_Button" >
</RadioGroup>
<Button
android:id="#+id/Sing_Select_Trans_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/btn_big"
android:padding="8dp"
android:text="Accept"
android:textSize="#dimen/txt_button" />
</RelativeLayout>
Try to set orientation to vertical for the *Body_Layout* LinearLayout. I think your pushing out of the screen the next rows after the first one(the width for the inflated view is set to fill the parent).
Related
This question already has answers here:
Trouble with ListView
(3 answers)
Closed 4 years ago.
I have a problem while I am trying to create a simple ListView.
Here is what I want : just an activity with 1 ListView with text inside (and buttons in the next steps) AND beside 1 (or more) button.
So in my XML I have a ListView, a TextView (the one inside the ListView's children and a button (the one I don't want inside my ListView). When I lunch my app, i have every views of my activity in my listView... And i don't know where I have to manage that.
Here is my code :
My activity :
ListView listView = findViewById(R.id.MainListDecks);
AdapterDeck adpDeck;
ArrayList<Deck> myDecks = new ArrayList<>();
for (int i = 0; i<5;i++){
myDecks.add(new Deck("Deck " + i));
}
adpDeck = new AdapterDeck(this, 0, myDecks);
listView.setAdapter(adpDeck);
My adapter :
public AdapterDeck(#NonNull Context context, int resource, #NonNull
ArrayList<Deck> decks) {
super(context, resource, decks);
this.decks = decks;
this.context = context;
}
#Override
public View getView(final int position, View convertView, ViewGroup
parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.activity_main, null);
}
TextView listItemText = view.findViewById(R.id.MainDeckNom);
listItemText.setText(decks.get(position).getNom())
return view;
}
My XML :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Background"
tools:context="activities.MainMenu">
<ListView
android:id="#+id/MainListDecks"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/MainDeckNom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="20dp"
android:layout_marginBottom="22dp"
android:text="Button" />
</RelativeLayout>
The result : the result i got and i don't want
Thanks for anyone who can help !
Cheers
They're not in your ListView, they're above it.
Your root layout is a RelativeLayout. RelativeLayout allows children to overlap. If you want a ListView with a button on the bottom right and (I'm assuming) some text on the bottom, you should be using a LinearLayout and a FrameLayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Background"
android:orientation="vertical"
tools:context="activities.MainMenu">
<ListView
android:id="#+id/MainListDecks"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/MainDeckNom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginBottom="22dp"
android:text="Button"
android:layout_gravity="center_vertical|end"
/>
</FrameLayout>
</LinearLayout>
I made this dialog for my app that shows up allows you to delete the items from db. It used to be a textView and a button which called the function, all in a linear layout, which worked good. Now I changed to a Relative Layout so I could add another button on the same line as the other, I also added an editText.
My problem is that since I added those objects I am not able to see the buttons anymore. the editText and textView are visible.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/dialog_deletefood"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.paulcosma.app2.SecondActivity">
<TextView
android:text="TextView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lblFoodDetails"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp" />
<Button
android:text="Şterge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnDeleteEatenFood"
android:layout_alignBaseline="#+id/btnModifyEatenFood"
android:layout_alignBottom="#+id/btnModifyEatenFood"
android:layout_toStartOf="#+id/lblFoodDetails"
android:visibility="visible" />
<Button
android:layout_marginTop="100dp"
android:text="Modifică"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnModifyEatenFood"
android:layout_below="#+id/lblFoodDetails"
android:layout_toEndOf="#+id/lblFoodDetails"
android:visibility="visible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/txtEditValue"
android:maxLength="6"
style="#style/Widget.AppCompat.AutoCompleteTextView"
android:maxLines="1"
android:textAppearance="#style/TextAppearance.AppCompat"
android:fontFamily="sans-serif"
android:textSize="14sp"
android:textAlignment="center"
android:textColorLink="#android:color/holo_blue_light"
android:inputType="numberDecimal"
android:layout_marginTop="21dp"
android:layout_below="#+id/lblFoodDetails"
android:layout_centerHorizontal="true" />
</RelativeLayout>
This dialog is supposed to show on listView item click. Below is the code I used to show it.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, String> hmap = (HashMap<String, String>)parent.getItemAtPosition(position);
final String _name = hmap.get("food");
final int _id = foodDB.getFoodId(_name);
AlertDialog.Builder mBuiler = new AlertDialog.Builder(SecondActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_deletefood,null);
final TextView lblFoodDetails = (TextView) mView.findViewById(R.id.lblFoodDetails);
final Button btnDeleteEatenFood = (Button) mView.findViewById(R.id.btnDeleteEatenFood);
lblFoodDetails.setText("Aţi ales produsul " + _name + ". Aici puteţi adăuga sau scădea cantitatea printr-o anumită valoare sau puteţi şterge apariţia produsului.");
mBuiler.setView(mView);
final AlertDialog dialog = mBuiler.create();
btnDeleteEatenFood.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
foodDB.deleteEatenFood(_id);
Toast.makeText(SecondActivity.this,_name+" a fost sters",Toast.LENGTH_SHORT).show();
dialog.dismiss();
updateFoodList(foodDB.getAllEatenFood());
}
});
dialog.show();
}
This layout file renders the buttons in the Android layout designer as described (see below)
The problem may reside in code.
The problem was that both buttons where placed based on textView's size. When it changed, buttons got placed out of the screen
Bug:
android:layout_toEndOf="#+id/lblFoodDetails"
android:layout_toEStartOf="#+id/lblFoodDetails"
I've been looking for the answer to my problem, and I've found similar entries and have fixed some things but not the main problem.
When I run my code all thumbnails' images are the same and there is no text displaying in the TextViews, even though the Log.D is showing I'm changing the textViews to the correct texts and images.
My activity code:
ListView lvMaterias;
String[] materiasNombre;
int[] thumbnails={R.drawable.thumbnailanato,
R.drawable.thumbnailbioqui,
R.drawable.thumbnailanato,
R.drawable.thumbnailbioqui,
R.drawable.thumbnailanato,
R.drawable.thumbnailbioqui,
R.drawable.thumbnailanato,
R.drawable.thumbnailbioqui,
R.drawable.thumbnailanato,
R.drawable.thumbnailbioqui,
R.drawable.thumbnailanato,
R.drawable.thumbnailbioqui,
R.drawable.thumbnailanato,
R.drawable.thumbnailbioqui,
R.drawable.thumbnailanato,
R.drawable.thumbnailbioqui};
List<materiaRow> materiasObjetos= new ArrayList<materiaRow>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
//inicializar los arrays y todo
lvMaterias = (ListView)findViewById(R.id.lvMaterias);
materiasNombre=this.getResources().getStringArray(R.array.nombreMateria);
int i=0;
for (String nombre : materiasNombre){
Log.d("loop", nombre);
materiasObjetos.add(new materiaRow(thumbnails[i],nombre, "0%"));
}
lvMaterias.setAdapter(new materiasAdapter(getApplicationContext(), R.layout.rowmateria, materiasObjetos));
}
}
My Adapter Class, the log.d at the getView method show I have the correct text and images, but textViews are not getting changes:
public class materiasAdapter extends ArrayAdapter implements View.OnClickListener{
private int layout;
public materiasAdapter(Context context, int resource, List objects) {
super(context, resource, objects);
layout=resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
materiaHolder mH;
if (convertView==null){
mH= new materiaHolder();
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView= inflater.inflate(layout, parent, false);
mH.thumbnail= (ImageView) convertView.findViewById(R.id.ivthumbnail);
mH.materia= (TextView) convertView.findViewById(R.id.tvmateria);
mH.porcentaje= (TextView) convertView.findViewById(R.id.tvporcentaje);
mH.favorito = (Button) convertView.findViewById(R.id.bfavoritos);
mH.favorito.setOnClickListener(this);
convertView.setTag(mH);
}
else {
mH= (materiaHolder) convertView.getTag();
}
materiaRow mR= (materiaRow) getItem(position);
mH.thumbnail.setImageResource(mR.getThumbnail());
mH.porcentaje.setText(mR.getPorcentaje());
Log.d("thumbnail", Integer.toString(mR.getThumbnail()));
mH.materia.setText(mR.getMateria());
Log.d("texto", mR.getMateria());
return convertView;
}
#Override
public void onClick(View v) {
}
class materiaHolder {
ImageView thumbnail;
TextView materia;
TextView porcentaje;
Button favorito;
}
}
XML as requested (Select Activity):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.quetzal.elite.Select">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/lvMaterias" />
And rowMateria (resource of the adapter class):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/ivthumbnail" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/tvmateria" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/tvporcentaje" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="fav"
android:id="#+id/bfavoritos" />
</LinearLayout>
Thanks in advance for the help.
As far as the issue with the thumgnail goes... In your main onCreate(), it doesn't look like you aren't adding the rows correctly.
int i=0;
for (String nombre : materiasNombre){
Log.d("loop", nombre);
materiasObjetos.add(new materiaRow(thumbnails[i],nombre, "0%"));
}
You need to increment i, or you will always get thumbnails[0].
When I ran your code, for whatever reason on my emulator, the text color and background color were the same - So the text was being set, but you could not see it.
You can set up your background on the activity as something specific like
android:background="#android:drawable/screen_background_dark"
and the text with something like
android:textColor="#android:color/primary_text_light"
I'm not very well versed on the preferred methods of setting the background and text colors - longer term you should be looking at the themes I believe.
Try making the ViewHolder class static like here:
https://github.com/isaacurbina/ViewHolderListView/blob/master/app/src/main/java/com/mac/isaac/viewholderlistview/MyArrayAdapter.java
Hope it helps!
So, I'm trying to create a screen that when clicking a button this takes the data from some EditText and add them to a ListView item, now I know there are a lot of examples on the web and I've done them and they work, but when I took them to what I want to do it just adds one item and stops working it doesn't throw any exception or anything, it just add one item, this is what I got so far...
create_class.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.eduardolaguna.mariela.app.activities.CreateClass">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/cc_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cc_hint_class_name" />
<View
android:id="#+id/cc_divider1"
style="#style/Divider"
android:layout_below="#+id/cc_name" />
<TextView
android:id="#+id/tv_cc_professors_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/cc_divider1"
android:text="#string/cc_tv_professors_data" />
<EditText
android:id="#+id/cc_professors_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_cc_professors_data"
android:hint="#string/cc__hint_professors_name"
android:inputType="textPersonName|textAutoComplete|textAutoCorrect" />
<EditText
android:id="#+id/cc_professors_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cc_professors_name"
android:hint="#string/cc_hint_professors_email"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/cc_professors_phonenumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cc_professors_email"
android:hint="#string/cc_hint_professors_phonenumber"
android:inputType="phone" />
<View
android:id="#+id/cc_divider2"
style="#style/Divider"
android:layout_below="#id/cc_professors_phonenumber" />
<TextView
android:id="#+id/tv_cc_schedule"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cc_divider2"
android:text="#string/cc_tv_class_schedule" />
<Spinner
android:id="#+id/sp_cc_day_of_the_week"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_cc_schedule"
android:entries="#array/dow" />
<EditText
android:id="#+id/cc_from_schedule"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/sp_cc_day_of_the_week"
android:hint="#string/cc_hint_from_schedule"
android:inputType="time" />
<EditText
android:id="#+id/cc_to_schedule"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/sp_cc_day_of_the_week"
android:layout_toRightOf="#id/cc_from_schedule"
android:hint="#string/cc_hint_to_schedule"
android:inputType="time" />
<EditText
android:id="#+id/cc_floor_number"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/sp_cc_day_of_the_week"
android:layout_toRightOf="#id/cc_to_schedule"
android:hint="#string/cc_hint_floor"
android:inputType="number" />
<EditText
android:id="#+id/cc_classroom"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/sp_cc_day_of_the_week"
android:layout_toRightOf="#id/cc_floor_number"
android:hint="#string/cc_hint_classroom" />
<Button
android:id="#+id/btn_cc_add_schedule"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cc_from_schedule"
android:text="#string/cc_add_schedule" />
<ListView
android:id="#+id/cc_schedule_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/btn_cc_add_schedule" />
</RelativeLayout>
</ScrollView>
</LinearLayout>
schedule_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/sch_day_of_the_week"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Miércoles"
android:textSize="60dp" />
<TextView
android:id="#+id/sch_from"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5:45"
android:textSize="20dp" />
<TextView
android:id="#+id/sch_to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7:15"
android:textSize="20dp" />
<TextView
android:id="#+id/sch_floor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7"
android:textSize="20dp" />
<TextView
android:id="#+id/sch_clasroom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="lab1"
android:textSize="20dp" />
</LinearLayout>
ScheduleAdapter.java
public class ScheduleAdapter extends ArrayAdapter<Actividad> {
private int layoutResourceId;
private LayoutInflater inflater;
private List<Actividad> shifts;
public ScheduleAdapter(Context context, int resource, List<Actividad> objects) {
super(context, resource, objects);
layoutResourceId = resource;
shifts = objects;
inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
Holder holder = null;
row = inflater.inflate(layoutResourceId, null);
holder = new Holder();
holder.actividad = shifts.get(position);
holder.dow = (TextView) row.findViewById(R.id.sch_day_of_the_week);
holder.fromTXT = (TextView) row.findViewById(R.id.sch_from);
holder.toTXT = (TextView) row.findViewById(R.id.sch_to);
holder.floorTXT = (TextView) row.findViewById(R.id.sch_floor);
holder.roomTXT = (TextView) row.findViewById(R.id.sch_clasroom);
setupItem(holder);
row.setTag(holder);
return row;
}
private void setupItem(Holder holder) {
holder.dow.setText(holder.actividad.getDiaDeLaSemana());
holder.fromTXT.setText(holder.actividad.getDesdeStr());
holder.toTXT.setText(holder.actividad.getHastaStr());
holder.floorTXT.setText(holder.actividad.getPiso());
holder.roomTXT.setText(holder.actividad.getSalon());
}
public static class Holder {
Actividad actividad;
TextView dow;
TextView fromTXT;
TextView toTXT;
TextView floorTXT;
TextView roomTXT;
}
}
And finally the CreateClass.java
public class CreateClass extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_class);
ListView view = (ListView) findViewById(R.id.cc_schedule_list);
final ScheduleAdapter adapter = new ScheduleAdapter(getApplicationContext(), R.layout.schedule_item, new ArrayList<Actividad>());
view.setAdapter(adapter);
Button addSchedule = (Button) findViewById(R.id.btn_cc_add_schedule);
addSchedule.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Resources res = getResources();
ListView scheduleView = (ListView) findViewById(R.id.cc_schedule_list);
ScheduleAdapter adapter1 = (ScheduleAdapter) scheduleView.getAdapter();
Actividad act = new Actividad(TipoEvento.CLASE);
Spinner dow = (Spinner) findViewById(R.id.sp_cc_day_of_the_week);
act.setDiaDeLaSemana(dow.getSelectedItem().toString());
TextView from = (TextView) findViewById(R.id.cc_from_schedule);
act.setDesdeStr(res.getString(R.string.from) + ": " + from.getText().toString());
TextView to = (TextView) findViewById(R.id.cc_to_schedule);
act.setHastaStr(res.getString(R.string.to) + ": " + to.getText().toString());
TextView floor = (TextView) findViewById(R.id.cc_floor_number);
act.setPiso(res.getString(R.string.floor) + ": " + floor.getText().toString());
TextView classroom = (TextView) findViewById(R.id.cc_classroom);
act.setSalon(res.getString(R.string.classroom) + ": " + classroom.getText().toString());
adapter1.add(act);
}
});
}
}
You should instantiate your adapter with the Activity context, not the application context.
Storing a copy of the constructed list in shifts can be dangerous.
Don't store shifts in the Holder. Only views go in there.
When populating the convertView, reference the internal getItem(position) method to obtain the Actividad data...not your shifts variable.
You're also using the ViewHolder paradigm incorrectly. Example here.
Turns out the problem is the ListView is inside the ScrollView, this does not work properly when the list grows dynamically, I move it out the ScrollView and it works like a charm.
This was according to #Romain Guy a developer in the Android project, where he stated that
Using a ListView to make it not scroll is extremely expensive and goes against the whole purpose of ListView. You should NOT do this. Just use a LinearLayout instead.
So I use a LinearLayout to draw the new items on the layout.
I have tried to add rating bar in Linear Layout at dynamically after some textfields. But I'm getting NullPointerException at the line = lL.addView(rating); Can someone help me please how to do this.Thanks in Advance.
Here is my xml file.
<ScrollView
android:id="#+id/scrollField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/btnDELETE"
android:layout_below="#+id/textTitle"
android:layout_marginTop="10dp" >
<LinearLayout
android:id="#+id/linearLayoutRatingDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_EmployeeName"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:text="dfkyjrtl"
android:textColor="#2E1F1F" />
<TextView
android:id="#+id/tv_TaskName"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="6dp"
android:text="dfkyjrtl"
android:textColor="#2E1F1F" />
<TextView
android:id="#+id/tv_TaskDate"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="6dp"
android:text="dfkyjrtl"
android:textColor="#2E1F1F" />
<TextView
android:id="#+id/tv_TaskRate"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="6dp"
android:text="dfkyjrtl"
android:textColor="#2E1F1F" />
</LinearLayout>
</ScrollView>
Hare is my Activity code.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.task_details);
Intent intent = getIntent();
str_IntentName = intent.getExtras().getString("EMP_NAME");
str_IntentTaskName = intent.getExtras().getString("TASK_NAME");
str_IntentDate = intent.getExtras().getString("TASK_DATE");
str_IntentRate = intent.getExtras().getString("TASK_RATE");
numStar = Integer.valueOf(str_IntentRate);
Log.e("integer numStar "," = " + numStar);
lL = (LinearLayout)findViewById(R.id.linearLayoutRatingDetails);
tvEmpName.setText(str_IntentName);
tvTaskName.setText(str_IntentTaskName);
tvDate.setText(str_IntentDate);
tvRate.setText(str_IntentRate);
Create_RatingBar();
}
private void Create_RatingBar()
{
stepSize = (float) 0.5;
rating = new RatingBar(Task_Details.this);
rating.setNumStars(numStar);
rating.setStepSize(stepSize);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
param.topMargin = 500;
rating.setLayoutParams(param);
lL.addView(rating);
}
In order to add it to the LinearLayout from your xml you need to get a "reference" to this LinearLayout in your code. For that you need to ad id it in xml and get an element using this id in your code.
public void onCreate(Bundle savedInstanceState){
//Some other code for other views
ViewGroup container = (ViewGroup) findViewById(R.id.linearLayoutRatingDetails);
setupRatingBar(container)
}
private void setupRatingBar(ViewGroup ratingBarContainer){
RatingBar ratingBar = new RatingBar(Task_Details.this);
//All methods you need to initialize the rating bar
//including setNumStars(), setStepSize() and layout params
ratingBarContainer.addView(ratingBar);
}
Also use more explanatory names. "lL" will cause you headache in the future.