Created
November 8, 2019 22:20
Problem creating ListView item on button press
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:id="@+id/main_view" | |
> | |
<ListView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/listView" | |
android:divider="@null"/> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/btn" | |
android:text="Create a new item"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android.scoresorter; | |
import android.content.Context; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
public class CustomeAdapter extends BaseAdapter { | |
private Context context; | |
public static ArrayList<EditModel> editModelArrayList; | |
public CustomeAdapter(Context context, ArrayList<EditModel> editModelArrayList) { | |
this.context = context; | |
CustomeAdapter.editModelArrayList = editModelArrayList; | |
} | |
@Override | |
public int getViewTypeCount() { | |
return getCount(); | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return position; | |
} | |
@Override | |
public int getCount() { | |
return editModelArrayList.size(); | |
} | |
@Override | |
public Object getItem(int position) { | |
return editModelArrayList.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return 0; | |
} | |
@Override | |
public View getView(final int position, View convertView, ViewGroup parent) { | |
final ViewHolder holder; | |
if (convertView == null) { | |
holder = new ViewHolder(); | |
LayoutInflater inflater = (LayoutInflater) context | |
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
convertView = inflater.inflate(R.layout.lv_item, null, true); | |
holder.editText = convertView.findViewById(R.id.editid); | |
convertView.setTag(holder); | |
}else { | |
// the getTag returns the viewHolder object set as a tag to the view | |
holder = (ViewHolder)convertView.getTag(); | |
} | |
holder.editText.setText(editModelArrayList.get(position).getEditTextValue()); | |
holder.editText.addTextChangedListener(new TextWatcher() { | |
@Override | |
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { | |
} | |
@Override | |
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { | |
editModelArrayList.get(position).setEditTextValue(holder.editText.getText().toString()); | |
} | |
@Override | |
public void afterTextChanged(Editable editable) { | |
} | |
}); | |
return convertView; | |
} | |
private class ViewHolder { | |
protected EditText editText; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android.scoresorter; | |
public class EditModel { | |
private String editTextValue; | |
public String getEditTextValue() { | |
return editTextValue; | |
} | |
public void setEditTextValue(String editTextValue) { | |
this.editTextValue = editTextValue; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="12dp" | |
android:textColor="#000" | |
android:text="TextView:" | |
android:layout_weight="1"/> | |
<EditText | |
android:id="@+id/editid" | |
android:layout_width="wrap_content" | |
android:layout_height="50dp" | |
android:layout_marginTop="10dp" | |
android:gravity="end" | |
android:paddingLeft="10dp" | |
android:paddingRight="20dp" | |
android:text="hello" | |
android:textColor="#000" /> | |
</LinearLayout> | |
<View | |
android:layout_width="match_parent" | |
android:layout_height="1dp" | |
android:layout_marginTop="10dp" | |
android:background="@color/colorAccent"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android.scoresorter; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.view.Gravity; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.ListView; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
public class MainActivity extends AppCompatActivity { | |
public Button btn; | |
private ListView lv; | |
private CustomeAdapter customeAdapter; | |
public ArrayList<EditModel> editModelArrayList; | |
int populateListMaxNum =3; | |
int listNumber = populateListMaxNum; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
lv = (ListView) findViewById(R.id.listView); | |
btn = (Button) findViewById(R.id.btn); | |
editModelArrayList = populateList(); | |
customeAdapter = new CustomeAdapter(this,editModelArrayList); | |
lv.setAdapter(customeAdapter); | |
/* TODO activate button */ | |
btn.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
addToList(); | |
Toast.makeText(getApplicationContext(), "button", Toast.LENGTH_LONG).show(); | |
} | |
}); | |
} | |
private ArrayList<EditModel> populateList(){ //this part works perfectly | |
ArrayList<EditModel> list = new ArrayList<>(); | |
for(int i = 0; i < populateListMaxNum; i++){ | |
EditModel editModel = new EditModel(); | |
//editModel.setEditTextValue(String.valueOf(i)); | |
list.add(editModel); | |
} | |
return list; | |
} | |
/*TODO make it work = expand */ | |
private void addToList(){ // this part doesn't work nor it doesn't fail | |
EditModel editModel = new EditModel(); | |
editModelArrayList.add(editModel); | |
customeAdapter.notifyDataSetChanged(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment