在Android Studio中将自定义行添加到列表视图



在我的CT_Locations活动中,当我单击";添加断点";按钮XML文件有一个标签、一个AutoCompleteTextView和一个";取消";ImageButton。我已经创建了一个断点类和一个断点适配器,但当我单击按钮时,它并没有像预期的那样添加一行。我做错什么了吗?谢谢


activity_ct_locations_filters.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CT_Locations">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/feefef"
android:layout_width="363dp"
android:layout_height="70dp"
android:layout_marginTop="12dp"
android:background="#ECECEC"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/listview">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="287dp"
android:layout_height="69dp"
android:background="#D5D5D5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<AutoCompleteTextView
android:id="@+id/destino"
android:layout_width="168dp"
android:layout_height="55dp"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.046"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.548" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/sdwdw"
android:layout_width="363dp"
android:layout_height="70dp"
android:background="#ECECEC"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.264">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="289dp"
android:layout_height="76dp"
android:background="#D5D5D5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<AutoCompleteTextView
android:id="@+id/origem"
android:layout_width="169dp"
android:layout_height="55dp"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="From"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.048"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.548" />
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageButton
android:id="@+id/homeBtnShowTransports2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_dialog_dialer" />
<ImageButton
android:id="@+id/returnBtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_menu_revert" />
<TextView
android:id="@+id/CT_LocationFiltersTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:text="Create Trip"
android:textSize="28sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/filtersBtn1"
android:layout_width="112dp"
android:layout_height="41dp"
android:layout_marginTop="44dp"
android:text="Filter by"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/CT_LocationFiltersTextView" />
<Button
android:id="@+id/searchResultsBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search Results"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.489"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.939" />
<ImageButton
android:id="@+id/swapBtn"
android:layout_width="58dp"
android:layout_height="54dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.954"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.143"
app:srcCompat="@android:drawable/ic_menu_rotate" />
<Button
android:id="@+id/addBreakpoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add Breakpoint"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/feefef"
app:layout_constraintVertical_bias="0.101" />
<ListView
android:id="@+id/listview"
android:layout_width="364dp"
android:layout_height="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/sdwdw"
app:layout_constraintVertical_bias="0.027" />
</androidx.constraintlayout.widget.ConstraintLayout>

CT_Locations.java

public class CT_Locations extends AppCompatActivity {
private Intent data;
private String depDate;
private String depTime;
private String arrTime;
private boolean bus;
private boolean train;
private boolean metro;
private String order;
private int numOfBreakpoints;
private ListView listView;
ArrayList<Breakpoint> breakpoints;
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_ct_locations_filters);
AutoCompleteTextView location2_1 = findViewById(R.id.origem);
AutoCompleteTextView location2_2 = findViewById(R.id.destino);
// update origin location with the main activity current location
location2_1.setText(MainActivity.currentLocation);
numOfBreakpoints = 0;
listView = findViewById(R.id.listview);
breakpoints = new ArrayList<Breakpoint>();
BreakpointAdapter adapter = new BreakpointAdapter(this, breakpoints);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
Button filtersBtn = (Button) findViewById(R.id.filtersBtn1);
filtersBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent goToFilters = new Intent(getApplicationContext(), CT_Filters.class);
startActivityForResult(goToFilters,1);
}
});
ImageButton swapBtn = (ImageButton) findViewById(R.id.swapBtn);
swapBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String location1 = location2_1.getText().toString();
String location2 = location2_2.getText().toString();
location2_1.setText(location2);
location2_2.setText(location1);
}
});
Button addBreakpoint = (Button) findViewById(R.id.addBreakpoint);
addBreakpoint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//add xml row to the listview
Breakpoint bk = new Breakpoint();
adapter.add(bk);
}
});
Button searchResultsBtn = (Button) findViewById(R.id.searchResultsBtn);
searchResultsBtn.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
String location1 = location2_1.getText().toString();
String location2 = location2_2.getText().toString();
if (location1 != "" && location2 != "") {
CT_SearchResults.origem = location1;
CT_SearchResults.destino = location2;
Intent goToResults = new Intent(getApplicationContext(), CT_SearchResults.class);
startActivity(goToResults);
} else {
System.out.println("Falta parametros");
}
}
});
}

}


row_location.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout4"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="29dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:background="#ECECEC"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/rwgwrgwgwrgw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="28dp"
android:layout_marginRight="28dp"
android:text="Breakpoint"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/constraintLayout2"
app:layout_constraintHorizontal_bias="0.048"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.49" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2"
android:layout_width="268dp"
android:layout_height="70dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:background="#D5D5D5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/rwgwrgwgwrgw"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<ImageButton
android:id="@+id/deleteBreakpoint"
android:layout_width="32dp"
android:layout_height="34dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.936"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.485"
app:srcCompat="@android:drawable/ic_menu_close_clear_cancel" />
<AutoCompleteTextView
android:id="@+id/locationAutoComplete"
android:layout_width="187dp"
android:layout_height="55dp"
android:completionHint="Insert a location"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.175"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.52" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

断点.java

public class Breakpoint {
public String location;
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}

}


断点适配器.java

public class BreakpointAdapter extends ArrayAdapter<Breakpoint> {
public BreakpointAdapter(Context context, ArrayList<Breakpoint> breakpoints) {
super(context, R.layout.row_location, breakpoints);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Breakpoint breakpoint = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_location, parent, false);
}
return convertView;
}

}


我已经有一段时间没有使用ListView或编码了,所以我可能错了。

在您将项目添加到适配器后,您应该调用:

adapter.notifyDataSetChanged()

在您的情况下:

Button addBreakpoint = (Button) findViewById(R.id.addBreakpoint);
addBreakpoint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//add xml row to the listview
Breakpoint bk = new Breakpoint();
adapter.add(bk);
adapter.notifyDataSetChanged() // Notify the adapter an item has been added
}
});

如果这是错误的尝试这个:

Button addBreakpoint = (Button) findViewById(R.id.addBreakpoint);
addBreakpoint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//add xml row to the listview
Breakpoint bk = new Breakpoint();
breakpoints.add(bk);
adapter.notifyDataSetChanged(); // Notify the adapter a list item has been added
}
});

最新更新