继承我的问题,我有2个活动,一个持有Listview和一个添加按钮,调用另一个持有listview和搜索面板的活动,当用户搜索产品并按下它时,产品被发送返回主活动(带有列表视图和按钮的活动),产品将添加到列表视图中。
这就是我想要解决的问题:
1)在用户找到他想要的产品并点击它之后,当MAIN活动重新加载时,它会在键盘打开时重新加载,我无法打开它。
2)每个listview项目都很大!它几乎占据了整个屏幕,每个listview项目之间的空间都很大。
这是相关网络:
主要活动:
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>
继承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>
我知道这是很多代码,但我认为这是向你展示洞的故事,所以你们可以帮助我。
非常感谢任何能帮助我的人......
答案 0 :(得分:1)
在清单文件的MainActivity标记中使用它
android:windowSoftInputMode="stateHidden"
关于ListViewItems大小,请尝试降低Items布局中视图的高度。
默认情况下,项目之间的差距必须很小。但无论如何你可以这样设置
android:dividerHeight="2dp"
但是在完成代码之后,你不应该使用 startActivityForResult 而不是MainActivity中的 startActivity 。或者在转到SearchActivity之前至少finish()
。