0
我想传递产品ArrayList到ListView格式。我很抱歉,因为我仍处于Android开发的初始阶段。任何援助将不胜感激!Android ArrayList到ListView的搜索活动
Search.java:
public class Search extends Activity {
// placeholder that you will be updating with the database data
private EditText inputSearch;
private ListView lv;
private String search;
ArrayList<Product> products;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_search);
inputSearch = (EditText) findViewById(R.id.inputSearch);
search = inputSearch.getText().toString();
new getDataFromDatabase().execute();
}
private class getDataFromDatabase extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... arg0) {
Connection conn = ConnectDB.getConnection();
lv = (ListView) findViewById(R.id.list_view);
products = SearchQuery.returnProductView(search, conn);
return null;
}
protected void onPostExecute(Void result) {
ProductAdapter adbProduct;
adbProduct = new ProductAdapter(Search.this, 0, products);
ListView listview = (ListView) findViewById(R.id.list_view);
listview.setAdapter(adbProduct);
}
}
}
SearchQuery.java:
public class SearchQuery extends Activity {
public SearchQuery() {}
public static ArrayList<Product> returnProductView (String search, Connection conn){
ArrayList<Product> list = new ArrayList<Product>();
Product product = null;
try {
PreparedStatement ps = conn.prepareStatement("SELECT * FROM inventory WHERE Name Like ? OR Department LIKE ? OR Description Like ?");
ps.setString(1, "%" + search + "%");
ps.setString(2, "%" + search + "%");
ps.setString(3, "%" + search + "%");
ResultSet result = ps.executeQuery();
while (result.next()) {
product = new Product();
product.setSKU(result.getInt("SKU"));
product.setProduct_name(result.getString("Name"));
product.setProduct_dept(result.getString("Department"));
product.setPrice(result.getFloat("Price"));
product.setProduct_desc(result.getString("Description"));
product.setProduct_img(result.getString("Image"));
product.setProduct_qty(result.getInt("Quantity"));
list.add(product);
}
//request.setAttribute("products", product_list);
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
return list;
}
}
ProductAdapter.java:
public class ProductAdapter extends ArrayAdapter<Product> {
private Activity activity;
private ArrayList<Product> products;
private static LayoutInflater inflater = null;
public ProductAdapter(Activity activity, int textViewResourceId, ArrayList<Product> products) {
super(activity, textViewResourceId, products);
try {
this.activity = activity;
this.products = products;
} catch (Exception e) {
return;
}
}
public int getCount() {
return products.size();
}
public Product getItem(Product position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView product_name;
public TextView product_desc;
public TextView product_price;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
final ViewHolder holder;
try {
if(convertView == null) {
vi = inflater.inflate(R.layout.activity_product_search, parent, false);
holder = new ViewHolder();
holder.product_name = (TextView) vi.findViewById(R.id.product_name);
holder.product_desc = (TextView) vi.findViewById(R.id.product_desc);
holder.product_price = (TextView) vi.findViewById(R.id.product_price);
vi.setTag(holder);
}
else {
holder = (ViewHolder) vi.getTag();
}
holder.product_name.setText(products.get(position).getProduct_name());
holder.product_desc.setText(products.get(position).getProduct_desc());
holder.product_price.setText(String.valueOf(products.get(position).getPrice()));
}
catch(Exception e) {
}
return vi;
}
}
activity_product_search:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:orientation="vertical" >
<EditText
android:id="@+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textVisiblePassword"
android:hint="Search products.. "/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_view">
</ListView>
</LinearLayout>
activity_search:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:orientation="vertical" >
<!-- Dislpay Product name, Product description, and Product price -->
<!-- Product name -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/product_name"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>
<!-- Product description -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/product_desc"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>
<!-- Product price -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/product_price"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>
</LinearLayout>
最后,错误堆栈:
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: FATAL EXCEPTION: main
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: Process: com.android.softwear, PID: 21043
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.widget.AbsListView.obtainView(AbsListView.java:2360)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.widget.ListView.makeAndAddView(ListView.java:1864)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.widget.ListView.fillDown(ListView.java:698)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.widget.ListView.fillFromTop(ListView.java:759)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.widget.ListView.layoutChildren(ListView.java:1673)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.widget.AbsListView.onLayout(AbsListView.java:2148)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.View.layout(View.java:15596)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4966)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.View.layout(View.java:15596)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4966)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.View.layout(View.java:15596)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4966)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.View.layout(View.java:15596)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4966)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.View.layout(View.java:15596)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4966)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:580)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:550)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5221)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
10-06 01:04:03.431 21043-21043/com.android.softwear E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
不应该被ü在草签您的onCreate的ListView ()然后只是从数据库获取数据,然后解析后传递给listView得到填充?尝试在oncreate()中移动listview的初始化,然后说出是什么问题。 –
删除此语句“lv =(ListView)findViewById(R.id.list_view);” –
准备将宣布活动的创建。您只需要通过提供最新数据集来更新列表视图,而不是每次初始化列表视图和设置适配器 –