0

ここに私の問題があります.2つのアクティビティがあります.1つはリストビューを保持し、もう1つはリストビューと検索パネルを保持する別のアクティビティを呼び出す追加ボタンです.ユーザーが製品を検索してそれを押すと、製品はメイン アクティビティ (リストビューとボタンのあるもの) と製品がリストビューに追加されます。

そして、これは私が解決したいものです:

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();

    }
}

メイン アクティビティのレイアウトは次のとおりです。

<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>

リストビュー アイテムの 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

これをマニフェスト ファイルの MainActivity タグで使用します。

android:windowSoftInputMode="stateHidden"

ListViewItems のサイズについては、Items レイアウトでビューの高さを下げてみてください。

デフォルトでアイテム間のギャップは小さくなければなりません。とにかく、このように設定できます

android:dividerHeight="2dp"

しかし、コードを調べた後、MainActivity の startActivity の代わりに startActivityForResult を使用ないでください。または、SearchActivity に移動する前に少なくともそれを行います。finish()

于 2012-12-29T21:15:59.750 に答える