Reputation: 293
Heres my issue, I'v got 2 activities, one that holds a Listview and an add button that calls another activie that hold a listview and a search pannel, when the user searches a product and press's it, the products gets sent back to the Main activity (the one with the listview and the button) and the product is add'd to the listview.
and this is what i want to solve:
1) after the user finds the product he wants and clicks it, when the MAIN activity reloads it reloads with the keyboard open and i cant get it to not open.
2) Each listview item is huge!, it hold up a almost a full screen and the space between each listview item is to big.
Here's the relavnet codde:
Main Activity:
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();
}
}
Heres the layout for the 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>
Heres the search activity:
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.
}
});
}
}
Heres the XML for the search activity:
<?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>
Heres the ListView Item 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>
I know it's a lot of code, but i figured it's to show you the hole story so you guys can help me.
Thanks a lot to any one who can help me...
Upvotes: 0
Views: 1220
Reputation: 24012
Use this in the MainActivity tag of the Manifest file
android:windowSoftInputMode="stateHidden"
Regarding the ListViewItems size, try lowering the height of the views in Items layout.
and the gap between the Items by default must be small. But anyways you can set it this way
android:dividerHeight="2dp"
But after going through your code, Shouldn't you use startActivityForResult in stead of just startActivity in the MainActivity. Or atleast finish()
it before going to the SearchActivity.
Upvotes: 1