0

我有一个自定义的列表视图,我把它放在一个相对布局中,它周围有一些按钮和标签,这个布局存储在 table_layout.xml 文件中,在列表视图的每一行中有三个文本视图和一个复选框,这些文本框将在运行时使用从数据库中提取的数据进行填充,我在一个名为 cell.xml 的单独 xml 文件中编写了这个布局,但是当我运行应用程序时,table_layout.xml 中的按钮和文本视图相互重叠,并且列表视图。但是当添加行时列表视图会滚动。我找不到原因,如果你建议我一个解决方案,我会很高兴。

这是 table_layout.xml

   <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    >

    <TextView 
        android:id="@+id/GOODNAME_CELL"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:background="@drawable/border"
        android:textSize="18dp"
        android:text="Order"
        android:paddingLeft="5dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"

        />
     <TextView 
        android:id="@+id/GOODUNITPRICE_CELL"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:background="@drawable/border"
        android:textSize="18dp"
        android:text="Unit Price"
        android:paddingLeft="5dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/GOODNAME_CELL"
        />
      <TextView 
        android:id="@+id/QUANTITYCELL"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:background="@drawable/border"
        android:textSize="18dp"
        android:text="Quantity"
        android:paddingLeft="5dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/GOODUNITPRICE_CELL"
        />



 <ListView 
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="@style/CodeFont"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/GOODUNITPRICE_CELL"
     >
    </ListView>

 <TextView
     android:id="@+id/TotalPriceTextview"
     android:layout_width="600dp"
     android:layout_height="wrap_content"
     android:text=""
     android:layout_below="@android:id/list"
     android:layout_centerHorizontal="true"
     android:background="@drawable/back"
     android:textColor="#FFFFFF"
     android:textSize="20dp"
        />
 <Button 
     android:id="@+id/DeleteSelectedGoodsButton"
     android:layout_width="150dp"
     android:layout_height="45dp"
     android:text="@string/DeleteSelectedGoods"
     android:layout_alignParentBottom="true"
     android:layout_alignParentLeft="true"
   />
 <Button 
     android:id="@+id/ConfirmDelete"
     android:layout_width="150dp"
     android:layout_height="45dp"
     android:text="@string/ConfirmDeleteButton"
     android:layout_alignParentBottom="true"
     android:layout_centerHorizontal="true"

     />
 <Button 
     android:id="@+id/CancelButton"
     android:layout_width="150dp"
     android:layout_height="45dp"
     android:text="@string/CancelButton"
     android:layout_alignParentBottom="true"
     android:layout_alignParentRight="true"

     />

</RelativeLayout>

这是cell.xml

    <?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:paddingTop="4dip"
    android:paddingBottom="6dip"
    android:orientation="horizontal"
    android:background="#000000"
    android:layout_marginRight="5dp"

    >

    <TextView 
        android:id="@+id/GOOD_NAME_CELL"
        android:layout_width="110dp"
        android:layout_height="55dp"
        android:layout_weight="1"
        android:textSize="18dp"
        android:textColor="#FFFFFF"
         android:paddingLeft="5dp"

        />
     <TextView 
        android:id="@+id/GOOD_UNITPRICE_CELL"
        android:layout_width="110dp"
        android:layout_height="55dp"
        android:layout_weight="1"
        android:textSize="18dp"
        android:textColor="#FFFFFF"
         android:paddingLeft="5dp"

        />
      <TextView 
        android:id="@+id/QUANTITY_CELL"
        android:layout_width="80dp"
        android:layout_height="55dp"
        android:layout_weight="1"
        android:textSize="18dp"
        android:paddingLeft="5dp"
        android:textColor="#FFFFFF"

        />
        <CheckBox 
          android:layout_width="40dp"
          android:layout_height="40dp"
          android:id="@+id/ChkOrder"
         android:layout_marginRight="15dp"
         android:clickable="true"
         android:focusable="false"

          />  

    </LinearLayout>

这是屏幕截图: 在此处输入图像描述

代码.java:

package com.example.nfc;


import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import com.example.nfc.ExternalDbOpenHelper;

