我有带图像和文本的customListview。我从数据库访问文本,图像存储为数组。当我点击一行listview时,我希望对应于该行图像的图像显示在另一行的图像视图中我不知道如何访问所选图像。我不想将图像存储在数据库中。
product_display.java
public class product_display extends Activity {
ListView prd_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product_dispay);
prd_list = (ListView) findViewById(R.id.list);
DataBaseHandler dbh = new DataBaseHandler(this);
Intent in=getIntent();
Bundle bundle=in.getExtras();
final String list=bundle.getString("key");
SQLiteDatabase db = dbh.getWritableDatabase();
Cursor cr = db.rawQuery("SELECT * FROM product", null);
final String[] pname = new String[cr.getCount()];
String[] price = new String[cr.getCount()];
int i = 0;
while(cr.moveToNext())
{
String name = cr.getString(cr.getColumnIndex("pname"));
String prprice = cr.getString(cr.getColumnIndex("pprice"));
pname[i] = name;
price[i] = prprice;
i++;
}
ListAdapter adapter = new ListAdapter(this, pname, price);
prd_list.setAdapter(adapter);
prd_list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String nme = pname[arg2];
// passing username from loginpage
Bundle bun = new Bundle();
bun.putString("key",list);
// passing product name as bundle
Bundle bn = new Bundle();
bn.putString("name",nme);
Intent in = new Intent(product_display.this, Product_Details.class);
in.putExtras(bun);
in.putExtras(bn);
startActivity(in);
}
});
}
}
ListAdapter.java
public class ListAdapter extends BaseAdapter {
private final String[] pname;
private final String[] price;
Integer [] pmge ={R.drawable.candle1,R.drawable.candl3,
R.drawable.candl4,R.drawable.candl5,R.drawable.candl6,
R.drawable.lawn,R.drawable.sglc10,R.drawable.senson,R.drawable.thejus6669};
private Context mcontext;
public ListAdapter(Context c,String[] pname,String[] price){
mcontext=c;
this.pname=pname;
this.price=price;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return pname.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View List;
LayoutInflater mLayoutinflater=(LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null){
List=new View(mcontext);
List=mLayoutinflater.inflate(R.layout.mylist, parent, false);
}
else{
List = (View)convertView;
}
TextView textView1 = (TextView)List.findViewById(R.id.pr_name);
TextView textView2 = (TextView)List.findViewById(R.id.pr_price);
ImageView imageview= (ImageView)List.findViewById(R.id.pr_img);
textView1.setText(pname[position].toString());
textView2.setText("Rs "+price[position] +" /-");
imageview.setImageResource(pmge[position]);
// TODO Auto-generated method stub
return List;
}
}
product_Details.java
public class Product_Details extends Activity{
TextView name,price,specification,feature;
String nme;
SQLiteDatabase mydb;
String pname;
String prprice;
String pspec;
String pfeature;
Button add2cart,by_nw;
ImageView image;
ImageButton imgbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.product_dtls);
image=(ImageView)findViewById(R.id.pr_img);
name = (TextView) findViewById(R.id.txtPr_name);
price = (TextView) findViewById(R.id.txtprice);
specification=(TextView)findViewById(R.id.txtPr_spec);
feature=(TextView)findViewById(R.id.txtPr_feature);
imgbtn=(ImageButton)findViewById(R.id.cartimg);
add2cart=(Button)findViewById(R.id.add2cart);
by_nw=(Button)findViewById(R.id.buy_nw);
DataBaseHandler dbh = new DataBaseHandler(this);
SQLiteDatabase db = dbh.getWritableDatabase();
Intent in = getIntent();
Bundle bn = in.getExtras();
Bundle bun=in.getExtras();
final String dtl=bun.getString("key");
nme = bn.getString("name");
while(cr.moveToNext())
{
String name = cr.getString(cr.getColumnIndex("pname"));
String pr1price = cr.getString(cr.getColumnIndex("pprice"));
String prspc=cr.getString(cr.getColumnIndex("pspec"));
String prfeature=cr.getString(cr.getColumnIndex("pfeature"));
pname = name;
prprice = pr1price;
pspec=prspc;
pfeature=prfeature;
}
name.setText(pname);
price.setText("Rs " +prprice + "/-");
specification.setText(pspec);
feature.setText(pfeature);
add2cart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
boolean incart=false;
String nm=name.getText().toString();
mydb=Product_Details.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
mydb.execSQL("CREATE TABLE IF NOT EXISTS add2cart(usr TEXT,img BLOB,pnme TEXT,prate NUMERIC,pqty NUMERIC,ptotl NUMERIC)");
Cursor cur=mydb.rawQuery("select * from add2cart where pnme='"+nm+"' AND usr='"+dtl+"'",null);
if (cur.moveToFirst()){
String prdname=cur.getString(cur.getColumnIndex("pnme"));
if (nm.equals(prdname)){
add2cart.setText("Already in Cart");
incart=true;
}
}
if(incart==false){
mydb.execSQL("INSERT INTO add2cart (usr,pnme,prate)VALUES('"+dtl+"','"+nm+"','"+prprice+"')");
Toast.makeText(getApplicationContext(),"added to cart",Toast.LENGTH_SHORT).show();
}
}
});
}
}
已编辑的代码
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
clas = (Integer)getListAdapter().getItem(arg2);
Intent in = new Intent(product_display.this, Product_Details.class);
in.putExtra("num", clas);
startActivity(in);
}
我通过
得到了价值Bundle extras = getIntent().getExtras();
int imgvalue = extras.getInt("num");
但如果将图像设置为我的图像视图,如果图像是我的图像视图
image.set ???
答案 0 :(得分:2)
点击Listview
// Capture ListView item click
itemVie.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
resulta = data.get(position);
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("albname", resulta.get(Portfolio.TAG_TITLE));
intent.putExtra("portimages",resulta.get(Portfolio.TAG_IMAGE));
context.startActivity(intent);
}
});
答案 1 :(得分:0)
如果你的图像在一个数组中,那么确保数组的索引与你从数据库中获取的数据的索引相匹配(你可以通过将光标传递给一个列表然后再将它传递给适配器来实现)然后在适配器的getview中,您可以通过以下代码获取图像:
//我认为你已经完成了我告诉你的事情。 // position是列表中的第n个项,item是从数据库获得的列表中的第n个对象(model)。 V是您要返回的当前视图
V.setTag(imageArray [position]);
此V将作为onListItemClick方法中的参数传递给您
通过这行代码获取图像
// V是传递的视图参数
Bitmap image = V.getTag();
答案 2 :(得分:0)
您可以使用ListView
调用适配器getItem
方法从getListAdapter
获取选定的行项目对象
prd_list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Integer class = (Integer)getListAdapter().getItem(arg2);
// pass this integer to the another activity and display there.
}