1

在我的主要布局中,我有一个网格视图,其中包含资源文件夹中的一些图像。当我单击主网格视图上的图像时,会弹出一个对话框窗口并显示另一个网格视图包含来自 sdcard 的图像。当我在对话框的网格视图上单击图像时,我想将单击的图像设置到在主网格视图上单击的位置。

public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image.add(R.drawable.split1);
image.add(R.drawable.spli21);
image.add(R.drawable.split22);
image.add(R.drawable.split31);
image.add(R.drawable.split32);
image.add(R.drawable.split33);
image.add(R.drawable.split34);
image.add(R.drawable.split41);
image.add(R.drawable.split42);
image.add(R.drawable.split43);
gv = (GridView) findViewById(R.id.gridView1);
gv.setAdapter(new AppsAdapter(MainActivity.this,image));
gv.setOnItemClickListener(new OnItemClickListener()  
 {   
public void onItemClick(AdapterView<?> parent, View view, int position,long id) 
{
// TODO Auto-generated method stub

//  Toast.makeText(getBaseContext(),
//        "pic" + (position) + " selected",
//          Toast.LENGTH_SHORT).show();
//Context context = getApplicationContext();
if(position==0)
{
createdialog(position);
}    
}
});
}

public void createdialog(int pos8)
{
pos1 = pos8;    
System.out.println("Position->"+pos1);
dialog = new Dialog(this);
dialog.setContentView(R.layout.content);
dialog.setTitle("Select Contents !!"); 
dirs.clear();
listFilesSDcard = new File(Environment.getExternalStorageDirectory()
                                                           .toString());
System.out.println("listFilesSDCard->"+listFilesSDcard);
getFolderList(listFilesSDcard, dirs);
System.out.println("ArrayList dirs Contains->"+dirs);
sgv = (GridView) dialog.findViewById(R.id.gridView);
sgv.setAdapter(new AppsAdapter1(dirs,pos1));
dialog.show();
}

/*  Apps adapter for dialog gridview        */

public class AppsAdapter1 extends BaseAdapter
 {

ArrayList<File> dirsTemp1 = new ArrayList<File>();
//int pos5;
/*Arraylist dirs contains the sdcard image files*/
public AppsAdapter1(ArrayList<File>dirs,int pos10) 
{
Log.d("In appsadapter2","Hiii");
dirsTemp1 = dirs;
pos2 = pos10;
}   
//---returns the number of images---

public final int getCount()
{    
return dirsTemp1.size();    
}

//---returns the ID of an item---

public final Object getItem(int position)
{
return dirsTemp1.get(position);
}

public final long getItemId(int position) 
{
return position;
}

public View getView(int position, View convertView, ViewGroup parent)
 {
// TODO Auto-generated method stub
//int i=1;

View v = null;
Bitmap mOriginalBitmap = null;
//View view;
//Bitmap map1;
final File f = (File) getItem(position);

if (convertView == null) 
{
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.gridicon, null);
ImageView iv = (ImageView) v.findViewById(R.id.imageView);
TextView tv = (TextView) v.findViewById(R.id.texttag);
tv.setTextColor(Color.BLACK);
String fName = f.getName();
if (f.isFile()) {
if (fName.endsWith(".png") || fName.endsWith(".jpeg")
|| fName.endsWith(".jpg")
|| fName.endsWith(".bmp")) 
{

tv.setText(fName);
mOriginalBitmap =BitmapFactory.decodeFile(f.getAbsolutePath());

}

if (mOriginalBitmap != null)
{
mOriginalBitmap = Bitmap.createScaledBitmap(mOriginalBitmap, 75, 75, true);
iv.setImageBitmap(mOriginalBitmap);
}
}
}
else 
{
v = convertView;
}
v.setOnLongClickListener(new OnLongClickListener() 
{
public boolean onLongClick(View v) 
{
// TODO Auto-generated method stub

//  ***** I stuck here ****/

if (dialog != null)
dialog.dismiss();
String path = f.getAbsolutePath();
Toast.makeText(getBaseContext(),"long clicked",Toast.LENGTH_LONG).show();
Bitmap pass = BitmapFactory.decodeFile(path);

return false;
}
});

return v;
}

}

我只是从 sd 卡中读取图像并将其显示在对话框的网格视图中。我如何用对话框网格视图上的长按图像替换主网格视图上的图像?我被对话框网格视图的onlong click监听器卡住了..请帮助我。提前致谢..

4

0 回答 0