import android.R.integer;
import android.R.string;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.SparseBooleanArray;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class PrepopSqliteDbActivity extends ListActivity {

    private static final String DB_NAME = "NFC.sqlite";
    //------------------------------------------------------------
    private static final String TABLE_NAME = "Tbl_Goods";
    private static final String GOOD_ID = "Good_ID";
    private static final String CART_ID = "Cart_ID";
    private static final String GOOD_NAME = "Good_Name";
    private static final String GOOD_UNITPRICE = "Good_UnitPrice";
    private static final String QUANTITY = "Quantity";


     Cursor goodsCursor;

    private SQLiteDatabase database;
    private ListView listView;
     int checkedItemPosition;




    ArrayList<HashMap<String, String>> goods=new ArrayList<HashMap<String,String>>();
    HashMap<String, String> map;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table_layout);

        //-------------------------------------------------------
        ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
        database = dbOpenHelper.openDataBase();
        //--------------------------------------



        fillgoods();
        ShowTotalPrice() ;
        setUpList(); 




  //////Delete Button

       Button DeleteGoods=(Button)findViewById(R.id.DeleteSelectedGoodsButton);
       DeleteGoods.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                /** Getting the position of the currently selected item**/

                  DeleteCheckedItem(checkedItemPosition);

                Toast.makeText(getApplicationContext(),"The selected order has been deleted successfully.", Toast.LENGTH_SHORT).show();

                finish();
                startActivity(getIntent());

            }

        });

        ///Confirm Button
       Button btnConfirm=(Button)findViewById(R.id.ConfirmDelete);
       btnConfirm.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            //ShowTotalPrice();

        }
    });
        ///Cancel Button
        Button btnCancel=(Button)findViewById(R.id.CancelButton);
        btnCancel.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {

                finish();
            }
        });

//ListView OnItemClickListener


listView.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        CheckBox cbx = (CheckBox)view.findViewById(R.id.ChkOrder);

            int firstPosition = listView.getFirstVisiblePosition();
            for(int i=firstPosition;i<listView.getCount();i++){
            View v=listView.getChildAt(i);
            cbx = (CheckBox)v.findViewById(R.id.ChkOrder);
            if(cbx.isChecked()){

                 Toast.makeText(getApplicationContext(), 
                                 "Checked position " + goods.get(i), 
                                  Toast.LENGTH_SHORT).show();
                 checkedItemPosition=i;


           }
        }

     }

});

    }


    private void setUpList() {

        setListAdapter(new SimpleAdapter(this, goods, R.layout.cells,
                new String[]{GOOD_NAME,GOOD_UNITPRICE,QUANTITY},
                new int[]{R.id.GOOD_NAME_CELL,R.id.GOOD_UNITPRICE_CELL,
                R.id.QUANTITY_CELL}));



        //---------------------------------layout------------------------------------



        listView = getListView();
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

///////////////////////
View footer = LayoutInflater.from(this).inflate(
  R.layout.footer, null);

listView.addFooterView(footer);
///////////////////////////////////////

            }
    private void DeleteCheckedItem(int chkPosition)
    {   
        String strName=goods.get(chkPosition).get(GOOD_NAME);
        String strPrice=goods.get(chkPosition).get(GOOD_UNITPRICE);
        String strQuantity=goods.get(chkPosition).get(QUANTITY);

        String whereClause = "GOOD_NAME = ? AND GOOD_UNITPRICE= ? AND QUANTITY= ?";
        String[] whereArgs = {strName,strPrice,strQuantity};

        database.delete(TABLE_NAME, whereClause, whereArgs);

        fillgoods();
    }




    //--------------------------------------------------------------------
    private void fillgoods() {
        goods = new ArrayList<HashMap<String, String>>();
         Cursor goodsCursor = database.query(TABLE_NAME,
                                             new String[] 
                                             {GOOD_ID,CART_ID,GOOD_NAME,GOOD_UNITPRICE,QUANTITY},
                                             null, null, null, null
                                             , GOOD_NAME);


        String[] goodsTable=new String[] 
                 {GOOD_NAME,GOOD_UNITPRICE,QUANTITY};
        goodsCursor.moveToFirst();
        String[] names=new String[1000];
        if(!goodsCursor.isAfterLast()) {
            do {
                 int i=0;

                 names[i] = goodsCursor.getString(2);
                 names[i+1]=goodsCursor.getString(3);
                 names[i+2]=goodsCursor.getString(4);


                 map=new HashMap<String, String>();

                 map.put(goodsTable[i],names[i]);
                 map.put(goodsTable[i+1], names[i+1]);
                 map.put(goodsTable[i+2], names[i+2]); 


                 goods.add(map);


            } while (goodsCursor.moveToNext());
        }

        goodsCursor.close();

    }


    /////////////////////////////////////////////////////

    //////////////Home And Back Button
    @Override
    public void onAttachedToWindow() {
       this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
      super.onAttachedToWindow();


    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
       if(keyCode == KeyEvent.KEYCODE_HOME)

           BackToMainIntent();

     else if(keyCode==KeyEvent.KEYCODE_BACK)
       {
         BackToMainIntent();
      }
       return false;
    }



    public void BackToMainIntent()
    {
        Intent intent = new Intent(this, Main.class);
           intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
           startActivity(intent);
      }
    }

