我基本上是在创建一个购物车应用程序,我的listView正在从我现有的数据库中填充,它为您提供商品,商品,价格,描述和商品图片,并且每张图片的信息都存储在数据库并放入我创建的Item类中。因此,当列表视图中显示的每个项目时,它还会显示每个图像底部的“添加到购物车”按钮,以便我可以单击该按钮并将其添加到另一个活动的购物车中。基本上,当我单击此按钮时,我想运行一个查询,将该项目添加到我的数据库,然后我将查询所有这些添加的项目的购物车活动,以便我可以显示我的所有项目添加到购物车那里。因此,当我点击按钮时,如何获取该特定项目并执行查询?它将如何获得该项目?我已在下面发布了我的代码文件,任何帮助都会很棒。
shirtsActivity.java
package ankitkaushal.app.healthysizing;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
public class shirtsActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shirts);
final SearchView shirtViewShirts = (SearchView) findViewById(R.id.searchView3);
final DatabaseHelper dbhelper;
final ListView listView;
final ListAdapter shirtsAdapter;
dbhelper = new DatabaseHelper(getApplicationContext());
try {
dbhelper.createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
listView = (ListView) findViewById(R.id.listViewShirts);
//List<Item> shirtsList = dbhelper.getAllShirts();
ArrayList<Item> shirtsList = dbhelper.getAllShirts();
if (shirtsList != null) {
//shirtsAdapter = new ArrayAdapter<Item>(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, shirtsList);
shirtsAdapter = new ListItemAdapter(getApplicationContext(), shirtsList);
listView.setAdapter(shirtsAdapter);
}
shirtViewShirts.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
//query = query.toLowerCase();
//query = Character.toString(query.charAt(0)).toUpperCase()+query.substring(1);
ListAdapter searchedShirtsAdapter;
Log.e("Brand: ", query);
ArrayList<Item> searchedShirtsList = dbhelper.getAllSearchedShirts(query);
if (searchedShirtsList != null) {
searchedShirtsAdapter = new ListItemAdapter(getApplicationContext(), searchedShirtsList);
listView.setAdapter(searchedShirtsAdapter);
}
listView.setEmptyView(findViewById(R.id.empty_shirts_message));
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
Item.java
package ankitkaushal.app.healthysizing;
public class Item {
private String brand;
private String size;
private String price;
private String store;
private String description;
private String ID;
private String gender;
public Item() {
}
public void setBrand(String brand) {
this.brand = brand;
}
public void setSize(String size) {
this.size = size;
}
public void setID(String ID) {
this.ID = ID;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setPrice(String price) {
this.price = price;
}
public void setStore(String store) {
this.store = store;
}
public void setDescription(String description) {this.description = description;}
public String getStore() {
return store;
}
public String getPrice() {
return price;
}
public String getGender() {return gender; }
public String getSize() {
return size;
}
public String getBrand() {
return brand;
}
public String getID() {
return ID;
}
public String getDescription() {return description; }
@Override
public String toString() {
return (this.brand + " " + this.store + " " + this.price + " " + this.description);
}
}
listItemAdapter.java
package ankitkaushal.app.healthysizing;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public final class ListItemAdapter extends ArrayAdapter<Item> implements View.OnClickListener{
public ListItemAdapter(Context context, ArrayList<Item> shirtItems) {
super(context, 0, shirtItems);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Item item = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_layout_shirts, parent, false);
}
// Lookup view for data population
TextView brand = (TextView) convertView.findViewById(R.id.txt_shirt_brand);
TextView price = (TextView) convertView.findViewById(R.id.txt_shirt_price);
TextView store = (TextView) convertView.findViewById(R.id.txt_shirt_store);
TextView size = (TextView) convertView.findViewById(R.id.txt_shirt_size);
TextView description = (TextView) convertView.findViewById(R.id.txt_shirt_description);
ImageView shirtsImage = (ImageView) convertView.findViewById(R.id.image_view_shirts);
Button addToCartButton = (Button) convertView.findViewById(R.id.addToCartButton);
// Populate the data into the template view using the data object
brand.setText("Brand:" + " " + item.getBrand());
price.setText("Price:" + " $" + item.getPrice());
store.setText("Store:" + " " + item.getStore());
size.setText("Size:" + " " + item.getSize());
description.setText("Description:" + " " + item.getDescription());
Context context = parent.getContext();
try {
String itemName = item.getID();
itemName = itemName.toLowerCase();
String uri1 = "@drawable/"+itemName;
int imageResource = context.getResources().getIdentifier(uri1, null, context.getApplicationContext().getPackageName());
Drawable drawable = context.getResources().getDrawable(imageResource);
shirtsImage.setImageDrawable(drawable);
} catch (Exception e) {
// TODO Auto-generated catch block
Drawable drawable = context.getResources().getDrawable(R.drawable.shirts); // Default image
shirtsImage.setImageDrawable(drawable);
}
addToCartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//to get a specific item
Toast.makeText(getContext(), "Your item has been added to the cart!", Toast.LENGTH_LONG).show();
}
});
// Return the completed view to render on screen
return convertView;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
activity_shirts.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#29A9D2"
android:weightSum="1"
android:id="@+id/shirt">
<SearchView
android:layout_width="358dp"
android:layout_height="wrap_content"
android:id="@+id/searchView3"
android:background="#ffffffff"
android:queryHint="Search for specific brand" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listViewShirts"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:fastScrollEnabled="true"
android:nestedScrollingEnabled="false"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<TextView
android:id="@+id/empty_shirts_message"
android:text="No Results Found"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#FFFFFF"
android:gravity="center|top"
android:textSize="25dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:visibility="invisible"
android:layout_marginTop="10dp"/>
</RelativeLayout>
</LinearLayout>
item_layout_shirts.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/txt_shirt_price"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/addToCartButton"
android:layout_alignEnd="@+id/addToCartButton"
android:layout_marginBottom="5dp"
android:layout_marginLeft="-5dp"
android:layout_marginTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/txt_shirt_brand"
android:layout_below="@+id/txt_shirt_price"
android:layout_alignLeft="@+id/txt_shirt_price"
android:layout_alignStart="@+id/txt_shirt_price"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/txt_shirt_store"
android:layout_below="@+id/txt_shirt_brand"
android:layout_alignLeft="@+id/txt_shirt_brand"
android:layout_alignStart="@+id/txt_shirt_brand"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/txt_shirt_size"
android:layout_below="@+id/txt_shirt_store"
android:layout_alignLeft="@+id/txt_shirt_store"
android:layout_alignStart="@+id/txt_shirt_store" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add item to Cart"
android:id="@+id/addToCartButton"
android:layout_below="@+id/txt_shirt_description"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:focusable="false"
android:focusableInTouchMode="false"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image_view_shirts"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/addToCartButton"
android:layout_alignBottom="@+id/txt_shirt_size"
android:layout_alignParentTop="true"
android:src="@drawable/shirts"
android:layout_marginLeft="10dp"
android:layout_toLeftOf="@+id/addToCartButton"
android:layout_marginBottom="-20dp"
android:layout_marginRight="-25dp"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/txt_shirt_description"
android:layout_below="@+id/image_view_shirts"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp" />
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:0)
您应该使用hirtsActivity.java文件中的最后一个函数:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
您可以选择特定项目,感谢id参数。