【问题标题】:Android ArrayList to ListView for search activityAndroid ArrayList 到 ListView 用于搜索活动
【发布时间】:2015-10-06 16:13:19
【问题描述】:

我正在尝试将产品的 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_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>

活动搜索:

<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,然后从 DB 中获取数据,然后在解析后将其传递给 listView 以进行填充吗?尝试在 oncreate() 中移动 listview 的初始化,然后告诉什么问题。
  • 去掉这条语句“ lv = (ListView) findViewById(R.id.list_view);”
  • Prepare 将声明活动的 onCreate。您只需要通过提供最新数据集而不是每次初始化列表视图和设置适配器来更新列表视图
  • 请提供您的 ProductAdapter 代码
  • 抱歉,我已经添加了 ProductAdapter 代码

标签: java android mysql listview arraylist


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-04
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多