我已经研究过this代码,并在应用中使用并且工作正常。但我的问题是,我认为意图始于放置在抽屉中的imegView的longClick事件。当我将代码放在MainActivity中时,我在图像视图上收到一个null点execption,所以我创建了一个像这样的新视图:
View view = LayoutInflater.from(getApplication()).inflate(R.layout.first_item, null);
但事件并没有开始。我无法将代码放在适配器中,因为我需要一个扩展" Activity"否则意图确实有效。
这是我的完整代码:
public class MainActivity extends Activity {
private static int PICK_IMAGE_REQUEST = 101;
private ImageView selectedImagePreview;
private DrawerLayout mDrawerLayout;
static ListView mDrawerList;
final String GREEN = "7f0b3452";
private static int itemInteger = 0;
int itemStatusInteger = 0;
DrawerItem dItem;
private static String newConsolName;
AddItem addItem = new AddItem();
static CustomDrawerAdapter adapter;
static List<DrawerItem> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = LayoutInflater.from(getApplication()).inflate(R.layout.first_item, null);
selectedImagePreview = (ImageView)view.findViewById(R.id.imageView);
selectedImagePreview.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
startGalleryIntent();
return false;
}
});
// Initializing
dataList = new ArrayList<DrawerItem>();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Add Drawer Item to dataList
dataList.add(new DrawerItem(R.mipmap.ps4miniatura, GREEN, 1));
dataList.add(new DrawerItem(R.mipmap.ps4miniatura, "PC", 0));
dataList.add(new DrawerItem(R.mipmap.ps4miniatura, "PC", 0));
dataList.add(new DrawerItem(R.mipmap.ps4miniatura, "PC", 0));
dataList.add(new DrawerItem(R.mipmap.ps4miniatura, "PC", 0));
dataList.add(new DrawerItem(R.mipmap.ps4miniatura, "PC", 0));
dataList.add(new DrawerItem(0, " ", 2));
adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item, adapter,
dataList);
// addItem.giveAdapter(adapter);
mDrawerList.setAdapter(adapter);
mDrawerList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
if (savedInstanceState == null) {
SelectItem(1);
}
}
public void SelectItem(int possition) {
Fragment fragment = null;
Bundle args = new Bundle();
switch (possition) {
case 1:
fragment = new Fragment1();
break;
case 2:
fragment = new Fragment2();
break;
case 3:
fragment = new Fragment3();
break;
case 4:
fragment = new Fragment4();
break;
default:
break;
}
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
mDrawerList.setItemChecked(possition, true);
setTitle(dataList.get(possition).getItemName());
mDrawerLayout.closeDrawer(mDrawerList);
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if ( dataList.get(position).getVersion() == 0) {
SelectItem(position);
} else {
}
}
}
public void startGalleryIntent(){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
try {
selectedImagePreview.setImageBitmap(new UserPicture(selectedImageUri, getContentResolver()).getBitmap());
} catch (IOException e) {
Log.e(MainActivity.class.getSimpleName(), "Failed to load image", e);
}
}
}
public String getPath(Uri uri) {
// just some safety built in
if( uri == null ) {
// perform some logging or show user feedback
Toast.makeText(getApplicationContext(), "false", Toast.LENGTH_LONG).show();
Log.d(MainActivity.class.getSimpleName(), "Failed to parse image path from image URI " + uri);
return null;
}
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
return uri.getPath();
}
}
我该怎么办?
答案 0 :(得分:0)
嗯,首先,您正在为新视图充气,但却没有使用your_layout.addView(查看)将其添加到现有布局中。
但由于这是主要问题的解决方法,因此适当的解决方案可能类似于适配器中的回调接口。像这样 - Callbacks between classes