这是listview java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manager_dashboard);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listView = (ListView) findViewById(R.id.list);
connectionClass = new ConnectionClass();
itemsArrayList = new ArrayList<ClassListItems>();
search = (EditText) findViewById(R.id.search);
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String text = search.getText().toString().toLowerCase(Locale.getDefault());
myAppAdapter.filter(text);
}
});
SyncData orderData = new SyncData();
orderData.execute("");
shp = this.getSharedPreferences("UserInfo", MODE_PRIVATE);
String userid = shp.getString("UserId", "none");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
TextView textViewUserId = (TextView) navigationView.getHeaderView(0).findViewById(R.id.textViewUserId);
textViewUserId.setText(userid);
}
private class SyncData extends AsyncTask<String, String, String> {
String msg = "Error";
ProgressDialog progress;
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(ManagerDashboard.this, "Synchronising",
"Listview Loading! Please Wait...", true);
}
@Override
protected String doInBackground(String... strings) {
try {
Connection conn = connectionClass.CONN();
if (conn == null) {
success = false;
} else {
String query = "SELECT * FROM dbo.Dashboard";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs != null) {
while (rs.next()) {
try {
if (rs.getString("AccountType").equals("Seller")) {
itemsArrayList.add(new ClassListItems(rs.getString("slsperid"), rs.getString("Visibility_Tgt"),
rs.getString("Visibility_Actual"), rs.getString("GPTgt"),
rs.getString("GPAct"), rs.getString("GSTgt"), rs.getString("GSAct")));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
msg = "Found";
success = true;
} else {
msg = "No Data found!";
success = false;
}
}
} catch (Exception e) {
e.printStackTrace();
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
msg = writer.toString();
success = false;
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
progress.dismiss();
Toast.makeText(ManagerDashboard.this, msg + "", Toast.LENGTH_LONG).show();
if (success == false) {
} else {
try {
myAppAdapter = new MyAppAdapter(itemsArrayList, ManagerDashboard.this);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(myAppAdapter);
} catch (Exception ex) {
}
}
}
}
Listview适配器
public class MyAppAdapter extends BaseAdapter {
public class ViewHolder{
TextView tvuserid, tvvstarget, tvvsact, tvvsp, tvgptarget, tvgpact, tvgpp, tvgstarget, tvgsact, tvgsp;
}
public List<ClassListItems> parkingList;
public Context context;
ArrayList<ClassListItems> arrayList;
private MyAppAdapter(List<ClassListItems> apps, Context context){
this.parkingList = apps;
this.context = context;
arrayList = new ArrayList<ClassListItems>();
arrayList.addAll(parkingList);
}
@Override
public int getCount() {return parkingList.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){
View rowView = convertView;
ViewHolder viewHolder = null;
if(rowView == null){
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.list_content, parent, false);
viewHolder = new ViewHolder();
viewHolder.tvuserid = (TextView) rowView.findViewById(R.id.tvuserid);
viewHolder.tvvstarget = (TextView) rowView.findViewById(R.id.tvvstarget);
viewHolder.tvvsact = (TextView) rowView.findViewById(R.id.tvvsact);
viewHolder.tvvsp = (TextView) rowView.findViewById(R.id.tvvsp);
viewHolder.tvgptarget = (TextView) rowView.findViewById(R.id.tvgptarget);
viewHolder.tvgpact = (TextView) rowView.findViewById(R.id.tvgpact);
viewHolder.tvgpp = (TextView) rowView.findViewById(R.id.tvgpp);
viewHolder.tvgstarget = (TextView) rowView.findViewById(R.id.tvgstarget);
viewHolder.tvgsact = (TextView) rowView.findViewById(R.id.tvgsact);
viewHolder.tvgsp = (TextView) rowView.findViewById(R.id.tvgsp);
rowView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvuserid.setText(parkingList.get(position).getName()+"");
viewHolder.tvvstarget.setText(parkingList.get(position).getVisibility_Tgt()+"");
viewHolder.tvvsact.setText(parkingList.get(position).getVisibility_Actual()+"");
viewHolder.tvuserid.setText(parkingList.get(position).getName()+"");
viewHolder.tvgptarget.setText(parkingList.get(position).getGPTgt()+"");
viewHolder.tvgpact.setText(parkingList.get(position).getGPAct()+"");
viewHolder.tvuserid.setText(parkingList.get(position).getName()+"");
viewHolder.tvgstarget.setText(parkingList.get(position).getGSTgt()+"");
viewHolder.tvgsact.setText(parkingList.get(position).getGSAct()+"");
Double value1 = Double.parseDouble(viewHolder.tvvstarget.getText().toString());
Double value2 = Double.parseDouble(viewHolder.tvvsact.getText().toString());
Double calculatedValue = (value2*100)/value1;
String formattedValue = String.format("%.2f", calculatedValue);
viewHolder.tvvsp.setText(formattedValue.toString()+"%");
Double value3 = Double.parseDouble(viewHolder.tvgptarget.getText().toString());
Double value4 = Double.parseDouble(viewHolder.tvgpact.getText().toString());
Double calculatedValue2 = (value4*100)/value3;
String formattedValue2 = String.format("%.2f", calculatedValue2);
viewHolder.tvgpp.setText(formattedValue2.toString()+"%");
Double value5 = Double.parseDouble(viewHolder.tvgstarget.getText().toString());
Double value6 = Double.parseDouble(viewHolder.tvgsact.getText().toString());
Double calculatedValue3 = (value6*100)/value5;
String formattedValue3 = String.format("%.2f", calculatedValue3);
viewHolder.tvgsp.setText(formattedValue3.toString()+"%");
return rowView;
}
public void filter(String charText){
charText = charText.toLowerCase(Locale.getDefault());
parkingList.clear();
if(charText.length() == 0){
parkingList.addAll(arrayList);
} else {
for (ClassListItems apps: arrayList){
if (apps.getName().toLowerCase(Locale.getDefault()).contains(charText)){
parkingList.add(apps);
}
}
}
notifyDataSetChanged();
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
Intent myIntent = new Intent(ManagerDashboard.this,
ManagerActivity.class);
startActivity(myIntent);
} else if (id == R.id.nav_profile) {
Intent myIntent = new Intent(ManagerDashboard.this,
ManagerProfile.class);
startActivity(myIntent);
} else if (id == R.id.nav_dashboard) {
} else if (id == R.id.nav_logout) {
LogOut();
return true;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void LogOut() {
SharedPreferences.Editor edit = shp.edit();
edit.putString("UserId", "");
edit.commit();
Intent intent = new Intent(ManagerDashboard.this, MainActivity.class);
startActivity(intent);
this.finish();
}
这是活动xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="170dp"
android:id="@+id/linearLayout1">
<TextView
android:layout_marginLeft="20dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Seller"
android:textStyle="bold"
android:textColor="#000000"
android:textSize="15dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.23"
android:text="VS %"
android:textStyle="bold"
android:textColor="#000000"
android:textSize="15dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.18"
android:text="GP %"
android:textStyle="bold"
android:textColor="#000000"
android:textSize="15dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.18"
android:text="GS %"
android:textStyle="bold"
android:textColor="#000000"
android:textSize="15dp"/>
</LinearLayout>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
app:srcCompat="@drawable/logo" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:choiceMode="multipleChoice"
android:id="@+id/list"
android:layout_marginTop="210dp"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="170dp"
android:background="@android:color/darker_gray" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/linearLayout1"
android:background="@android:color/darker_gray" />
<EditText
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="95dp"
android:drawableRight="@drawable/search"
android:layout_marginLeft="150dp"
android:layout_marginRight="150dp"/>
这是listview xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:id="@+id/tvuserid"
android:layout_marginLeft="20dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Seller"
android:textColor="#000000"
android:textSize="15dp"/>
<TextView
android:id="@+id/tvvsp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="VS %"
android:textColor="#000000"
android:textSize="15dp"/>
<TextView
android:id="@+id/tvgpp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="GP %"
android:textColor="#000000"
android:textSize="15dp"/>
<TextView
android:id="@+id/tvgsp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="GS %"
android:textColor="#000000"
android:textSize="15dp"/>
</LinearLayout>
<TextView
android:id="@+id/tvvstarget"
android:layout_width="0dp"
android:layout_weight="0"
android:layout_height="wrap_content"
android:textSize="15dp"
android:visibility="invisible"/>
<TextView
android:id="@+id/tvvsact"
android:layout_width="0dp"
android:layout_weight="0"
android:layout_height="wrap_content"
android:textSize="15dp"
android:visibility="invisible"/>
<TextView
android:id="@+id/tvgptarget"
android:layout_width="0dp"
android:layout_weight="0"
android:layout_height="wrap_content"
android:textSize="15dp"
android:visibility="invisible"/>
<TextView
android:id="@+id/tvgpact"
android:layout_width="0dp"
android:layout_weight="0"
android:layout_height="wrap_content"
android:textSize="15dp"
android:visibility="invisible"/>
<TextView
android:id="@+id/tvgstarget"
android:layout_width="0dp"
android:layout_weight="0"
android:layout_height="wrap_content"
android:textSize="15dp"
android:visibility="invisible"/>
<TextView
android:id="@+id/tvgsact"
android:layout_width="0dp"
android:layout_weight="0"
android:layout_height="wrap_content"
android:textSize="15dp"
android:visibility="invisible"/>
为什么listview有额外的空间?即使我在xml文件中添加填充它也不会改变。当我在viewholder中添加很多id时会发生这种情况。这意味着如果在视图中只有gt 4 id,则listview在每个项目的底部都没有额外的空间。如何划分间距?任何人都帮忙。谢谢。
将listView的高度更改为wrap_content。
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:choiceMode="multipleChoice"
android:id="@+id/list"
android:layout_marginTop="210dp"/>