日志猫:

02-16 11:26:51.171: E/AndroidRuntime(222): Uncaught handler: thread main exiting due to uncaught exception
02-16 11:26:51.182: E/AndroidRuntime(222): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nfc/com.example.nfc.PrepopSqliteDbActivity}: java.lang.NullPointerException
02-16 11:26:51.182: E/AndroidRuntime(222):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
02-16 11:26:51.182: E/AndroidRuntime(222):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-16 11:26:51.182: E/AndroidRuntime(222):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-16 11:26:51.182: E/AndroidRuntime(222):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-16 11:26:51.182: E/AndroidRuntime(222):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 11:26:51.182: E/AndroidRuntime(222):  at android.os.Looper.loop(Looper.java:123)
02-16 11:26:51.182: E/AndroidRuntime(222):  at android.app.ActivityThread.main(ActivityThread.java:4363)
02-16 11:26:51.182: E/AndroidRuntime(222):  at java.lang.reflect.Method.invokeNative(Native Method)
02-16 11:26:51.182: E/AndroidRuntime(222):  at java.lang.reflect.Method.invoke(Method.java:521)
02-16 11:26:51.182: E/AndroidRuntime(222):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-16 11:26:51.182: E/AndroidRuntime(222):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-16 11:26:51.182: E/AndroidRuntime(222):  at dalvik.system.NativeStart.main(Native Method)
02-16 11:26:51.182: E/AndroidRuntime(222): Caused by: java.lang.NullPointerException
02-16 11:26:51.182: E/AndroidRuntime(222):  at com.example.nfc.PrepopSqliteDbActivity.ShowTotalPrice(PrepopSqliteDbActivity.java:164)
02-16 11:26:51.182: E/AndroidRuntime(222):  at com.example.nfc.PrepopSqliteDbActivity.onCreate(PrepopSqliteDbActivity.java:79)
02-16 11:26:51.182: E/AndroidRuntime(222):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-16 11:26:51.182: E/AndroidRuntime(222):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
02-16 11:26:51.182: E/AndroidRuntime(222):  ... 11 more
02-16 11:26:51.192: I/Process(51): Sending signal. PID: 222 SIG: 3
02-16 11:26:51.192: I/dalvikvm(222): threadid=7: reacting to signal 3
02-16 11:26:51.202: E/dalvikvm(222): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
02-16 11:26:51.373: I/ARMAssembler(51): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x4e2f38:0x4e2ff4] in 383010 ns

提前致谢

4

1 回答 1

0

有两种解决方案:

首先,您可以将带有 textview 的树按钮放在线性布局中,并将此线性布局放在 xml 布局的底部

在这种情况下,按钮不会随着项目滚动

<Linearlayout layout_width= "fill_parent"
layout_hight = "warp_conetent"
orentation = "horizantally"
alighparentbotton = "true"
>
<TextView
     android:id="@+id/TotalPriceTextview"
     android:layout_width="600dp"
     android:layout_height="wrap_content"
     android:text=""
     android:background="@drawable/back"
     android:textColor="#FFFFFF"
     android:textSize="20dp"
        />
 <Button 
     android:id="@+id/DeleteSelectedGoodsButton"
     android:layout_width="150dp"
     android:layout_height="45dp"
     android:text="@string/DeleteSelectedGoods"
   />
 <Button 
     android:id="@+id/ConfirmDelete"
     android:layout_width="150dp"
     android:layout_height="45dp"
     android:text="@string/ConfirmDeleteButton"

     />
 <Button 
     android:id="@+id/CancelButton"
     android:layout_width="150dp"
     android:layout_height="45dp"
     android:text="@string/CancelButton"
     android:layout_alignParentBottom="true"
     android:layout_alignParentRight="true"

     />
