Я добавляю элементы в свой список, он отлично работает, но список не расширяется.Как добавить элементы в listview Динамически в Android?
Это мой код:
private ArrayList<NameDetails> nameDetails = null;
внутри onCreate()
код
nameDetails = new ArrayList<NameDetails>();
case R.id.tvAddMore:
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.location_name_details_dialog);
final EditText etName = (EditText) dialog.findViewById(R.id.etName);
final EditText etPhone = (EditText) dialog.findViewById(R.id.etPhone);
Button btnOk = (Button) dialog.findViewById(R.id.btnOk);
Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
btnOk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
String name= etName.getText().toString();
String phone= etPhone.getText().toString();
nameDetails = new ArrayList<NameDetails>();
NameDetails nameDetailss = new NameDetails();
nameDetailss.setName(name);
nameDetailss.setPhone(phone);
nameDetails.add(nameDetailss);
NameDetailsAdapter nameDetailsAdapter = new NameDetailsAdapter(nameDetails);
lvNameDetails.setAdapter(nameDetailsAdapter);
nameDetailsAdapter.notifyDataSetChanged();
dialog.cancel();
}
});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
dialog.cancel();
}
});
dialog.show();
break;
public class NameDetailsAdapter extends BaseAdapter
{
ArrayList<NameDetails> nameDetails;
String Name,Phone;
int count = 0;
Context ctx;
Activity act;
public NameDetailsAdapter(ArrayList<NameDetails> nameDetails)
{
this.nameDetails = nameDetails;
}
@Override
public int getCount() {
return nameDetails.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
LayoutInflater vi = (LayoutInflater)MyApp.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.name_details, null);
TextView tvName = (TextView)convertView.findViewById(R.id.tvName);
TextView tvPhone = (TextView)convertView.findViewById(R.id.tvPhone);
TextView tvNameOne = (TextView)convertView.findViewById(R.id.tvNameOne);
TextView tvPhoneOne = (TextView)convertView.findViewById(R.id.tvPhoneOne);
NameDetails name = nameDetails.get(position);
tvName.setText(name.getName());
tvPhone.setText(name.getPhone());
tvNameOne.setTypeface(AppConstants.BOLD);
tvPhoneOne.setTypeface(AppConstants.BOLD);
return convertView;
}
}
NameDetails.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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin15"
android:layout_marginRight="@dimen/margin15"
android:layout_marginTop="5dp"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/tvNameOne"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="@string/name"
android:textSize="@dimen/text_size14" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text=":" />
<TextView
android:id="@+id/tvName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin10"
android:layout_weight="0.65"
android:background="@drawable/text_field"
android:gravity="center_vertical"
android:inputType="textPersonName"
android:maxEms="40"
android:paddingLeft="@dimen/padding5"
android:paddingRight="@dimen/padding5"
android:singleLine="true"
android:textColor="@android:color/black"
android:textSize="@dimen/text_size13" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/tvPhoneOne"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="@string/phone_no"
android:textSize="@dimen/text_size14" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text=":" />
<TextView
android:id="@+id/tvPhone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin10"
android:layout_weight="0.65"
android:background="@drawable/text_field"
android:gravity="center_vertical"
android:inputType="phone"
android:maxEms="40"
android:textColor="@android:color/black"
android:paddingLeft="@dimen/padding5"
android:paddingRight="@dimen/padding5"
android:singleLine="true"
android:textSize="@dimen/text_size13" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
в вашем XML. где находится список? –
Всякий раз, когда вы добавляете новые данные, вызывайте notifyDataSetChanged(), и ваш список будет обновлен. Что notifyDataSetChanged() будет делать, так это сообщить вашему ListView, что данные были обновлены, и восстановить данные в ListView –