0

这是我的问题,我有 2 个活动,一个包含 Listview 和一个添加按钮,该按钮调用另一个包含 listview 和搜索面板的活动,当用户搜索产品并按下它时,产品会被发送回主要活动(具有列表视图和按钮的活动)和产品被添加到列表视图中。

这就是我要解决的问题:

1)在用户找到他想要的产品并单击它后,当 MAIN 活动重新加载时,它会在键盘打开的情况下重新加载,我无法让它不打开。

2)每个列表视图项目都很大!它几乎占据了整个屏幕,每个列表视图项目之间的空间很大。

这是relavnet代码:

主要活动:

public class Main extends Activity
{
    public ListView lstView;
    ProductAdapter productListAdapter;
    DBAdaptor mDb;
    ArrayList<Product> products;
    EditText et;
    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_screen);
        InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
        openDB();
        Bundle b = this.getIntent().getExtras();
        if(b!=null) 
        {
            products =(ArrayList<Product>) b.getSerializable("products");
            productListAdapter = new ProductAdapter(this, R.layout.shoping_list_row,ArrayListToArray(products));
            lstView = (ListView)findViewById(R.id.main_list);
            lstView.setAdapter(productListAdapter);

        }
    }

    private Product[] ArrayListToArray(ArrayList<Product> products)
    {
        Product[] prods = new Product[products.size()];
        for (int i = 0; i < prods.length; i++)
        {
            prods[i]=products.get(i);
        }
        return prods;
    }

    public void startSearchActivity(View v)
    {
        Intent i = new Intent(this,SearchActivity.class);
        if(products!=null)
        {
            Bundle b = new Bundle();
            b.putSerializable("products", products);
            i.putExtras(b);
        }

        startActivity(i);
    }

    private void openDB()
    {
        mDb = new DBAdaptor(this);  
        mDb.open();

    }
}

这是 Main Activity 的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical" >

    <ListView
        android:id="@+id/main_list"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"

         >

    </ListView>

    <Button
        android:id="@+id/button1"
        style="@style/AppTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:onClick="startSearchActivity"
        android:text="+"
        android:textColor="#009900"
        android:textSize="50sp" />

</LinearLayout>

继承人的搜索活动:

public class SearchActivity extends Activity implements Serializable {




    /*
     *Class Properties 
     */

    private ListView searchListView;
    ArrayAdapter<String> shopingListAdapter;
    EditText inputSearch;
    DBAdaptor mDb;
    ArrayList<Product> products;






    /*
     *Initialize All Views And Data For ListView
     */
    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_screean_layout);
        searchListView = (ListView) findViewById(R.id.list_view);
        inputSearch = (EditText) findViewById(R.id.inputSearch);
        InputMethodManager imm = (InputMethodManager)getSystemService(
                  Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0);
        Bundle b = this.getIntent().getExtras();
        if(b!=null) products =(ArrayList<Product>) b.getSerializable("products");
        openDB();
        fillListView();
        setOnTextChangedListener();
        setOnItemClickListener();
        setOnEditorActionListener();


    }

    /*
     *Fills the ListView with data from the Database
     * 
     */
    private void fillListView() 
    {
        String[] products = getAllProducts();
        shopingListAdapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
        searchListView.setAdapter(shopingListAdapter);  

    }

    private void setOnItemClickListener()
    {
        searchListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) 
            {
                if(products==null) products = new ArrayList<Product>();
                String selectedProduct = (String)searchListView.getItemAtPosition(arg2);
                products.add(new Product(DBAdaptor.getProductByName(selectedProduct).getImg(),selectedProduct, 0));
                Bundle b = new Bundle();
                b.putSerializable("products", products);
                Intent i = new Intent(getBaseContext(),Main.class);
                i.putExtras(b);
                startActivity(i);

                //START INTENT TO MAIN SCREEN


            }
        });
    }

    /*
     *Sets the event listener
     * for the ListView Search Bar
     * Each time a letter will be entered the filter kick in;
     */

    private void setOnTextChangedListener() {
        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                SearchActivity.this.shopingListAdapter.getFilter().filter(cs);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
            }
        });

    }

    /*
     *Gets all the products from the Database
     */

    private String[] getAllProducts()
    {

        ArrayList<Product> list = mDb.getAllProducts();
        String[] products = new String[list.size()];
        for (int i = 0; i < products.length; i++) 
        {
            products[i] = list.get(i).getName();
        }
        return products;


    }

    /*
     *Initialize DBAdapter
     * And opens communication with the Database 
     */
    private void openDB()
    {
        mDb = new DBAdaptor(this);  
        mDb.open();
        mDb.deletAllProductTable();
        mDb.createProductEntry(new Product(R.drawable.bread_1_icon, "Bread", 0));


    }

    public void onEditTextClick(View v)
    {

    }

    public void setOnEditorActionListener()
    {
        inputSearch.setOnEditorActionListener(
                new EditText.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                                actionId == EditorInfo.IME_ACTION_DONE ||
                                event.getAction() == KeyEvent.ACTION_DOWN &&
                                event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                            if (!event.isShiftPressed()) {

                                return true; 
                            }                
                        }
                        return false; // pass on to other listeners. 
                    }



                });
    }
}

下面是搜索活动的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <!-- Editext for Search -->
    <EditText android:id="@+id/inputSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Search products.."
        android:inputType="textVisiblePassword"
        android:onClick="onEditTextClick"/>

    <!-- List View -->
    <ListView
        android:id="@+id/list_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

     <ListView
        android:id="@+id/list_v"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

下面是 ListView 项目 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

     <ImageView
         android:id="@+id/imgIcon"
         android:layout_width="20dp"
         android:layout_height="20dp"
         android:gravity="center_vertical" />

     <TextView
         android:id="@+id/productName"
         android:layout_width="116dp"
         android:layout_height="51dp"
         android:gravity="center_vertical"
         android:textSize="22dp"
         android:textStyle="bold" />

     <EditText
         android:id="@+id/productQuantity"
         android:layout_width="98dp"
         android:layout_height="wrap_content"
         android:ems="10"
         android:gravity="center_vertical"
         android:textSize="22dp"
         android:textStyle="bold" />

</LinearLayout>

我知道这是很多代码,但我认为这是向您展示漏洞故事,以便你们可以帮助我。

非常感谢任何可以帮助我的人...

4

1 回答 1

1

在 Manifest 文件的 MainActivity 标记中使用它

android:windowSoftInputMode="stateHidden"

关于 ListViewItems 大小,请尝试降低 Items 布局中视图的高度。

并且默认情况下Item之间的差距必须很小。但无论如何你可以这样设置

android:dividerHeight="2dp"

但是在浏览完你的代码之后,你不应该在 MainActivity 中使用startActivityForResult而不是startActivity 。或者至少finish()在去 SearchActivity 之前。

于 2012-12-29T21:15:59.750 回答