</linearlayout>

其次,您可以使用将这三个按钮和文本视图放在页脚中并将此页脚添加到列表视图中。在这种情况下,页脚将随项目一起滚动

  View footer = LayoutInflater.from(this).inflate(
                    R.layout.footer, null);

yourListView.addFooterView(footer);

页脚.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    >
 <TextView
     android:id="@+id/TotalPriceTextview"
     android:layout_width="600dp"
     android:layout_height="wrap_content"
     android:text=""
     android:layout_below="@android:id/list"
     android:layout_centerHorizontal="true"
     android:background="@drawable/back"
     android:textColor="#FFFFFF"
     android:textSize="20dp"
        />
 <Button 
     android:id="@+id/DeleteSelectedGoodsButton"
     android:layout_width="150dp"
     android:layout_height="45dp"
     android:text="@string/DeleteSelectedGoods"
     android:layout_alignParentBottom="true"
     android:layout_alignParentLeft="true"
   />
 <Button 
     android:id="@+id/ConfirmDelete"
     android:layout_width="150dp"
     android:layout_height="45dp"
     android:text="@string/ConfirmDeleteButton"
     android:layout_alignParentBottom="true"
     android:layout_centerHorizontal="true"

     />
 <Button 
     android:id="@+id/CancelButton"
     android:layout_width="150dp"
     android:layout_height="45dp"
     android:text="@string/CancelButton"
     android:layout_alignParentBottom="true"
     android:layout_alignParentRight="true"

     />

</RelativeLayout>

编辑后

package com.stackoverflow.questions;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import android.R.integer;
import android.R.string;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import android.util.SparseBooleanArray;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class StackActivity extends Activity {

    private static final String DB_NAME = "NFC.sqlite";
    // ------------------------------------------------------------
    private static final String TABLE_NAME = "Tbl_Goods";
    private static final String GOOD_ID = "Good_ID";
    private static final String CART_ID = "Cart_ID";
    private static final String GOOD_NAME = "Good_Name";
    private static final String GOOD_UNITPRICE = "Good_UnitPrice";
    private static final String QUANTITY = "Quantity";

    Cursor goodsCursor;

    private SQLiteDatabase database;
    private ListView listViewsssss;
    int checkedItemPosition;

    ArrayList<HashMap<String, String>> goods = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table_layout);
        // fillgoods();
        setUpList();

    }

    private void setUpList() {

        listViewsssss = (ListView) findViewById(R.id.list);

        View footer = LayoutInflater.from(this).inflate(R.layout.footer, null);

        listViewsssss.addFooterView(footer);

        listViewsssss.setAdapter(new SimpleAdapter(this, goods, R.layout.cell,
                new String[] { GOOD_NAME, GOOD_UNITPRICE, QUANTITY },
                new int[] { R.id.GOOD_NAME_CELL, R.id.GOOD_UNITPRICE_CELL,
                        R.id.QUANTITY_CELL }));

        // ---------------------------------layout------------------------------------

        listViewsssss.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        // /////////////////////

        // /////////////////////////////////////

    }

    // --------------------------------------------------------------------

    @Override
    public void onAttachedToWindow() {
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
        super.onAttachedToWindow();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_HOME)

            BackToMainIntent();

        else if (keyCode == KeyEvent.KEYCODE_BACK) {
            BackToMainIntent();
        }
        return false;
    }

    public void BackToMainIntent() {
        Intent intent = new Intent(this, StackActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
}

在此处输入图像描述

我将页脚更改为

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/TotalPriceTextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="asdf"
        android:textColor="#FFFFFF"
        android:textSize="20dp" />

    <Button
        android:id="@+id/DeleteSelectedGoodsButton"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:text="asdf" />

    <Button
        android:id="@+id/ConfirmDelete"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:text="sdaf" />

    <Button
        android:id="@+id/CancelButton"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:text="asdf" />

</LinearLayout>
于 2013-02-16T07:22:03.853 回答