java - Android ArrayList 到 ListView 用于搜索 Activity

标签 java android mysql listview arraylist

我正在尝试将产品的 ArrayList 传递为 ListView 格式。我很抱歉,因为我仍处于 Android 开发的初级阶段。任何帮助将不胜感激!

搜索.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 产品搜索:

<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 搜索:

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

最佳答案

也许你的适配器在 getView 方法中有错误

public View getView(int position, View convertView, ViewGroup parent)
{
...
return null; 
}

关于java - Android ArrayList 到 ListView 用于搜索 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32974729/

相关文章:

java - 在 Guice 中,如何创建具有不同作用域依赖关系的 Map Binder 单例绑定(bind)?

java - 提交后,票务按钮将完全更改

android - 收到一条错误消息使用 Android 位置服务模拟位置

php - 从mysql获取第二行的输出

php - 如何使用 Reddit 和 Hacker News 排名算法?

python - SQLAlchemy/MySQL 的表设计

java - 具有不相关返回类型的继承方法

java - 使用 CellSignalStrengthGsm 只给出 Integer.MAX_VALUE

android - Cocos 与安卓版 AndEngine

java - ScrollView 里面的 ImageView 应该适合设备布局